Example usage for javax.crypto Mac init

List of usage examples for javax.crypto Mac init

Introduction

In this page you can find the example usage for javax.crypto Mac init.

Prototype

public final void init(Key key) throws InvalidKeyException 

Source Link

Document

Initializes this Mac object with the given key.

Usage

From source file:Main.java

private static byte[] hmacSha1(byte[] value, byte[] key) {
    try {//  w  w w.  ja  va  2s  .  c o m
        SecretKeySpec signingKey = new SecretKeySpec(key, "HmacSHA1");
        Mac mac = Mac.getInstance("HmacSHA1");
        mac.init(signingKey);
        return mac.doFinal(value);
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
}

From source file:Main.java

public static byte[] generateCramMD5ClientResponse(String userName, String userPassword, byte[] challengeBytes)
        throws Exception {
    String macAlgorithm = "HmacMD5";
    Mac mac = Mac.getInstance(macAlgorithm);
    mac.init(new SecretKeySpec(userPassword.getBytes("UTF-8"), macAlgorithm));
    final byte[] messageAuthenticationCode = mac.doFinal(challengeBytes);
    String responseAsString = userName + " " + toHex(messageAuthenticationCode);
    return responseAsString.getBytes();
}

From source file:com.amazonaws.sqs.util.AwsSignature.java

public static String calculate(String stringToSign, String secretKey) throws Exception {
    // get an hmac_sha1 key from the raw key bytes
    SecretKeySpec signingKey = new SecretKeySpec(secretKey.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(stringToSign.getBytes());

    // base64-encode the hmac
    String result = new String(Base64.encodeBase64(rawHmac));
    return result;
}

From source file:Main.java

/**
 *
 * @param key//from  ww  w  .j ava  2 s . c  o  m
 * @param algo
 * @return
 * @throws UnsupportedEncodingException
 * @throws NoSuchAlgorithmException
 * @throws InvalidKeyException
 */
static Mac getMac(String key, String algo)
        throws UnsupportedEncodingException, NoSuchAlgorithmException, InvalidKeyException {
    SecretKeySpec secretKeySpec = new SecretKeySpec(key.getBytes(KEY_ENCODING), algo);
    Mac mac = Mac.getInstance(algo);
    mac.init(secretKeySpec);
    return mac;
}

From source file:Main.java

public static byte[] generateCramMD5HexClientResponse(String userName, String userPassword,
        byte[] challengeBytes) throws Exception {
    String macAlgorithm = "HmacMD5";
    byte[] digestedPasswordBytes = MessageDigest.getInstance("MD5").digest(userPassword.getBytes("UTF-8"));
    byte[] hexEncodedDigestedPasswordBytes = toHex(digestedPasswordBytes).getBytes("UTF-8");
    Mac mac = Mac.getInstance(macAlgorithm);
    mac.init(new SecretKeySpec(hexEncodedDigestedPasswordBytes, macAlgorithm));
    final byte[] messageAuthenticationCode = mac.doFinal(challengeBytes);
    String responseAsString = userName + " " + toHex(messageAuthenticationCode);
    return responseAsString.getBytes();
}

From source file:com.opentok.util.Crypto.java

public static String signData(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);
    return toHexString(mac.doFinal(data.getBytes()));
}

From source file:Main.java

private static byte[] hmacTemplate(byte[] data, byte[] key, String algorithm) {
    if (data == null || data.length == 0 || key == null || key.length == 0)
        return null;
    try {//w ww  .j  a va2 s  . com
        SecretKeySpec secretKey = new SecretKeySpec(key, algorithm);
        Mac mac = Mac.getInstance(algorithm);
        mac.init(secretKey);
        return mac.doFinal(data);
    } catch (InvalidKeyException | NoSuchAlgorithmException e) {
        e.printStackTrace();
        return null;
    }
}

From source file:com.messagemedia.restapi.client.v1.internal.http.interceptors.HMACUtils.java

/**
 * This method encrypts data with secret using HmacSHA1. The strings are converted to bytes using the UTF-8 encoding.
 *
 * @param secret - the secret which is being used
 * @param data   - the data which is encrypted
 * @return the base64 encoded result//  w w  w.  j a va 2 s.  co  m
 */
static String sha1(String secret, String data) {
    try {
        SecretKeySpec signingKey = new SecretKeySpec(secret.getBytes("UTF-8"), HMAC_SHA1);
        Mac mac = Mac.getInstance(HMAC_SHA1);
        mac.init(signingKey);
        byte[] rawHmac = mac.doFinal(data.getBytes("UTF-8"));
        return new String(Base64.encodeBase64(rawHmac), "UTF-8");
    } catch (GeneralSecurityException e) {
        throw new RuntimeException("Failed to encrypt data", e);
    } catch (UnsupportedEncodingException e) {
        throw new RuntimeException("Can not find the UTF-8 charset", e);
    }
}

From source file:Main.java

private static byte[] encryptHMAC(String data, String secret) throws IOException {
    byte[] bytes = null;
    try {/*from   w w w  .j  a  v a 2s  . c  o  m*/
        SecretKey secretKey = new SecretKeySpec(secret.getBytes("UTF-8"), "HmacMD5");
        Mac mac = Mac.getInstance(secretKey.getAlgorithm());
        mac.init(secretKey);
        bytes = mac.doFinal(data.getBytes("UTF-8"));
    } catch (GeneralSecurityException gse) {
        String msg = getStringFromException(gse);
        throw new IOException(msg);
    }
    return bytes;
}

From source file:Main.java

/**
 * Generate the mac based on HMAC_ALGORITHM
 *
 * @param integrityKey The key used for hmac
 * @param byteCipherText the cipher text
 * @return A byte array of the HMAC for the given key & ciphertext
 * @throws NoSuchAlgorithmException/*from  w ww.j  a v  a 2  s. co  m*/
 * @throws InvalidKeyException
 */
public static byte[] generateMac(byte[] byteCipherText, SecretKey integrityKey)
        throws NoSuchAlgorithmException, InvalidKeyException {
    //Now compute the mac for later integrity checking
    Mac sha256_HMAC = Mac.getInstance(HMAC_ALGORITHM);
    sha256_HMAC.init(integrityKey);
    return sha256_HMAC.doFinal(byteCipherText);
}