List of usage examples for javax.crypto Mac doFinal
public final byte[] doFinal(byte[] input) throws IllegalStateException
From source file:com.music.util.SecurityUtils.java
/** * Calculates a HmacSHA1 value//from ww w. java 2s .c o m * * @param data * @param key * @return HmacSHA1 */ public static String hmac(String data, String key) { try { // get an hmac_sha1 key from the raw key bytes SecretKeySpec signingKey = new SecretKeySpec(key.getBytes(), HMAC_SHA1_ALGORITHM); Mac mac = Mac.getInstance(HMAC_SHA1_ALGORITHM); mac.init(signingKey); // compute the hmac on input data bytes byte[] rawHmac = mac.doFinal(data.getBytes()); String result = new String(Hex.encodeHex(rawHmac)); return result.toUpperCase(); } catch (Exception ex) { throw new RuntimeException("Problem with calculating hmac", ex); } }
From source file:crocserver.app.CrocSecurity.java
public static String createCode(byte[] secret, String string, long value) throws Exception { Mac mac = createHmac(secret); mac.update(string.getBytes());//from ww w .j av a 2s .c o m return new Base32().encodeAsString(mac.doFinal(toByteArray(value))); }
From source file:com.cloud.bridge.util.S3SoapAuth.java
/** * Create a signature by the following method: * new String( Base64( SHA1( key, byte array ))) * // w w w . j a v a2s . com * @param signIt - the data to generate a keyed HMAC over * @param secretKey - the user's unique key for the HMAC operation * @return String - the recalculated string */ private static String calculateRFC2104HMAC(String signIt, String secretKey) { String result = null; try { SecretKeySpec key = new SecretKeySpec(secretKey.getBytes(), "HmacSHA1"); Mac hmacSha1 = Mac.getInstance("HmacSHA1"); hmacSha1.init(key); byte[] rawHmac = hmacSha1.doFinal(signIt.getBytes()); result = new String(Base64.encodeBase64(rawHmac)); } catch (Exception e) { logger.error("Failed to generate keyed HMAC on soap request: " + e.getMessage()); return null; } return result.trim(); }
From source file:com.janrain.backplane.common.HmacHashUtils.java
private static String hmacSign(SecretKey key, String password) throws NoSuchAlgorithmException, InvalidKeyException, UnsupportedEncodingException { Mac mac = Mac.getInstance(key.getAlgorithm()); mac.init(key);//from w w w . j av a 2 s . c o m return new String(Base64.encodeBase64(mac.doFinal(password.getBytes())), UTF8_STRING_ENCODING); }
From source file:com.amazonaws.sqs.util.AwsSignature.java
public static String calculate(String stringToSign, String secretKey) throws Exception { // get an hmac_sha1 key from the raw key bytes SecretKeySpec signingKey = new SecretKeySpec(secretKey.getBytes(), HMAC_SHA1_ALGORITHM); // get an hmac_sha1 Mac instance and initialize with the signing key Mac mac = Mac.getInstance(HMAC_SHA1_ALGORITHM); mac.init(signingKey);/*from w ww .ja va 2 s .c om*/ // compute the hmac on input data bytes byte[] rawHmac = mac.doFinal(stringToSign.getBytes()); // base64-encode the hmac String result = new String(Base64.encodeBase64(rawHmac)); return result; }
From source file:controllers.DiscourseAuth.java
private static String checksum(SecretKeySpec key, String macData) throws NoSuchAlgorithmException, UnsupportedEncodingException, InvalidKeyException { Mac mac = Mac.getInstance("HmacSHA256"); mac.init(key);//from ww w .ja va2s . c o m byte[] doFinal = mac.doFinal(macData.getBytes(StandardCharsets.UTF_8)); return Hex.encodeHexString(doFinal); }
From source file:com.opentok.test.Helpers.java
private static String signData(String data, String key) throws SignatureException, NoSuchAlgorithmException, InvalidKeyException { SecretKeySpec signingKey = new SecretKeySpec(key.getBytes(), HMAC_SHA1_ALGORITHM); Mac mac = Mac.getInstance(HMAC_SHA1_ALGORITHM); mac.init(signingKey);/*from w w w . ja v a2 s . c o m*/ return toHexString(mac.doFinal(data.getBytes())); }
From source file:name.martingeisse.common.util.HmacUtil.java
/** * Base function to generate a HMAC.//from w w w . j a v a2s .c o m * * @param payload the payload data * @param secret the secret to sign the HMAC * @param algorithm the HMAC algorithm to use * @return the HMAC */ public static byte[] generateHmac(byte[] payload, byte[] secret, String algorithm) { try { final Mac mac = Mac.getInstance(algorithm); mac.init(new SecretKeySpec(secret, algorithm)); return mac.doFinal(payload); } catch (NoSuchAlgorithmException e) { throw new RuntimeException(e); } catch (InvalidKeyException e) { throw new RuntimeException(e); } }
From source file:com.rogoman.easyauth.HMAC.java
/** * Calculates the HMAC digest value based on the provided parameters. * * @param msg Message//from ww w. java2s . c o m * @param secretKey Key to be used in the hashing process * @param algorithm HMAC algorithm to be used * @return HMAC digest * @throws java.security.NoSuchAlgorithmException thrown when the passed digest algorithm name cannot be recognized * @throws java.security.InvalidKeyException thrown when the passed secret key value is invalid according to the digest algorithm */ static byte[] hmacDigest(final byte[] msg, final byte[] secretKey, final String algorithm) throws NoSuchAlgorithmException, InvalidKeyException { if (msg == null) { throw new IllegalArgumentException("msg is empty"); } if (secretKey == null) { throw new IllegalArgumentException("secretKey is empty"); } if (StringUtils.isEmpty(algorithm)) { throw new IllegalArgumentException("algo is empty"); } SecretKeySpec key = new SecretKeySpec(secretKey, algorithm); Mac mac = Mac.getInstance(algorithm); mac.init(key); return mac.doFinal(msg); }
From source file:org.sentilo.common.rest.hmac.HMACBuilder.java
private static String calculateHMAC(final String secret, final String data) throws GeneralSecurityException { final SecretKeySpec signingKey = new SecretKeySpec(secret.getBytes(), MAC_ALGORITHM); final Mac mac = Mac.getInstance(MAC_ALGORITHM); mac.init(signingKey);//from ww w .j a va2 s . c om final byte[] rawHmac = mac.doFinal(data.getBytes()); return new String(Base64.encodeBase64(rawHmac)); }