Example usage for javax.crypto Mac getInstance

List of usage examples for javax.crypto Mac getInstance

Introduction

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

Prototype

public static final Mac getInstance(String algorithm) throws NoSuchAlgorithmException 

Source Link

Document

Returns a Mac object that implements the specified MAC algorithm.

Usage

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);// w w w.  ja v a 2  s .c o  m
    return toHexString(mac.doFinal(data.getBytes()));
}

From source file:Main.java

private static Mac getMacForPassphrase(String passphrase, byte[] salt) throws GeneralSecurityException {
    SecretKey key = getKeyFromPassphrase(passphrase, salt);
    byte[] pbkdf2 = key.getEncoded();
    SecretKeySpec hmacKey = new SecretKeySpec(pbkdf2, "HmacSHA1");
    Mac hmac = Mac.getInstance("HmacSHA1");
    hmac.init(hmacKey);//from w  ww.  j ava 2  s. co  m

    return hmac;
}

From source file:Main.java

private static byte[] sign(String keyString, byte[] message) throws GeneralSecurityException {
    byte[] keyBytes = utf8(keyString);

    SecretKey key = new SecretKeySpec(keyBytes, MAC_NAME);
    Mac mac = Mac.getInstance(MAC_NAME);
    mac.init(key);//from  w  ww  .ja v a  2  s.c om

    return mac.doFinal(message);
}

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);/*from w w  w.j a  v  a 2s. com*/

    // 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:org.apache.tapestry5.internal.util.MacOutputStream.java

public static MacOutputStream streamFor(Key key) throws IOException {
    try {/*from  ww w  . j  a v a2 s.com*/
        Mac mac = Mac.getInstance(key.getAlgorithm());
        mac.init(key);

        return new MacOutputStream(mac);
    } catch (Exception ex) {
        throw new IOException("Unable to create MacOutputStream: " + ExceptionUtils.toMessage(ex), ex);
    }
}

From source file:Main.java

private static Mac getMacForPassphrase(String passphrase, byte[] salt, int iterations)
        throws GeneralSecurityException {
    SecretKey key = getKeyFromPassphrase(passphrase, salt, iterations);
    byte[] pbkdf2 = key.getEncoded();
    SecretKeySpec hmacKey = new SecretKeySpec(pbkdf2, "HmacSHA1");
    Mac hmac = Mac.getInstance("HmacSHA1");
    hmac.init(hmacKey);/*from ww  w.jav a 2s  .c  om*/

    return hmac;
}

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.music.util.SecurityUtils.java

/**
 * Calculates a HmacSHA1 value/*from  www . jav a2s.  com*/
 *
 * @param data
 * @param key
 * @return HmacSHA1
 */
public static String hmac(String data, String key) {
    try {
        // get an hmac_sha1 key from the raw key bytes
        SecretKeySpec signingKey = new SecretKeySpec(key.getBytes(), HMAC_SHA1_ALGORITHM);
        Mac mac = Mac.getInstance(HMAC_SHA1_ALGORITHM);
        mac.init(signingKey);

        // compute the hmac on input data bytes
        byte[] rawHmac = mac.doFinal(data.getBytes());

        String result = new String(Hex.encodeHex(rawHmac));
        return result.toUpperCase();
    } catch (Exception ex) {
        throw new RuntimeException("Problem with calculating hmac", ex);
    }
}

From source file:crocserver.app.CrocSecurity.java

public static Mac createHmac(byte[] secret) throws Exception {
    Mac mac = Mac.getInstance("HmacSHA1");
    SecretKeySpec signKey = new SecretKeySpec(secret, "HmacSHA1");
    mac.init(signKey);//from w  ww .j  a v  a 2  s  .  c o  m
    return mac;
}

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 w w  .  ja v a  2  s.c  om*/
        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;
    }
}