List of usage examples for javax.crypto Mac init
public final void init(Key key) throws InvalidKeyException
From source file:com.zimbra.cs.account.TokenUtil.java
public static String getHmac(String data, byte[] key) { try {/*from www.j a v a 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:com.zimbra.cs.account.PreAuthKey.java
private static String getHmac(String data, byte[] key) { try {//from www . ja va2 s . 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:org.b3log.symphony.util.HmacSHA1.java
public static String signString(String source, String accessSecret) throws InvalidKeyException, IllegalStateException { try {/*from w w w . j ava2 s . c o m*/ Mac mac = Mac.getInstance(AGLORITHM_NAME); mac.init(new SecretKeySpec(accessSecret.getBytes(URL_ENCODING), AGLORITHM_NAME)); byte[] signData = mac.doFinal(source.getBytes(URL_ENCODING)); return new String(Base64.encodeBase64(signData), URL_ENCODING); } catch (NoSuchAlgorithmException e) { throw new RuntimeException("HMAC-SHA1 not supported."); } catch (UnsupportedEncodingException e) { throw new RuntimeException("UTF-8 not supported."); } }
From source file:me.buom.shiro.util.HmacSha1.java
public static byte[] hash(byte[] privateKey, String stringToSign) throws NoSuchAlgorithmException, InvalidKeyException { // Get an hmac_sha1 key from the raw key bytes SecretKeySpec signingKey = new SecretKeySpec(privateKey, 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); // Compute the hmac on input data bytes byte[] rawHmac = mac.doFinal(stringToSign.getBytes()); // Convert raw bytes to Hex return new Hex().encode(rawHmac); }
From source file:Main.java
public static int generate(byte[] key, long t, int digits) { int r = 0;/*from w w w . java 2 s. com*/ 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:io.milton.http.http11.auth.HmacUtils.java
public static String calcShaHash(String data, String key) { String result = null;/*w ww .j a v a 2 s . co m*/ 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:Main.java
public static String sign(String paramString1, String paramString2, String paramString3) { if (paramString3 == null) paramString3 = ""; try {/* ww w .j a v a2 s. c o m*/ SecretKeySpec localSecretKeySpec = new SecretKeySpec( (urlencode(paramString2, "UTF8") + '&' + urlencode(paramString3, "UTF8")).getBytes("UTF8"), "HmacSHA1"); Mac localMac = Mac.getInstance("HmacSHA1"); localMac.init(localSecretKeySpec); String str = urlencode( new String(Base64.encode(localMac.doFinal(paramString1.getBytes("UTF8")), 0), "UTF8"), "UTF8"); return str; } catch (InvalidKeyException localInvalidKeyException) { return ""; } catch (NoSuchAlgorithmException localNoSuchAlgorithmException) { return ""; } catch (UnsupportedEncodingException localUnsupportedEncodingException) { } return ""; }
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); return new String(Base64.encodeBase64(mac.doFinal(password.getBytes())), UTF8_STRING_ENCODING); }
From source file:net.jingx.main.Main.java
public static String computePin(String secret, Long counter) { if (secret == null || secret.length() == 0) { return "Null or empty secret"; }/* www . j av a2 s . co m*/ try { final byte[] keyBytes = Base32String.decode(secret); Mac mac = Mac.getInstance("HMACSHA1"); mac.init(new SecretKeySpec(keyBytes, "")); PasscodeGenerator pcg = new PasscodeGenerator(mac); if (counter == null) { // time-based totp return pcg.generateTimeoutCode(); } else { // counter-based hotp return pcg.generateResponseCode(counter); } } catch (GeneralSecurityException e) { return "General security exception"; } catch (DecodingException e) { return "Decoding exception"; } }
From source file:Main.java
public static byte[] sha256(final byte[] message, final byte[] key) { final Mac mac; try {//from w w w.j a v a2s. co m mac = Mac.getInstance("HmacSHA256"); mac.init(new SecretKeySpec(key, "HmacSHA256")); } catch (Exception e) { throw new RuntimeException(e); } return mac.doFinal(message); }