List of usage examples for javax.crypto Mac init
public final void init(Key key) throws InvalidKeyException
From source file:Main.java
static byte[] encodeHMAC(String key, byte[] message) throws NoSuchAlgorithmException, InvalidKeyException { SecretKeySpec keySpec = new SecretKeySpec(key.getBytes(), "RAW"); Mac mac = Mac.getInstance("HmacSHA1"); mac.init(keySpec); return mac.doFinal(message); }
From source file:Main.java
public static byte[] hmacSha1(String key, String value) throws UnsupportedEncodingException, NoSuchAlgorithmException, InvalidKeyException { String type = "HmacSHA1"; SecretKeySpec secret = new SecretKeySpec(key.getBytes(), type); Mac mac = Mac.getInstance(type); mac.init(secret); return mac.doFinal(value.getBytes()); }
From source file:Main.java
public static String doSignWithSHA1(String toSign, String keyString) throws Exception { SecretKeySpec key = new SecretKeySpec((keyString).getBytes(UTF_8), HMAC_SHA1); Mac mac = Mac.getInstance(HMAC_SHA1); mac.init(key); byte[] bytes = mac.doFinal(toSign.getBytes(UTF_8)); return Base64.encodeToString(bytes, Base64.CRLF).replace(CARRIAGE_RETURN, EMPTY_STRING); }
From source file:com.music.util.SecurityUtils.java
/** * Calculates a HmacSHA1 value//w ww . j ava 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:com.centurylink.mdw.util.HmacSha1Signature.java
public static String getHMACHexdigestSignature(byte[] payloadBytes, String key) throws InvalidKeyException, NoSuchAlgorithmException { SecretKeySpec keySpec = new SecretKeySpec(key.getBytes(), ALGORITHM); Mac mac = Mac.getInstance(ALGORITHM); mac.init(keySpec); byte[] rawHmac = mac.doFinal(payloadBytes); return Hex.encodeHexString(rawHmac); }
From source file:Main.java
public static String computeHmacSha1(String value, String keyString) throws InvalidKeyException, IllegalStateException, UnsupportedEncodingException, NoSuchAlgorithmException { SecretKeySpec key = new SecretKeySpec((keyString).getBytes("UTF-8"), "HmacSHA1"); Mac mac = Mac.getInstance("HmacSHA1"); mac.init(key); byte[] bytes = mac.doFinal(value.getBytes("UTF-8")); return convToHex(bytes); }
From source file:Main.java
/** * This method generates a byte array corresponding to the * signed secret using value a signing operator * //w w w . ja v a 2s . co m * @param value the value used to sign the key * @param secretKey the key to sign * @return a byte array corresponding to the raw HMAC-SHA1 hash */ public static byte[] hmacSha1(byte[] value, byte[] secretKey) throws RuntimeException { try { SecretKeySpec signingKey = new SecretKeySpec(secretKey, HMAC_ALGORITHM); Mac mac = Mac.getInstance(HMAC_ALGORITHM); mac.init(signingKey); byte[] rawHmac = mac.doFinal(value); return (rawHmac); } catch (Exception e) { throw new RuntimeException(e); } }
From source file:Main.java
private static Mac getMacForPassphrase(String passphrase, byte[] salt) throws GeneralSecurityException { SecretKey key = getKeyFromPassphrase(passphrase, salt); byte[] pbkdf2 = key.getEncoded(); SecretKeySpec hmacKey = new SecretKeySpec(pbkdf2, "HmacSHA1"); Mac hmac = Mac.getInstance("HmacSHA1"); hmac.init(hmacKey); return hmac;//from w w w . jav a 2 s .com }
From source file:Main.java
private static byte[] sign(String keyString, byte[] message) throws GeneralSecurityException { byte[] keyBytes = utf8(keyString); SecretKey key = new SecretKeySpec(keyBytes, MAC_NAME); Mac mac = Mac.getInstance(MAC_NAME); mac.init(key); return mac.doFinal(message); }
From source file:Main.java
private static Mac getMacForPassphrase(String passphrase, byte[] salt, int iterations) throws GeneralSecurityException { SecretKey key = getKeyFromPassphrase(passphrase, salt, iterations); byte[] pbkdf2 = key.getEncoded(); SecretKeySpec hmacKey = new SecretKeySpec(pbkdf2, "HmacSHA1"); Mac hmac = Mac.getInstance("HmacSHA1"); hmac.init(hmacKey); return hmac;/*from w w w . ja v a2s.c o m*/ }