List of usage examples for javax.crypto Mac init
public final void init(Key key) throws InvalidKeyException
From source file:com.threatconnect.sdk.conn.ConnectionUtil.java
public static String getHmacSha256Signature(String signature, String apiSecretKey) { try {//from www. ja v a 2s .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: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); final byte[] publicBytes = sha1Mac.doFinal(customValues.getBytes()); final String publicDigest = encoder.encodeBuffer(publicBytes); return publicDigest.replaceAll("\n", ""); }
From source file:com.hubrick.vertx.s3.signature.AWS4SignatureBuilder.java
private static byte[] hmacSha256(final byte[] key, final String value) { try {/* ww w . j av a2 s .co m*/ final String algorithm = "HmacSHA256"; final Mac mac = Mac.getInstance(algorithm); mac.init(new SecretKeySpec(key, algorithm)); return mac.doFinal(utf8Bytes(value)); } catch (Exception e) { throw Throwables.propagate(e); } }
From source file:Pusher.java
/** * Returns a HMAC/SHA256 representation of the given string * @param data/*ww w.j a va 2s . c o m*/ * @return */ private static String hmacsha256Representation(String data) { try { // Create the HMAC/SHA256 key from application secret final SecretKeySpec signingKey = new SecretKeySpec(pusherApplicationSecret.getBytes(), "HmacSHA256"); // Create the message authentication code (MAC) final Mac mac = Mac.getInstance("HmacSHA256"); mac.init(signingKey); //Process and return data byte[] digest = mac.doFinal(data.getBytes("UTF-8")); digest = mac.doFinal(data.getBytes()); //Convert to string BigInteger bigInteger = new BigInteger(1, digest); return String.format("%0" + (digest.length << 1) + "x", bigInteger); } catch (NoSuchAlgorithmException nsae) { //We should never come here, because GAE has HMac SHA256 throw new RuntimeException("No HMac SHA256 algorithm"); } catch (UnsupportedEncodingException e) { //We should never come here, because UTF-8 should be available throw new RuntimeException("No UTF-8"); } catch (InvalidKeyException e) { throw new RuntimeException("Invalid key exception while converting to HMac SHA256"); } }
From source file:com.monitor.baseservice.utils.XCodeUtil.java
public static String hmacSha1(String value, String key) { try {// w w w.j av a2 s . c o m // Get an hmac_sha1 key from the raw key bytes byte[] keyBytes = key.getBytes(); SecretKeySpec signingKey = new SecretKeySpec(keyBytes, "HmacSHA1"); // Get an hmac_sha1 Mac instance and initialize with the signing key Mac mac = Mac.getInstance("HmacSHA1"); mac.init(signingKey); // Compute the hmac on input data bytes byte[] rawHmac = mac.doFinal(value.getBytes()); return Base64.encodeBase64URLSafeString(rawHmac); } catch (NoSuchAlgorithmException e) { throw new RuntimeException(e); } catch (InvalidKeyException e) { throw new RuntimeException(e); } }
From source file:example.DecrypterException.java
/** * Performs the decryption algorithm./*from w w w . j a v a 2 s .c om*/ * * This method decrypts the ciphertext using the encryption key and verifies * the integrity bits with the integrity key. The encrypted format is: * {initialization_vector (16 bytes)}{ciphertext}{integrity (4 bytes)} * https://developers.google.com/ad-exchange/rtb/response-guide/decrypt- * hyperlocal, * https://developers.google.com/ad-exchange/rtb/response-guide/decrypt * -price and https://support.google.com/adxbuyer/answer/3221407?hl=en have * more details about the encrypted format of hyperlocal, winning price, * IDFA, hashed IDFA and Android Advertiser ID. */ public static byte[] decrypt(byte[] ciphertext, SecretKey encryptionKey, SecretKey integrityKey) throws DecrypterException { try { // Step 1. find the length of initialization vector and clear text. final int plaintext_length = ciphertext.length - INITIALIZATION_VECTOR_SIZE - SIGNATURE_SIZE; if (plaintext_length < 0) { throw new RuntimeException("The plain text length can't be negative."); } System.out.println(Arrays.toString(ciphertext)); System.out.println(byte2hex(ciphertext)); System.out.println(ciphertext.length); System.out.println(plaintext_length); byte[] iv = Arrays.copyOf(ciphertext, INITIALIZATION_VECTOR_SIZE); // Step 2. recover clear text final Mac hmacer = Mac.getInstance("HmacSHA1"); hmacer.init(encryptionKey); final int ciphertext_end = INITIALIZATION_VECTOR_SIZE + plaintext_length; final byte[] plaintext = new byte[plaintext_length]; boolean add_iv_counter_byte = true; for (int ciphertext_begin = INITIALIZATION_VECTOR_SIZE, plaintext_begin = 0; ciphertext_begin < ciphertext_end;) { System.out.println("=====> FOR:"); hmacer.reset(); hmacer.init(encryptionKey); System.out.println("iv: " + byte2hex(iv)); final byte[] pad = hmacer.doFinal(iv); System.out.println("pad: " + byte2hex(pad) + " len(pad): " + pad.length); Base64 encoder = new Base64(); String pad_base64 = new String(encoder.encode(pad)); System.out.println("pad Base64: " + pad_base64); int i = 0; while (i < BLOCK_SIZE && ciphertext_begin != ciphertext_end) { plaintext[plaintext_begin++] = (byte) (ciphertext[ciphertext_begin++] ^ pad[i++]); } if (!add_iv_counter_byte) { final int index = iv.length - 1; add_iv_counter_byte = ++iv[index] == 0; } if (add_iv_counter_byte) { add_iv_counter_byte = false; iv = Arrays.copyOf(iv, iv.length + 1); } } System.out.println("plaintext: " + byte2hex(plaintext)); // Step 3. Compute integrity hash. The input to the HMAC is // clear_text // followed by initialization vector, which is stored in the 1st // section // or ciphertext. hmacer.reset(); hmacer.init(integrityKey); hmacer.update(plaintext); hmacer.update(Arrays.copyOf(ciphertext, INITIALIZATION_VECTOR_SIZE)); final byte[] computedSignature = Arrays.copyOf(hmacer.doFinal(), SIGNATURE_SIZE); final byte[] signature = Arrays.copyOfRange(ciphertext, ciphertext_end, ciphertext_end + SIGNATURE_SIZE); if (!Arrays.equals(signature, computedSignature)) { throw new DecrypterException("Signature mismatch."); } return plaintext; } catch (NoSuchAlgorithmException e) { throw new RuntimeException("HmacSHA1 not supported.", e); } catch (InvalidKeyException e) { throw new RuntimeException("Key is invalid for this purpose.", e); } }
From source file:org.apache.shindig.common.crypto.Crypto.java
/** * HMAC sha1/*from w w w . ja va2 s .c o m*/ * * @param key the key must be at least 8 bytes in length. * @param in byte array to HMAC. * @return the hash * * @throws GeneralSecurityException */ public static byte[] hmacSha1(byte[] key, byte[] in) throws GeneralSecurityException { if (key.length < MIN_HMAC_KEY_LEN) { throw new GeneralSecurityException("HMAC key should be at least " + MIN_HMAC_KEY_LEN + " bytes."); } Mac hmac = Mac.getInstance(HMAC_TYPE); Key hmacKey = new SecretKeySpec(key, HMAC_TYPE); hmac.init(hmacKey); hmac.update(in); return hmac.doFinal(); }
From source file:com.cloud.test.utils.UtilsForTest.java
public static String signRequest(String request, String key) { try {/*from w w w . j a va 2 s .c om*/ Mac mac = Mac.getInstance("HmacSHA1"); SecretKeySpec keySpec = new SecretKeySpec(key.getBytes(), "HmacSHA1"); mac.init(keySpec); mac.update(request.getBytes()); byte[] encryptedBytes = mac.doFinal(); //System.out.println("HmacSHA1 hash: " + encryptedBytes); return Base64.encodeBase64String(encryptedBytes); } catch (Exception ex) { System.out.println("unable to sign request"); ex.printStackTrace(); } return null; }
From source file:org.apache.shindig.common.crypto.Crypto.java
/** * Verifies an HMAC SHA1 hash. Throws if the verification fails. * /* w ww. ja v a 2 s . co m*/ * @param key * @param in * @param expected * @throws GeneralSecurityException */ public static void hmacSha1Verify(byte[] key, byte[] in, byte[] expected) throws GeneralSecurityException { Mac hmac = Mac.getInstance(HMAC_TYPE); Key hmacKey = new SecretKeySpec(key, HMAC_TYPE); hmac.init(hmacKey); hmac.update(in); byte actual[] = hmac.doFinal(); if (actual.length != expected.length) { throw new GeneralSecurityException("HMAC verification failure"); } for (int i = 0; i < actual.length; i++) { if (actual[i] != expected[i]) { throw new GeneralSecurityException("HMAC verification failure"); } } }
From source file:de.anycook.social.facebook.FacebookHandler.java
public static boolean verifySigSHA256(String sig, String payload) { try {/*from w ww . ja v a2 s . c om*/ 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; }