List of usage examples for javax.crypto Mac doFinal
public final byte[] doFinal(byte[] input) throws IllegalStateException
From source file:org.apache.sling.auth.xing.login.XingLoginUtil.java
private static byte[] hmac(final String message, final String secretKey, final String hashAlgorithm) throws NoSuchAlgorithmException, InvalidKeyException, UnsupportedEncodingException { final SecretKeySpec secretKeySpec = new SecretKeySpec(secretKey.getBytes(), hashAlgorithm); final Mac mac = Mac.getInstance(hashAlgorithm); mac.init(secretKeySpec);/*from w w w . j a v a2 s . c om*/ return mac.doFinal(message.getBytes("UTF-8")); }
From source file:Main.java
public static String createHMACWithMD5(String source, String key) throws NoSuchAlgorithmException, InvalidKeyException { Mac hmac = Mac.getInstance("HmacMD5"); SecretKeySpec secretKey = new SecretKeySpec(key.getBytes(), hmac.getAlgorithm()); hmac.init(secretKey);/*ww w . ja va2 s . co m*/ byte[] signature = hmac.doFinal(source.getBytes()); return Base64.encodeToString(signature, Base64.DEFAULT); }
From source file:org.mule.modules.acquialift.client.HmacUtil.java
private static String hmacEncodeRequest(String canonicalRequest, String secretKey) throws Exception { SecretKeySpec keySpec = new SecretKeySpec(secretKey.getBytes(StandardCharsets.UTF_8), HMAC_SHA1_ALGORITHM); Mac mac = Mac.getInstance(HMAC_SHA1_ALGORITHM); mac.init(keySpec);//from w ww. j a v a 2s . c o m byte[] result = mac.doFinal(canonicalRequest.getBytes(StandardCharsets.UTF_8)); return Base64.encodeBytes(result); }
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);/*ww w.j ava2 s. co m*/ // 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:org.apache.cloudstack.cloudian.client.CloudianUtils.java
/** * Generates RFC-2104 compliant HMAC signature * @param data/*from w ww . j av a 2 s. c om*/ * @param key * @return returns the generated signature or null on error */ public static String generateHMACSignature(final String data, final String key) { if (Strings.isNullOrEmpty(data) || Strings.isNullOrEmpty(key)) { return null; } try { final SecretKeySpec signingKey = new SecretKeySpec(key.getBytes(), HMAC_SHA1_ALGORITHM); final Mac mac = Mac.getInstance(HMAC_SHA1_ALGORITHM); mac.init(signingKey); byte[] rawHmac = mac.doFinal(data.getBytes()); return Base64.encodeBase64String(rawHmac); } catch (final Exception e) { LOG.error("Failed to generate HMAC signature from provided data and key, due to: ", e); } return null; }
From source file:cn.ucloud.sdk.utils.EncoderUtils.java
/** * SHA1//from www. java 2s.com * @param key * @param str * @return */ public static String sha1(String key, String str) { String algorithm = "HmacSHA1"; String result = null; try { SecretKeySpec keySpec = new SecretKeySpec(key.getBytes("UTF-8"), algorithm); Mac mac = Mac.getInstance(algorithm); mac.init(keySpec); result = getFormattedText(mac.doFinal(str.getBytes("UTF-8"))); } catch (Exception e) { LogUtils.exception(logger, e); } return result; }
From source file:Main.java
public static String encodeHmac(String key, String data) throws Exception { Mac sha256_HMAC = Mac.getInstance("HmacSHA256"); SecretKeySpec secret_key = new SecretKeySpec(key.getBytes(), "HmacSHA256"); sha256_HMAC.init(secret_key);//from ww w.j a v a 2s. c o m return bytesToHex(sha256_HMAC.doFinal(data.getBytes())); }
From source file:Main.java
public static byte[] generateCramMD5ClientResponse(String userName, String userPassword, byte[] challengeBytes) throws Exception { String macAlgorithm = "HmacMD5"; Mac mac = Mac.getInstance(macAlgorithm); mac.init(new SecretKeySpec(userPassword.getBytes("UTF-8"), macAlgorithm)); final byte[] messageAuthenticationCode = mac.doFinal(challengeBytes); String responseAsString = userName + " " + toHex(messageAuthenticationCode); return responseAsString.getBytes(); }
From source file:Main.java
private static String computeSignature(String baseString, String keyString) throws GeneralSecurityException, UnsupportedEncodingException { Mac mac = Mac.getInstance("HmacSHA256"); SecretKeySpec secret = new SecretKeySpec(keyString.getBytes("UTF-8"), "HmacSHA256"); mac.init(secret);//from ww w . j av a2 s .c om byte[] byteData = mac.doFinal(baseString.getBytes("UTF-8")); BigInteger hash = new BigInteger(1, byteData); String hmac = hash.toString(16); if (hmac.length() % 2 != 0) { hmac = "0" + hmac; } return hmac; }
From source file:com.nlworks.wowapi.util.ConnectionManager.java
private static String generateSignature(String key, String data) throws GeneralSecurityException, IOException { byte[] hmacData = null; SecretKeySpec secretKey = new SecretKeySpec(key.getBytes("UTF-8"), "HmacSHA1"); Mac mac = Mac.getInstance("HmacSHA1"); mac.init(secretKey);/*from w w w .j a v a 2 s. c o m*/ hmacData = mac.doFinal(data.getBytes("UTF-8")); return Base64.encodeBase64String(hmacData); }