List of usage examples for javax.crypto Mac doFinal
public final byte[] doFinal(byte[] input) throws IllegalStateException
From source file:org.gtri.totp.java
/** getCode * Calculates the code for the provided time and shared secret. *///from w ww .j av a2 s .c o m public int getCode(String sharedSecret, long time) throws NoSuchAlgorithmException, InvalidKeyException { byte[] secret = new Base32().decode(sharedSecret); SecretKeySpec signKey = new SecretKeySpec(secret, "HmacSHA1"); ByteBuffer buffer = ByteBuffer.allocate(8); buffer.putLong(time); byte[] timeBytes = buffer.array(); Mac mac = Mac.getInstance("HmacSHA1"); mac.init(signKey); byte[] hash = mac.doFinal(timeBytes); int offset = hash[19] & 0xf; long truncatedHash = hash[offset] & 0x7f; for (int i = 1; i < 4; i++) { truncatedHash <<= 8; truncatedHash |= hash[offset + i] & 0xff; } return (int) (truncatedHash %= 1000000); }
From source file:au.com.borner.salesforce.client.rest.domain.LoginResponse.java
public void verify(String consumerSecret) { SecretKey hmacKey = null;// ww w.ja v a 2s. c om try { byte[] key = consumerSecret.getBytes(); hmacKey = new SecretKeySpec(key, ALGORITHM); Mac mac = Mac.getInstance(ALGORITHM); mac.init(hmacKey); byte[] digest = mac.doFinal((getIdUrl() + getIssuedAt()).getBytes()); byte[] decode_sig = new Base64(true).decode(getSignature()); if (!Arrays.equals(digest, decode_sig)) { throw new SecurityException("Signature could not be verified!"); } } catch (NoSuchAlgorithmException e) { throw new SecurityException(String.format( "Algorithm not found while trying to verifying signature: algorithm=%s; message=%s", ALGORITHM, e.getMessage()), e); } catch (InvalidKeyException e) { throw new SecurityException( String.format("Invalid key encountered while trying to verify signature: key=%s; message=%s", hmacKey, e.getMessage()), e); } }
From source file:com.k42b3.aletheia.oauth.HMACSHA1.java
public String build(String baseString, String consumerSecret, String tokenSecret) { try {/* w w w .jav a2 s .c om*/ String key = Oauth.urlEncode(consumerSecret) + "&" + Oauth.urlEncode(tokenSecret); Mac mac = Mac.getInstance("HmacSHA1"); SecretKeySpec secret = new SecretKeySpec(key.getBytes(), "HmacSHA1"); mac.init(secret); byte[] result = mac.doFinal(baseString.getBytes()); return Base64.encodeBase64String(result); } catch (Exception e) { Aletheia.handleException(e); return null; } }
From source file:net.webpasswordsafe.server.plugin.authentication.TwoStepTOTPAuthenticator.java
private int calculateCode(byte[] key, long t) throws NoSuchAlgorithmException, InvalidKeyException { byte[] data = new byte[8]; long value = t; for (int i = 8; i-- > 0; value >>>= 8) { data[i] = (byte) value; }//from ww w . jav a 2 s . co m SecretKeySpec signKey = new SecretKeySpec(key, "HmacSHA1"); Mac mac = Mac.getInstance("HmacSHA1"); mac.init(signKey); byte[] hash = mac.doFinal(data); int offset = hash[20 - 1] & 0xF; long truncatedHash = 0; for (int i = 0; i < 4; ++i) { truncatedHash <<= 8; truncatedHash |= (hash[offset + i] & 0xFF); } truncatedHash &= 0x7FFFFFFF; truncatedHash %= 1000000; return (int) truncatedHash; }
From source file:com.coinkite.CoinkiteSigningRequestInterceptor.java
public String[] createSigAndTimestamp(String url) throws NoSuchAlgorithmException, InvalidKeyException { String apiSecret = getApiSecret(); SecretKeySpec signingKey = new SecretKeySpec(apiSecret.getBytes(StandardCharsets.UTF_8), HMAC_SHA512_ALG); Mac mac = Mac.getInstance(HMAC_SHA512_ALG); mac.init(signingKey);/*from w ww . ja v a 2 s. c om*/ String ts = getDateTime().format(ISO_DATE_TIME); byte[] bytes = mac.doFinal(getData(url, ts)); String encoded = Hex.encodeHexString(bytes); return new String[] { encoded, ts }; }
From source file:com.eucalyptus.auth.login.Hmacv1LoginModule.java
public String getSignature(final String queryKey, final String subject, final Hmac mac) throws AuthenticationException { SecretKeySpec signingKey = new SecretKeySpec(queryKey.getBytes(), mac.toString()); try {/*from w w w .ja v a 2s. co m*/ Mac digest = mac.getInstance(); digest.init(signingKey); byte[] rawHmac = digest.doFinal(subject.getBytes()); return sanitize(Base64.encode(rawHmac)); } catch (Exception e) { LOG.error(e, e); throw new AuthenticationException("Failed to compute signature"); } }
From source file:ninja.utils.Crypto.java
private String signHmacSha1(String value, String key) { try {/* ww w . ja va2 s . c o m*/ // Get an hmac_sha1 key from the raw key bytes byte[] keyBytes = key.getBytes(); SecretKeySpec signingKey = new SecretKeySpec(keyBytes, "HmacSHA1"); // Get an hmac_sha1 Mac instance and initialize with the signing key Mac mac = Mac.getInstance("HmacSHA1"); mac.init(signingKey); // Compute the hmac on input data bytes byte[] rawHmac = mac.doFinal(value.getBytes()); // Convert raw bytes to Hex byte[] hexBytes = new Hex().encode(rawHmac); // Covert array of Hex bytes to a String return new String(hexBytes, "UTF-8"); } catch (Exception e) { throw new RuntimeException(e); } }
From source file:org.ksb.util.geo.G3UrlSigner.java
/** * Provide Google API Compatible URL signing (see https://developers.google.com/maps/documentation/business/webservices/auth) * * @param path URL path String/*from w w w. j a va2s. c om*/ * @param query URL query String * @return String URL * * @throws NoSuchAlgorithmException * @throws InvalidKeyException * @throws UnsupportedEncodingException * @throws URISyntaxException */ public String signRequest(String path, String query) throws NoSuchAlgorithmException, InvalidKeyException, UnsupportedEncodingException, URISyntaxException { // Retrieve the proper URL components to sign String resource = path + '?' + query; // Get an HMAC-SHA1 signing key from the raw key bytes SecretKeySpec sha1Key = new SecretKeySpec(key, "HmacSHA1"); // Get an HMAC-SHA1 Mac instance and initialize it with the HMAC-SHA1 key Mac mac = Mac.getInstance("HmacSHA1"); mac.init(sha1Key); // compute the binary signature for the request byte[] sigBytes = mac.doFinal(resource.getBytes()); // base 64 encode the binary signature //String signature = Base64.encodeBase64URLSafeString(sigBytes); String signature = Base64.encodeBase64String(sigBytes); // convert the signature to 'web safe' base 64 signature = signature.replace('+', '-'); signature = signature.replace('/', '_'); return resource + "&signature=" + signature; }
From source file:com.mlohr.hvvgti.ApiClient.java
private String generateSignature(JSONObject data) { Charset passwordEncoding = Charset.forName("UTF-8"); String algorithm = getSignatureAlgorithm().getAlgorithmString(); byte[] key = authKey.getBytes(passwordEncoding); SecretKeySpec keySpec = new SecretKeySpec(key, algorithm); try {//from ww w . j a va 2s.c o m Mac mac = Mac.getInstance(algorithm); mac.init(keySpec); byte[] signature = mac.doFinal(data.toString().getBytes()); return new String(Base64.encodeBase64(signature)); } catch (NoSuchAlgorithmException e) { throw new RuntimeException(e); } catch (InvalidKeyException e) { throw new RuntimeException(e); } }
From source file:org.megam.api.APIContentBuilder.java
private String calculateHMAC(String secret, String data) throws NoSuchAlgorithmException, InvalidKeyException { SecretKeySpec signingKey = new SecretKeySpec(secret.getBytes(), "RAW"); Mac mac = Mac.getInstance(HMAC_SHA1_ALGORITHM); mac.init(signingKey);// w w w . j a v a 2s . c o m byte[] rawHmac = mac.doFinal(data.getBytes()); //String result = new String(Base64.encodeBase64(rawHmac)); String result = bytesToHex(rawHmac); return result; }