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.alibaba.openapi.client.util.SignatureUtil.java

public static byte[] hmacSha1(byte[] data, SecretKeySpec signingKey) {
    Mac mac = null;//from www  . jav  a  2 s . com
    try {
        mac = Mac.getInstance(HMAC_SHA1);
        mac.init(signingKey);
    } catch (NoSuchAlgorithmException e) {
        throw new IllegalStateException(e.getMessage(), e);
    } catch (InvalidKeyException e) {
        throw new IllegalStateException(e.getMessage(), e);
    }
    return mac.doFinal(data);
}

From source file:com.konakart.bl.modules.payment.barclaycardsmartpayhosted.BarclaycardSmartPayHostedHMACTools.java

/**
 * Get a HMAC signature using the specified secret 
 * @param secret//  w  w  w. ja va  2s  . co  m
 * @param signingData
 * @return a Base64 encoded signature
 */
public static String getBase64EncodedSignature(String secret, String signingData) {
    SecretKey key = getMacKey(secret);
    try {
        Mac mac = Mac.getInstance(key.getAlgorithm());
        mac.init(getMacKey(secret));
        byte[] digest = mac.doFinal(signingData.getBytes("UTF8"));
        return new String(Base64.encodeBase64(digest), "ASCII");
    } catch (NoSuchAlgorithmException e) {
        e.printStackTrace();
    } catch (IllegalStateException e) {
        e.printStackTrace();
    } catch (UnsupportedEncodingException e) {
        e.printStackTrace();
    } catch (InvalidKeyException e) {
        e.printStackTrace();
    }
    return null;
}

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 . jav 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:de.desy.dcache.s3.Signature.java

/**
* Computes RFC 2104-compliant HMAC signature.
* * @param data/*  www  .  j av  a2 s  . c  o m*/
* The data to be signed.
* @param key
* The signing key.
* @return
* The Base64-encoded RFC 2104-compliant HMAC signature.
* @throws
* java.security.SignatureException when signature generation fails
*/
public static String calculateRFC2104HMAC(String data, String key) throws java.security.SignatureException {
    String result;
    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
        result = new String(Base64.encodeBase64(rawHmac));

    } catch (Exception e) {
        throw new SignatureException("Failed to generate HMAC: " + e.getMessage());
    }
    return result;
}

From source file:com.gmu.uav.RadioSecurity.java

public static String hmacSha1(String value, byte[] key) {
    try {//from  w  ww  . j a  va2 s.co  m
        // Get an hmac_sha1 key from the raw key bytes    
        SecretKeySpec signingKey = new SecretKeySpec(key, "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());

        // Convert raw bytes to Hex
        byte[] hexBytes = new Hex().encode(rawHmac);

        //  Covert array of Hex bytes to a String
        return new String(hexBytes, "UTF-8");
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
}

From source file:Main.java

/**
 *
 * @param key//from  w w  w  .  j  ava  2s .c  om
 * @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

/**
 * 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  www.jav a  2 s .c o  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);
}

From source file:cn.crawin.msg.gateway.http.SignUtil.java

/**
 * ??//w ww  . j  a v a 2 s .co m
 *
 * @param secret APP
 * @param method HttpMethod
 * @param path
 * @param headers
 * @param querys
 * @param bodys
 * @param signHeaderPrefixList ???Header?
 * @return ???
 */
public static String sign(String secret, String method, String path, Map<String, String> headers,
        Map<String, String> querys, Map<String, String> bodys, List<String> signHeaderPrefixList) {
    try {
        Mac hmacSha256 = Mac.getInstance(Constants.HMAC_SHA256);
        byte[] keyBytes = secret.getBytes(Constants.ENCODING);
        hmacSha256.init(new SecretKeySpec(keyBytes, 0, keyBytes.length, Constants.HMAC_SHA256));

        return new String(Base64.encodeBase64(
                hmacSha256.doFinal(buildStringToSign(method, path, headers, querys, bodys, signHeaderPrefixList)
                        .getBytes(Constants.ENCODING))),
                Constants.ENCODING);
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
}

From source file:de.marx_labs.utilities.common.util.HashUtil.java

public static String hmacSha1(String value, String key) {
    try {//  w  w  w.  j a v a 2s .  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());

        // Convert raw bytes to Hex
        byte[] hexBytes = new Hex().encode(rawHmac);

        // Covert array of Hex bytes to a String
        return new String(hexBytes, "UTF-8");
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
}

From source file:com.example.aliyundemo.ocr.util.SignUtil.java

/**
 * ??//from   www .  j  ava  2s.  c  o  m
 *
 * @param method               HttpMethod
 * @param url                  Path+Query
 * @param headers              Http
 * @param formParamMap         POST??
 * @param secret               APP
 * @param signHeaderPrefixList ???Header?
 * @return ???
 */
public static String sign(String method, String url, Map<String, String> headers, Map formParamMap,
        String secret, List<String> signHeaderPrefixList) {
    try {
        Mac hmacSha256 = Mac.getInstance(Constants.HMAC_SHA256);
        byte[] keyBytes = secret.getBytes(Constants.ENCODING);
        hmacSha256.init(new SecretKeySpec(keyBytes, 0, keyBytes.length, Constants.HMAC_SHA256));

        return new String(Base64.encodeBase64(
                hmacSha256.doFinal(buildStringToSign(headers, url, formParamMap, method, signHeaderPrefixList)
                        .getBytes(Constants.ENCODING))),
                Constants.ENCODING);
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
}