List of usage examples for javax.crypto Mac doFinal
public final byte[] doFinal(byte[] input) throws IllegalStateException
From source file:com.comcast.cmb.common.util.AuthUtil.java
protected static byte[] sign(byte[] data, byte[] key, SigningAlgorithm algorithm) throws AmazonClientException { try {//from w ww . j av a2s . c o m Mac mac = Mac.getInstance(algorithm.toString()); mac.init(new SecretKeySpec(key, algorithm.toString())); return mac.doFinal(data); } catch (Exception e) { throw new AmazonClientException("Unable to calculate a request signature: " + e.getMessage(), e); } }
From source file:net.seleucus.wsp.crypto.FwknopSymmetricCrypto.java
public static String sign(byte[] auth_key, String message, byte hmac_type) throws NoSuchAlgorithmException, InvalidKeyException { // Check if hmac_type is valid if (hmac_type > 4 || hmac_type < 0) throw new IllegalArgumentException("Invalid digest type was specified"); // Create Mac instance Mac hmac; hmac = Mac.getInstance(HMAC_ALGORITHMS[hmac_type]); // Create key SecretKeySpec hmac_key = new SecretKeySpec(auth_key, HMAC_ALGORITHMS[hmac_type]); // Init hmac object hmac.init(hmac_key);/* ww w .jav a2s . c om*/ // Prepare enc_part to calculate HMAC byte[] msg_to_hmac = FWKNOP_ENCRYPTION_HEADER.concat(message).getBytes(); // Calculate HMAC and return return message.concat(Base64.encodeBase64String(hmac.doFinal(msg_to_hmac)).replace("=", "")); }
From source file:com.github.benyzhous.springboot.web.core.gateway.sign.backend.Sign.java
/** * HTTP??// ww w . j a va2 s . c o m * * @param uri HTTPPATH??Query * @param httpMethod HTTP * @param headers HTTP * @param paramsMap HTTPQuery+Form? * @param inputStreamBytes HTTPBodyPOST/PUT????,????paramsMap * @return ?? * @throws Exception */ public static String serviceSign(String uri, String httpMethod, Map<String, String> headers, Map<String, Object> paramsMap, byte[] inputStreamBytes) throws Exception { Map<String, String> headersToSign = buildHeadersToSign(headers); String bodyMd5 = buildBodyMd5(httpMethod, inputStreamBytes); String resourceToSign = buildResource(uri, paramsMap); String stringToSign = buildStringToSign(headersToSign, resourceToSign, httpMethod, bodyMd5); Mac hmacSha256 = Mac.getInstance(HMAC_SHA256); String secret = signSecretMap.get(headers.get( HTTP_HEADER_TO_LOWER_CASE ? CA_PROXY_SIGN_SECRET_KEY.toLowerCase() : CA_PROXY_SIGN_SECRET_KEY)); byte[] keyBytes = secret.getBytes(ENCODING); hmacSha256.init(new SecretKeySpec(keyBytes, 0, keyBytes.length, HMAC_SHA256)); return new String(Base64.encodeBase64(hmacSha256.doFinal(stringToSign.getBytes(ENCODING))), ENCODING); }
From source file:com.comcast.cmb.common.util.AuthUtil.java
public static String generateSignature(URL url, Map<String, String> parameters, String version, String algorithm, String accessSecret) throws Exception { String data = null;//from w ww. j a va 2 s. com if (version.equals("1")) { data = constructV1DataToSign(parameters); } else if (version.equals("2")) { parameters.put("SignatureMethod", algorithm); data = constructV2DataToSign(url, parameters); } else { return null; } Mac mac = Mac.getInstance(algorithm); mac.init(new SecretKeySpec(accessSecret.getBytes("UTF-8"), algorithm)); byte[] bytes = mac.doFinal(data.getBytes("UTF-8")); String signature = new String(Base64.encodeBase64(bytes)); return signature; }
From source file:com.otaupdater.utils.Utils.java
public static String hmac(String str, String key) { try {/*from ww w. j a va 2s.com*/ Mac mac = Mac.getInstance(Config.HMAC_ALGORITHM); String salt = randomSaltString(mac.getMacLength()); mac.init(new SecretKeySpec(key.getBytes(), mac.getAlgorithm())); return byteArrToStr(mac.doFinal((salt + str + salt).getBytes("UTF-8"))) + salt; } catch (Exception e) { e.printStackTrace(); } return null; }
From source file:org.apereo.openlrs.utils.OAuthUtils.java
public static String sign(String secret, Map<String, String> oauthParameters, String algorithm, String method, String url) {/* w w w.ja v a 2s .co m*/ StringBuilder signatureBase = new StringBuilder(OAuthUtils.percentEncode(method)); signatureBase.append("&"); signatureBase.append(OAuthUtils.percentEncode(url)); signatureBase.append("&"); Map<String, String> treeMap = new TreeMap<String, String>(oauthParameters); treeMap.remove("oauth_signature"); treeMap.remove("realm"); boolean first = true; for (Map.Entry<String, String> entry : treeMap.entrySet()) { if (!first) signatureBase.append(OAuthUtils.percentEncode("&")); else first = false; signatureBase.append(OAuthUtils.percentEncode(entry.getKey() + "=" + entry.getValue())); } Mac mac = null; try { SecretKeySpec secretKeySpec = new SecretKeySpec((OAuthUtils.percentEncode(secret) + "&").getBytes(), algorithm); mac = Mac.getInstance(secretKeySpec.getAlgorithm()); mac.init(secretKeySpec); } catch (Exception e) { throw new RuntimeException(e); } if (log.isDebugEnabled()) { log.debug("signatureBaseString: " + signatureBase.toString()); } byte[] bytes = mac.doFinal(signatureBase.toString().getBytes()); byte[] encodedMacBytes = Base64.encodeBase64(bytes); return new String(encodedMacBytes); }
From source file:eap.util.EDcodeUtil.java
private static byte[] hmac(byte[] data, byte[] key, String algorithm) { try {//from ww w . j av a 2 s .c o m SecretKey secretKey = new SecretKeySpec(key, algorithm); Mac mac = Mac.getInstance(secretKey.getAlgorithm(), provider); mac.init(secretKey); return mac.doFinal(data); } catch (NoSuchAlgorithmException e) { throw new IllegalArgumentException("No such algorithm [" + algorithm + "]"); } catch (InvalidKeyException e) { throw new IllegalArgumentException(e.getMessage(), e); } }
From source file:org.cloudfoundry.identity.uaa.authentication.RubyUserTokenTests.java
private static byte[] sign(String username, long validUntil, SecretKey key) { Mac mac; try {//from w ww.j a v a 2 s. c om mac = Mac.getInstance("HMACSHA1"); mac.init(key); } catch (GeneralSecurityException e) { throw new RuntimeException("Failed to create and initialize MAC: ", e); } byte[] bytesToSign = Utf8.encode(username + validUntil); // HexDumpEncoder enc = new HexDumpEncoder(); // System.out.println("Signing bytes: \n" + enc.encode(bytesToSign)); byte[] sig = mac.doFinal(bytesToSign); // System.out.println("Signature is: \n" + enc.encode(sig)); return sig; }
From source file:at.alladin.rmbt.shared.Helperfunctions.java
public static String calculateHMAC(final String secret, final String data) { try {//from w w w .j a v a2 s . c o m final SecretKeySpec signingKey = new SecretKeySpec(secret.getBytes(), "HmacSHA1"); final Mac mac = Mac.getInstance("HmacSHA1"); mac.init(signingKey); final byte[] rawHmac = mac.doFinal(data.getBytes()); final String result = new String(Base64.encodeBytes(rawHmac)); return result; } catch (final GeneralSecurityException e) { System.out.println("Unexpected error while creating hash: " + e.getMessage()); return ""; } }
From source file:com.akamai.edgegrid.auth.EdgeGridV1Signer.java
/** * Helper method to calculate the HMAC signature of a given string. * /*from w ww.j ava 2 s. co m*/ * @param s the string to sign. * @param key the key for the signature. * @param algorithm the signing algorithm. * @return the HMac signature. * @throws RequestSigningException */ private static byte[] sign(String s, byte[] key, String algorithm) throws RequestSigningException { try { SecretKeySpec signingKey = new SecretKeySpec(key, algorithm); Mac mac = Mac.getInstance(algorithm); mac.init(signingKey); byte[] valueBytes = s.getBytes(CHARSET); return mac.doFinal(valueBytes); } catch (NoSuchAlgorithmException nsae) { throw new RequestSigningException("Failed to sign: algorithm not found", nsae); } catch (InvalidKeyException ike) { throw new RequestSigningException("Failed to sign: invalid key", ike); } catch (UnsupportedEncodingException uee) { throw new RequestSigningException("Failed to sign: invalid string encoding", uee); } }