List of usage examples for javax.crypto Mac doFinal
public final byte[] doFinal(byte[] input) throws IllegalStateException
From source file:com.the_incognito.darry.incognitochatmessengertest.BouncyCastleImplementation.java
public static String hmacSha256(String key, String value) { try {//from www .j av a 2s.c o m //System.out.println(Base64.getEncoder().encodeToString(keyBytes)); // Get an hmac_sha256 key from the raw key bytes System.out.println("First HMAC on = " + value); SecretKeyFactory factory = SecretKeyFactory.getInstance("PBKDF2WithHmacSHA256", new BouncyCastleProvider()); char password[] = key.toCharArray(); byte salt[] = "salt".getBytes(); KeySpec spec = new PBEKeySpec(password, salt, 65536, 256); SecretKey tmp = factory.generateSecret(spec); SecretKey secret = new SecretKeySpec(tmp.getEncoded(), "HmacSHA256"); // Get an hmac_sha256 Mac instance and initialize with the signing key Mac mac = Mac.getInstance("HmacSHA256", new BouncyCastleProvider()); mac.init(secret); // Compute the hmac on input data bytes byte[] rawHmac = mac.doFinal(value.getBytes()); // Convert raw bytes to Hex byte[] hexBytes = new Hex().encode(rawHmac); // Covert array of Hex bytes to a String return new String(hexBytes, "UTF-8"); } catch (Exception e) { throw new RuntimeException(e); } }
From source file:org.apache.hadoop.mapred.PriorityAuthorization.java
/** * Adapted from AWS Query Authentication cookbook: * Computes RFC 2104-compliant HMAC signature. * * @param data//from w w w . j a v a 2 s .c o m * The data to be signed. * @param key * The signing key. * @return * The base64-encoded RFC 2104-compliant HMAC signature. * @throws * java.security.SignatureException when signature generation fails */ public static String hmac(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, e); } return result; }
From source file:kcb.billerengine.utils.Utils.java
public static String computeToken(String key, String data) { String token = null;/*from w w w .j a v a2 s . c o m*/ try { SecretKey secretKey = null; byte[] keyBytes = key.getBytes("UTF-8"); Mac mac = Mac.getInstance("HMACSHA256"); secretKey = new SecretKeySpec(keyBytes, mac.getAlgorithm()); mac.init(secretKey); byte[] text = data.getBytes("UTF-8"); byte[] encodedText = mac.doFinal(text); token = new String(Base64.encodeBase64(encodedText)).trim(); } catch (UnsupportedEncodingException | InvalidKeyException | NoSuchAlgorithmException ex) { LOG.error("ComputeToken error : " + ex); } return token; }
From source file:org.apache.hadoop.security.token.SecretManager.java
/** * Compute HMAC of the identifier using the secret key and return the * output as password// www . j av a 2 s . com * @param identifier the bytes of the identifier * @param key the secret key * @return the bytes of the generated password */ protected static byte[] createPassword(byte[] identifier, SecretKey key) { Mac mac = threadLocalMac.get(); try { mac.init(key); } catch (InvalidKeyException ike) { throw new IllegalArgumentException("Invalid key to HMAC computation", ike); } return mac.doFinal(identifier); }
From source file:za.co.bronkode.jwtbroker.Tokenizer.java
public static String GetSignature(String header, String claim) { try {/* ww w. j a va 2 s.co m*/ StringBuilder sb = new StringBuilder(header); sb.append("."); sb.append(claim); Mac mac = Mac.getInstance("HmacSHA256"); SecretKey key = new SecretKeySpec(privateKey.getBytes(), "HmacSHA256"); mac.init(key); String signature = Base64.getEncoder().encodeToString(mac.doFinal(sb.toString().getBytes())); return signature; } catch (NoSuchAlgorithmException | InvalidKeyException ex) { Logger.getLogger(Tokenizer.class.getName()).log(Level.SEVERE, null, ex); } return ""; }
From source file:com.threatconnect.sdk.conn.ConnectionUtil.java
public static String getHmacSha256Signature(String signature, String apiSecretKey) { try {// w ww . ja v a2s .c o m String calculatedSignature; SecretKeySpec spec = new SecretKeySpec(apiSecretKey.getBytes(), "HmacSHA256"); Mac mac = Mac.getInstance("HmacSHA256"); mac.init(spec); byte[] rawSignature = mac.doFinal(signature.getBytes()); calculatedSignature = Base64.encodeBase64String(rawSignature); return calculatedSignature; } catch (NoSuchAlgorithmException | InvalidKeyException | IllegalStateException ex) { logger.error("Error creating HMAC SHA256 signature", ex); return null; } }
From source file:com.amazon.pay.impl.Util.java
/** * Helper method to calculate base64 encoded signature using specified secret key * */// ww w . j av a2 s .co m public static String getSignature(String stringToSign, String secretKey) throws IllegalStateException, InvalidKeyException, NoSuchAlgorithmException, UnsupportedEncodingException { Mac mac = Mac.getInstance("HmacSHA256"); mac.init(new SecretKeySpec(secretKey.getBytes("UTF-8"), "HmacSHA256")); byte[] signature = mac.doFinal(stringToSign.getBytes("UTF-8")); String signatureBase64 = new String(Base64.encodeBase64(signature), "UTF-8"); return signatureBase64; }
From source file:no.digipost.android.authentication.OAuth.java
private static String encryptHmacSHA256(final String data) { SecretKeySpec secretKey = new SecretKeySpec(Secret.CLIENT_SECRET.getBytes(), ApplicationConstants.HMACSHA256); Mac mac = null; try {/*from w w w .j a va 2 s . c o m*/ mac = Mac.getInstance(ApplicationConstants.HMACSHA256); mac.init(secretKey); } catch (Exception e) { // Ignore } byte[] hmacData = mac.doFinal(data.getBytes()); return new String(hmacData); }
From source file:de.hybris.platform.integration.cis.payment.strategies.impl.CisPaymentIntegrationTestHelper.java
public static String getPublicDigest(final String customValues) throws Exception { final String pub = getSharedSecret(); final BASE64Encoder encoder = new BASE64Encoder(); final Mac sha1Mac = Mac.getInstance("HmacSHA1"); final SecretKeySpec publicKeySpec = new SecretKeySpec(pub.getBytes(), "HmacSHA1"); sha1Mac.init(publicKeySpec);//from w w w . ja va 2s . c o m final byte[] publicBytes = sha1Mac.doFinal(customValues.getBytes()); final String publicDigest = encoder.encodeBuffer(publicBytes); return publicDigest.replaceAll("\n", ""); }
From source file:de.anycook.social.facebook.FacebookHandler.java
public static boolean verifySigSHA256(String sig, String payload) { try {//www . j a v a 2s.co m Mac mac = Mac.getInstance("HmacSHA256"); SecretKeySpec secret = new SecretKeySpec(APP_SECRET.getBytes(), "HmacSHA256"); mac.init(secret); byte[] digest = mac.doFinal(payload.getBytes()); String expected_sig = new String(digest); if (sig.equals(expected_sig)) { return true; } } catch (NoSuchAlgorithmException | InvalidKeyException e) { LOGGER.error(e, e); } LOGGER.error("signatures are not the same!"); return false; }