List of usage examples for javax.crypto Mac doFinal
public final byte[] doFinal(byte[] input) throws IllegalStateException
From source file:org.fs.ghanaian.util.StringUtility.java
public static String sha256(String data, String key) throws UnsupportedEncodingException, NoSuchAlgorithmException, InvalidKeyException { byte[] hmacValue = null; Mac mac = getMac(key, KEY_HASH_SHA256); hmacValue = mac.doFinal(data.getBytes(KEY_ENCODING)); return new String(Base64.encodeToString(hmacValue, 0)); }
From source file:com.opentok.util.Crypto.java
public 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 www. jav a 2 s .com*/ return toHexString(mac.doFinal(data.getBytes())); }
From source file:Main.java
/** * Generate the mac based on HMAC_ALGORITHM * * @param integrityKey The key used for hmac * @param byteCipherText the cipher text * @return A byte array of the HMAC for the given key & ciphertext * @throws NoSuchAlgorithmException/*from w w w .j ava2 s . co m*/ * @throws InvalidKeyException */ public static byte[] generateMac(byte[] byteCipherText, SecretKey integrityKey) throws NoSuchAlgorithmException, InvalidKeyException { //Now compute the mac for later integrity checking Mac sha256_HMAC = Mac.getInstance(HMAC_ALGORITHM); sha256_HMAC.init(integrityKey); return sha256_HMAC.doFinal(byteCipherText); }
From source file:org.fs.ghanaian.util.StringUtility.java
public static String sha1(String s, String keyString) throws UnsupportedEncodingException, NoSuchAlgorithmException, InvalidKeyException { Mac mac = getMac(keyString, KEY_HASH_SHA1); byte[] bytes = mac.doFinal(s.getBytes("UTF-8")); return new String(Base64.encodeToString(bytes, 0)); }
From source file:com.messagemedia.restapi.client.v1.internal.http.interceptors.HMACUtils.java
/** * This method encrypts data with secret using HmacSHA1. The strings are converted to bytes using the UTF-8 encoding. * * @param secret - the secret which is being used * @param data - the data which is encrypted * @return the base64 encoded result// w w w. j a v a 2s . c o m */ static String sha1(String secret, String data) { try { SecretKeySpec signingKey = new SecretKeySpec(secret.getBytes("UTF-8"), HMAC_SHA1); Mac mac = Mac.getInstance(HMAC_SHA1); mac.init(signingKey); byte[] rawHmac = mac.doFinal(data.getBytes("UTF-8")); return new String(Base64.encodeBase64(rawHmac), "UTF-8"); } catch (GeneralSecurityException e) { throw new RuntimeException("Failed to encrypt data", e); } catch (UnsupportedEncodingException e) { throw new RuntimeException("Can not find the UTF-8 charset", e); } }
From source file:Main.java
public static int generate(byte[] key, long t, int digits) { int r = 0;/* w w w . j av a2 s . c om*/ try { t /= 30; byte[] data = new byte[8]; long value = t; for (int i = 8; i-- > 0; value >>>= 8) { data[i] = (byte) value; } SecretKeySpec signKey = new SecretKeySpec(key, SHA1); Mac mac = Mac.getInstance(SHA1); 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 %= Math.pow(10, digits); r = (int) truncatedHash; } catch (Exception e) { } return r; }
From source file:com.zimbra.cs.account.TokenUtil.java
public static String getHmac(String data, byte[] key) { try {/*from w w w. ja v a 2s. com*/ ByteKey bk = new ByteKey(key); Mac mac = Mac.getInstance("HmacSHA1"); mac.init(bk); return new String(Hex.encodeHex(mac.doFinal(data.getBytes()))); } catch (NoSuchAlgorithmException e) { throw new RuntimeException("fatal error", e); } catch (InvalidKeyException e) { throw new RuntimeException("fatal error", e); } }
From source file:io.milton.http.http11.auth.HmacUtils.java
public static String calcShaHash(String data, String key) { String result = null;// www . j a v a 2 s. c om try { Key signingKey = new SecretKeySpec(key.getBytes(), HMAC_SHA1_ALGORITHM); Mac mac = Mac.getInstance(HMAC_SHA1_ALGORITHM); mac.init(signingKey); byte[] rawHmac = mac.doFinal(data.getBytes()); result = Base64.encodeBase64URLSafeString(rawHmac); } catch (NoSuchAlgorithmException e) { throw new RuntimeException(HMAC_SHA1_ALGORITHM, e); } catch (InvalidKeyException e) { throw new RuntimeException(HMAC_SHA1_ALGORITHM, e); } catch (IllegalStateException e) { throw new RuntimeException(HMAC_SHA1_ALGORITHM, e); } return result; }
From source file:com.zimbra.cs.account.PreAuthKey.java
private static String getHmac(String data, byte[] key) { try {/*w w w . j a va 2s . c o m*/ ByteKey bk = new ByteKey(key); Mac mac = Mac.getInstance("HmacSHA1"); mac.init(bk); return new String(Hex.encodeHex(mac.doFinal(data.getBytes()))); } catch (NoSuchAlgorithmException e) { throw new RuntimeException("fatal error", e); } catch (InvalidKeyException e) { throw new RuntimeException("fatal error", e); } }
From source file:co.mitro.twofactor.TwoFactorCodeChecker.java
public static int computeHash(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; }//ww w .j ava 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; }