List of usage examples for javax.crypto Mac doFinal
public final byte[] doFinal(byte[] input) throws IllegalStateException
From source file:com.hubrick.vertx.s3.signature.AWS4SignatureBuilder.java
private static byte[] hmacSha256(final byte[] key, final String value) { try {// w ww . j a v a 2 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:flpitu88.web.backend.psicoweb.config.Jwt.java
/** * Private method to generate a signature from a key * * @param input Data to sign//www .ja v a2 s.c o m * @param key Key used for the signature * @param method Algorithm * * @return Signature * * @throws NoSuchAlgorithmException * @throws UnsupportedEncodingException * @throws InvalidKeyException */ private static byte[] sign(String input, String key, String method) throws NoSuchAlgorithmException, UnsupportedEncodingException, InvalidKeyException { Mac hmac = Mac.getInstance(method); SecretKey secretKey = new SecretKeySpec(key.getBytes(), method); hmac.init(secretKey); return hmac.doFinal(input.getBytes()); }
From source file:Main.java
static byte[] decryptJWE(String jwe, Key privRsaKey) { // Log.d("","decryptJWE"); try {/*from w ww .j av a 2 s.co m*/ // split jwe string StringTokenizer tokens = new StringTokenizer(jwe, "."); int count = tokens.countTokens(); // Log.d("","parts.length: "+count); if (count != 5) return null; String jweProtectedHeader64 = tokens.nextToken(); String jweEncrypted64 = tokens.nextToken(); String jweInitVector64 = tokens.nextToken(); String cryptedBytes64 = tokens.nextToken(); String auth_tag64 = tokens.nextToken(); // decrypt cek using private rsa key byte[] cek = decryptRsaB64(jweEncrypted64, privRsaKey); // check cek result byte array if (cek == null || cek.length == 0 || (cek.length % 2) != 0) return null; int keySize = cek.length / 2; Log.d("", "Decryption AES: " + keySize * 8); // build aes_key and hmac_key byte aes_key[] = new byte[keySize]; byte hmac_key[] = new byte[keySize]; System.arraycopy(cek, 0, hmac_key, 0, keySize); System.arraycopy(cek, keySize, aes_key, 0, keySize); // decode initialization vector byte[] iv_key = decodeB64(jweInitVector64); Log.d("", "hmac_key: " + bytesToHex(hmac_key)); Log.d("", "aes_key: " + bytesToHex(aes_key)); Log.d("", "iv_key: " + bytesToHex(iv_key)); // decrypt content using aes_key and iv_key byte[] cryptedBytes = decodeB64(cryptedBytes64); Cipher decrypt = Cipher.getInstance("AES/CBC/PKCS5Padding", "SC"); decrypt.init(Cipher.DECRYPT_MODE, new SecretKeySpec(aes_key, "AES"), new IvParameterSpec(iv_key)); byte[] decryptedBytes = decrypt.doFinal(cryptedBytes); Log.d("", "decryptedBytes:"); Log.d("", bytesToHex(decryptedBytes)); // validation verification byte[] aad = jweProtectedHeader64.getBytes(); long al = aad.length * 8; // concatenate aad, iv_key, cryptedBytes and al byte[] hmacData = new byte[aad.length + iv_key.length + cryptedBytes.length + 8]; int offset = 0; System.arraycopy(aad, offset, hmacData, 0, aad.length); offset += aad.length; System.arraycopy(iv_key, 0, hmacData, offset, iv_key.length); offset += iv_key.length; System.arraycopy(cryptedBytes, 0, hmacData, offset, cryptedBytes.length); offset += cryptedBytes.length; ByteBuffer buffer = ByteBuffer.allocate(8); buffer.putLong(al); System.arraycopy(buffer.array(), 0, hmacData, offset, 8); // compute hmac Mac hmac = Mac.getInstance("HmacSHA256", "SC"); hmac.init(new SecretKeySpec(hmac_key, "HmacSHA256")); byte[] hmacValue = hmac.doFinal(hmacData); // pick authentication tag byte[] authTag = Arrays.copyOf(hmacValue, 16); // validate authentication tag byte[] authTagRead = decodeB64(auth_tag64); for (int i = 0; i < 16; i++) { if (authTag[i] != authTagRead[i]) { Log.d("", "validation failed"); return decryptedBytes; } } Log.d("", "validation success"); // validation success return decryptedBytes; } catch (Exception e) { e.printStackTrace(); } return null; }
From source file:main.java.com.amazonaws.cognito.devauthsample.Utilities.java
public static String sign(String content, String key) { try {/*from w w w . ja v a2s. c o m*/ byte[] data = content.getBytes(Constants.ENCODING_FORMAT); Mac mac = Mac.getInstance(Constants.SIGNATURE_METHOD); mac.init(new SecretKeySpec(key.getBytes(Constants.ENCODING_FORMAT), Constants.SIGNATURE_METHOD)); char[] signature = Hex.encodeHex(mac.doFinal(data)); return new String(signature); } catch (Exception e) { log.log(Level.SEVERE, "Exception during sign", e); } return null; }
From source file:org.jahia.security.TOTP.java
/** * This method uses the JCE to provide the crypto algorithm. HMAC computes a * Hashed Message Authentication Code with the crypto hash algorithm as a * parameter./* w ww . ja v a 2s .com*/ * * @param crypto * : the crypto algorithm (HmacSHA1, HmacSHA256, HmacSHA512) * @param keyBytes * : the bytes to use for the HMAC key * @param text * : the message or text to be authenticated */ private static byte[] hmac_sha(String crypto, byte[] keyBytes, byte[] text) { try { Mac hmac; hmac = Mac.getInstance(crypto); SecretKeySpec macKey = new SecretKeySpec(keyBytes, "RAW"); hmac.init(macKey); return hmac.doFinal(text); } catch (GeneralSecurityException gse) { throw new UndeclaredThrowableException(gse); } }
From source file:com.chumbok.aauth.otp.TOTP.java
/** * This method uses the JCE to provide the crypto algorithm. HMAC computes a * Hashed Message Authentication Code with the crypto hash algorithm as a * parameter./* w w w .ja v a 2s . co m*/ * * @param crypto * : the crypto algorithm (HmacSHA1, HmacSHA256, HmacSHA512) * @param keyBytes * : the bytes to use for the HMAC key * @param text * : the message or text to be authenticated */ private static byte[] hmac_sha(String crypto, byte[] keyBytes, byte[] text) { try { Mac hmac; hmac = Mac.getInstance(crypto); SecretKeySpec macKey = new SecretKeySpec(keyBytes, "RAW"); hmac.init(macKey); return hmac.doFinal(text); } catch (GeneralSecurityException gse) { throw new UndeclaredThrowableException(gse); } }
From source file:com.cloud.utils.SwiftUtil.java
static String calculateRFC2104HMAC(String data, String key) throws SignatureException, NoSuchAlgorithmException, InvalidKeyException { SecretKeySpec signingKey = new SecretKeySpec(key.getBytes(), HMAC_SHA1_ALGORITHM); Mac mac = Mac.getInstance(HMAC_SHA1_ALGORITHM); mac.init(signingKey);/*from w w w . jav a 2s . c om*/ return toHexString(mac.doFinal(data.getBytes())); }
From source file:org.apache.http.contrib.auth.AWSScheme.java
/** * Computes RFC 2104-compliant HMAC signature. * * @param data// w w w.ja 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 RuntimeException * when signature generation fails */ private static String calculateRFC2104HMAC(final String data, final String key) throws AuthenticationException { 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 return Base64.encodeBase64String(rawHmac); } catch (InvalidKeyException ex) { throw new AuthenticationException("Failed to generate HMAC: " + ex.getMessage(), ex); } catch (NoSuchAlgorithmException ex) { throw new AuthenticationException(HMAC_SHA1_ALGORITHM + " algorithm is not supported", ex); } }
From source file:com.annuletconsulting.homecommand.node.AsyncSend.java
/** * Creates the signature from the timestamp using the sharedKey. This same method will be used * on the server and the results compared to authenticate the request. * //from w w w . j a v a2 s .co m * @param timeStamp * @return */ private static String getSignature(String timeStamp) { if (sharedKey != null) try { byte[] data = timeStamp.getBytes(ENCODING_FORMAT); Mac mac = Mac.getInstance(SIGNATURE_METHOD); mac.init(new SecretKeySpec(sharedKey.getBytes(ENCODING_FORMAT), SIGNATURE_METHOD)); char[] signature = Hex.encodeHex(mac.doFinal(data)); return new String(signature); } catch (Exception exception) { exception.printStackTrace(); } return "Error in getSignature()"; }
From source file:com.grouptuity.venmo.VenmoSDK.java
private static String hash_hmac(String payload, String app_secret, String algorithm) { try {/* w w w .java2s . com*/ Mac mac = Mac.getInstance(algorithm); SecretKeySpec secret = new SecretKeySpec(app_secret.getBytes(), algorithm); mac.init(secret); byte[] digest = mac.doFinal(payload.getBytes()); String enc = new String(digest); return enc; } catch (Exception e) { Log.d("VenmoSDK Error Message Caught", e.getMessage()); return ""; } }