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.brienwheeler.lib.security.HmacSha256.java

public static String base64HmacSha256(String secretKey, String signData) {
    try {/* ww w.  j a  v  a2  s  .  co m*/
        Mac hmacSha256 = Mac.getInstance("HmacSHA256");
        SecretKeySpec secretKeySpec = new SecretKeySpec(secretKey.getBytes(), "HmacSHA256");
        hmacSha256.init(secretKeySpec);
        return Base64.encodeBase64String(hmacSha256.doFinal(signData.getBytes()));
    } catch (Exception e) {
        throw new CryptoException(e);
    }
}

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  .jav  a 2 s  .c o 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:org.b3log.symphony.util.HmacSHA1.java

public static String signString(String source, String accessSecret)
        throws InvalidKeyException, IllegalStateException {
    try {// w w  w.  j a v a  2s . co  m
        Mac mac = Mac.getInstance(AGLORITHM_NAME);
        mac.init(new SecretKeySpec(accessSecret.getBytes(URL_ENCODING), AGLORITHM_NAME));
        byte[] signData = mac.doFinal(source.getBytes(URL_ENCODING));

        return new String(Base64.encodeBase64(signData), URL_ENCODING);
    } catch (NoSuchAlgorithmException e) {
        throw new RuntimeException("HMAC-SHA1 not supported.");
    } catch (UnsupportedEncodingException e) {
        throw new RuntimeException("UTF-8 not supported.");
    }
}

From source file:me.buom.shiro.util.HmacSha1.java

public static byte[] hash(byte[] privateKey, String stringToSign)
        throws NoSuchAlgorithmException, InvalidKeyException {
    // Get an hmac_sha1 key from the raw key bytes
    SecretKeySpec signingKey = new SecretKeySpec(privateKey, 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);//  w w  w .  jav a  2s .co  m

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

    // Convert raw bytes to Hex
    return new Hex().encode(rawHmac);
}

From source file:Main.java

/**
 * Compute the HMAC with SHA-256 of data, as defined in
 * http://tools.ietf.org/html/rfc2104#section-2 .
 * @param key The key byte array./*w  ww  .  j a v  a  2s.  co  m*/
 * @param data The input byte buffer. This does not change the position.
 * @return The HMAC result.
 */
public static byte[] computeHmacWithSha256(byte[] key, ByteBuffer data) {
    final String algorithm = "HmacSHA256";
    Mac mac;
    try {
        mac = Mac.getInstance(algorithm);
    } catch (NoSuchAlgorithmException ex) {
        // Don't expect this to happen.
        throw new Error("computeHmac: " + algorithm + " is not supported: " + ex.getMessage());
    }

    try {
        mac.init(new SecretKeySpec(key, algorithm));
    } catch (InvalidKeyException ex) {
        // Don't expect this to happen.
        throw new Error("computeHmac: Can't init " + algorithm + " with key: " + ex.getMessage());
    }
    int savePosition = data.position();
    mac.update(data);
    data.position(savePosition);
    return mac.doFinal();
}

From source file:org.b3log.latke.util.Crypts.java

/**
 * Signs the specified source string using the specified secret.
 *
 * @param source the specified source string
 * @param secret the specified secret/*from   w  w w . j  a v a2s  . com*/
 * @return signed string
 */
public static String signHmacSHA1(final String source, final String secret) {
    try {
        final Mac mac = Mac.getInstance("HmacSHA1");
        mac.init(new SecretKeySpec(secret.getBytes("UTF-8"), "HmacSHA1"));
        final byte[] signData = mac.doFinal(source.getBytes("UTF-8"));

        return new String(Base64.encodeBase64(signData), "UTF-8");
    } catch (final Exception e) {
        throw new RuntimeException("HMAC-SHA1 sign failed", e);
    }
}

From source file:com.ai.smart.bottom.helper.MacUtils.java

public static String hmacsha256(String secret, String data) {
    Mac mac = null;/*from  w ww. j  av a  2 s . c  o  m*/
    byte[] doFinal = null;
    try {
        mac = Mac.getInstance(HMAC_ALGORITHM);
        //??MD5
        byte[] dataBytes = DigestUtils.md5(data);
        //sourcekeyMD5,
        SecretKey secretkey = new SecretKeySpec(DigestUtils.md5(secret), HMAC_ALGORITHM);
        mac.init(secretkey);
        //HmacSHA256
        doFinal = mac.doFinal(dataBytes);
    } catch (NoSuchAlgorithmException e) {
        throw new RuntimeException(e);
    } catch (InvalidKeyException e) {

    }
    String checksum = Hex.encodeHexString(doFinal).toLowerCase();
    return checksum;
}

From source file:com.miyue.util.Cryptos.java

/**
 * HMAC-SHA1???, ,20./*from w ww .ja v a  2  s  .  com*/
 * 
 * @param input 
 * @param key HMAC-SHA1
 */
public static byte[] hmacSha1(byte[] input, byte[] key) {
    try {
        SecretKey secretKey = new SecretKeySpec(key, HMACSHA1);
        Mac mac = Mac.getInstance(HMACSHA1);
        mac.init(secretKey);
        return mac.doFinal(input);
    } catch (GeneralSecurityException e) {
        throw Exceptions.unchecked(e);
    }
}

From source file:com.zimbra.cs.account.TokenUtil.java

public static String getHmac(String data, byte[] key) {
    try {//from   ww  w  . ja v a  2s  . c  om
        ByteKey bk = new ByteKey(key);
        Mac mac = Mac.getInstance("HmacSHA1");
        mac.init(bk);
        return new String(Hex.encodeHex(mac.doFinal(data.getBytes())));
    } catch (NoSuchAlgorithmException e) {
        throw new RuntimeException("fatal error", e);
    } catch (InvalidKeyException e) {
        throw new RuntimeException("fatal error", e);
    }
}

From source file:org.apache.cloudstack.cloudian.client.CloudianUtils.java

/**
 * Generates RFC-2104 compliant HMAC signature
 * @param data//from w  w w  . j a  v a 2  s  .c  om
 * @param key
 * @return returns the generated signature or null on error
 */
public static String generateHMACSignature(final String data, final String key) {
    if (Strings.isNullOrEmpty(data) || Strings.isNullOrEmpty(key)) {
        return null;
    }
    try {
        final SecretKeySpec signingKey = new SecretKeySpec(key.getBytes(), HMAC_SHA1_ALGORITHM);
        final Mac mac = Mac.getInstance(HMAC_SHA1_ALGORITHM);
        mac.init(signingKey);
        byte[] rawHmac = mac.doFinal(data.getBytes());
        return Base64.encodeBase64String(rawHmac);
    } catch (final Exception e) {
        LOG.error("Failed to generate HMAC signature from provided data and key, due to: ", e);
    }
    return null;
}