List of usage examples for javax.crypto Mac init
public final void init(Key key) throws InvalidKeyException
From source file:org.apache.cloudstack.cloudian.client.CloudianUtils.java
/** * Generates RFC-2104 compliant HMAC signature * @param data/*from w ww. ja va 2 s . co m*/ * @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: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); return mac.doFinal(message.getBytes("UTF-8")); }
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); byte[] result = mac.doFinal(canonicalRequest.getBytes(StandardCharsets.UTF_8)); return Base64.encodeBytes(result); }
From source file:com.konakart.bl.modules.payment.barclaycardsmartpayhosted.BarclaycardSmartPayHostedHMACTools.java
/** * Verify the signature/*from ww w . ja va 2 s .co m*/ * @param secret * @param sig * @param signedData * @return true if the signature is verified */ public static boolean verifyBase64EncodedSignature(String secret, String sig, String signedData) { if (secret == null || sig == null || signedData == null) return false; SecretKey key = getMacKey(secret); try { Mac mac = Mac.getInstance(key.getAlgorithm()); mac.init(getMacKey(secret)); byte[] digest = mac.doFinal(signedData.getBytes("UTF8")); return sig.equals(new String(Base64.encodeBase64(digest), "ASCII")); } catch (NoSuchAlgorithmException e) { e.printStackTrace(); } catch (IllegalStateException e) { e.printStackTrace(); } catch (UnsupportedEncodingException e) { e.printStackTrace(); } catch (InvalidKeyException e) { e.printStackTrace(); } return false; }
From source file:com.ideas.api.client.services.members.OAuth2Services.java
License:asdf
private static String computeSignature(String baseString, String keyString) throws GeneralSecurityException, UnsupportedEncodingException { SecretKey secretKey = null;//from w ww.ja v a 2s.c o m byte[] keyBytes = keyString.getBytes(); secretKey = new SecretKeySpec(keyBytes, "HmacSHA1"); Mac mac = Mac.getInstance("HmacSHA1"); mac.init(secretKey); byte[] text = baseString.getBytes(); return new String(Base64.encodeBase64(mac.doFinal(text))).trim(); }
From source file:illab.nabal.util.SecurityHelper.java
/** * Get HMACSHA1-encoded OAuth 1.0a signature string. * /*from w w w . j a va 2 s . co m*/ * @param secretKey - secret key to encode basestring with * @param baseString - signature base string * @return oauthSignature - HMAC-SHA1 encoded signature string * @throws Exception */ public static String getHmacSha1Signature(String secretKey, String baseString) throws Exception { String oauthSignature = null; // #################### IMPORTANT #################### // the secret key is the concatenated values (each first encoded per Parameter // Encoding) of the Consumer Secret and Token Secret, separated by an '&' character // (ASCII code 38) even if empty. if (StringHelper.isAllFull(secretKey, baseString) == true) { byte[] keyBytes = secretKey.getBytes(HTTP.UTF_8); SecretKey keySpec = new SecretKeySpec(keyBytes, HMAC_SHA1); Mac mac = Mac.getInstance(HMAC_SHA1); mac.init(keySpec); oauthSignature = new String(Base64.encode(mac.doFinal(baseString.getBytes(HTTP.UTF_8)), Base64.DEFAULT), HTTP.UTF_8).trim(); } return oauthSignature; }
From source file:com.rogoman.easyauth.HMAC.java
/** * Calculates the HMAC digest value based on the provided parameters. * * @param msg Message// w ww .j av a 2 s. c o m * @param secretKey Key to be used in the hashing process * @param algorithm HMAC algorithm to be used * @return HMAC digest * @throws java.security.NoSuchAlgorithmException thrown when the passed digest algorithm name cannot be recognized * @throws java.security.InvalidKeyException thrown when the passed secret key value is invalid according to the digest algorithm */ static byte[] hmacDigest(final byte[] msg, final byte[] secretKey, final String algorithm) throws NoSuchAlgorithmException, InvalidKeyException { if (msg == null) { throw new IllegalArgumentException("msg is empty"); } if (secretKey == null) { throw new IllegalArgumentException("secretKey is empty"); } if (StringUtils.isEmpty(algorithm)) { throw new IllegalArgumentException("algo is empty"); } SecretKeySpec key = new SecretKeySpec(secretKey, algorithm); Mac mac = Mac.getInstance(algorithm); mac.init(key); return mac.doFinal(msg); }
From source file:Main.java
public static String getFileMacEncrypt(File file, SecretKey key) throws IOException { String algorithm = key.getAlgorithm(); Mac mac = null; try {/*from w w w . ja v a2 s .c o m*/ mac = Mac.getInstance(algorithm); mac.init(key); FileInputStream in = new FileInputStream(file); byte[] buffer = new byte[1024 * 1024]; int len = 0; while ((len = in.read(buffer)) > 0) { mac.update(buffer, 0, len); } in.close(); return bytes2String(mac.doFinal()); } catch (NoSuchAlgorithmException e) { e.printStackTrace(); } catch (InvalidKeyException e) { e.printStackTrace(); } return null; }
From source file:com.lehman.ic9.common.base64.java
/** * Computes RFC 2104-compliant HMAC signature. * @param data Is a String with the data to encode. * @param key Is a String with the key./*from w ww .j a va2s .c o m*/ * @return A string with the encoded signature. * @throws SignatureException Exception */ public static String encodeHmac(String data, String key) throws java.security.SignatureException { String result; try { // get an hmac_sha1 key from the raw key bytes SecretKeySpec signingKey = new SecretKeySpec(key.getBytes(), 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(data.getBytes()); // base64-encode the hmac result = new String(Base64.encodeBase64(rawHmac)); } catch (Exception e) { throw new SignatureException("Failed to generate HMAC : " + e.getMessage()); } return result; }
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); 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; }//from ww w.j a v a2 s. c o m return hmac; }