Example usage for javax.crypto Mac doFinal

List of usage examples for javax.crypto Mac doFinal

Introduction

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

Prototype

public final byte[] doFinal(byte[] input) throws IllegalStateException 

Source Link

Document

Processes the given array of bytes and finishes the MAC operation.

Usage

From source file:org.encuestame.oauth1.support.OAuth1Utils.java

/**
 *
 * @param signatureBaseString//  w w  w .jav a 2s .c o m
 * @param key
 * @return
 */
private static String sign(String signatureBaseString, String key) {
    try {
        Mac mac = Mac.getInstance(HMAC_SHA1_MAC_NAME);
        SecretKeySpec spec = new SecretKeySpec(key.getBytes(), HMAC_SHA1_MAC_NAME);
        mac.init(spec);
        byte[] text = signatureBaseString.getBytes("UTF-8");
        byte[] signatureBytes = mac.doFinal(text);
        signatureBytes = Base64.encodeBase64(signatureBytes);
        String signature = new String(signatureBytes, "UTF-8");
        return signature;
    } catch (NoSuchAlgorithmException e) {
        throw new IllegalStateException(e);
    } catch (InvalidKeyException e) {
        throw new IllegalStateException(e);
    } catch (UnsupportedEncodingException e) {
        throw new RuntimeException(e);
    }
}

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

/**
 * ??/*from  w  ww .j  av  a2s. c om*/
 *
 * @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:com.example.aliyundemo.ocr.util.SignUtil.java

/**
 * ??/*from w  ww  .  ja va  2s.  com*/
 *
 * @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);
    }
}

From source file:OAUTHnesia.java

private static String sha1(String s, String keyString)
        throws UnsupportedEncodingException, NoSuchAlgorithmException, InvalidKeyException {

    SecretKeySpec key = new SecretKeySpec((keyString).getBytes("UTF-8"), "HmacSHA1");
    Mac mac = Mac.getInstance("HmacSHA1");
    mac.init(key);//w w w  .  j  a v a 2s. co m

    byte[] bytes = mac.doFinal(s.getBytes("UTF-8"));

    return new String(Base64.encode(bytes, Base64.DEFAULT));
}

From source file:com.livily.pusher.Pusher.java

/**
 * Returns a HMAC/SHA256 representation of the given string
 * //w w w  .j a v a2s .co  m
 * @param data
 * @return
 */
private static String hmacsha256Representation(String data) {
    try {
        // Create the HMAC/SHA256 key from application secret
        final SecretKeySpec signingKey = new SecretKeySpec(pusherApplicationSecret.getBytes(), "HmacSHA256");

        // Create the message authentication code (MAC)
        final Mac mac = Mac.getInstance("HmacSHA256");
        mac.init(signingKey);

        // Process and return data
        byte[] digest = mac.doFinal(data.getBytes("UTF-8"));
        digest = mac.doFinal(data.getBytes());
        // Convert to string
        BigInteger bigInteger = new BigInteger(1, digest);
        return String.format("%0" + (digest.length << 1) + "x", bigInteger);
    } catch (NoSuchAlgorithmException nsae) {
        // We should never come here, because GAE has HMac SHA256
        throw new RuntimeException("No HMac SHA256 algorithm");
    } catch (UnsupportedEncodingException e) {
        // We should never come here, because UTF-8 should be available
        throw new RuntimeException("No UTF-8");
    } catch (InvalidKeyException e) {
        throw new RuntimeException("Invalid key exception while converting to HMac SHA256");
    }
}

From source file:com.ljt.openapi.demo.util.SignUtil.java

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

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

From source file:Networking.Server.java

public static String calculateHMAC(String data, byte[] key) {
    Mac mac = null;
    byte[] res = null;
    try {/*from   w w w.  java 2s. com*/
        SecretKeySpec signingKey = new SecretKeySpec(key, "HmacSHA1");
        mac = Mac.getInstance("HmacSHA1");
        mac.init(signingKey);
        res = (mac.doFinal(data.getBytes()));

    } catch (InvalidKeyException | NoSuchAlgorithmException ex) {
        Logger.getLogger(Server.class.getName()).log(Level.SEVERE, null, ex);
    }
    return toHexString(res);
}

From source file:conexionSiabra.Oauth.java

private static String hmac_sha1(String value, String key) {
    try {/*from www.j  av  a2  s.co m*/
        SecretKey secretKey = null;
        byte[] keyBytes = key.getBytes();
        secretKey = new SecretKeySpec(keyBytes, "HmacSHA1");
        Mac mac = Mac.getInstance("HmacSHA1");
        mac.init(secretKey);
        byte[] text = value.getBytes();
        return new String(Base64.encode(mac.doFinal(text), 0)).trim();
    } catch (NoSuchAlgorithmException e) {
        e.printStackTrace();
    } catch (InvalidKeyException e) {
        e.printStackTrace();
    }
    return null;
}

From source file:SecurityUtils.java

/**
 * Converts a source string to its HMAC/SHA-1 value.
 * //w  w w . j  av a2 s .c om
 * @param source
 *            The source string to convert.
 * @param secretKey
 *            The secret key to use for conversion.
 * @return The HMac value of the source string.
 */
public static byte[] toHMac(String source, String secretKey) {
    byte[] result = null;

    try {
        // Create the HMAC/SHA1 key
        final SecretKeySpec signingKey = new SecretKeySpec(secretKey.getBytes(), "HmacSHA1");

        // Create the message authentication code (MAC)
        final Mac mac = Mac.getInstance("HmacSHA1");
        mac.init(signingKey);

        // Compute the HMAC value
        result = mac.doFinal(source.getBytes());
    } catch (NoSuchAlgorithmException nsae) {
        throw new RuntimeException("Could not find the SHA-1 algorithm. HMac conversion failed.", nsae);
    } catch (InvalidKeyException ike) {
        throw new RuntimeException("Invalid key exception detected. HMac conversion failed.", ike);
    }

    return result;
}

From source file:com.amazonaws.cbui.AmazonFPSCBUIPipeline.java

/**
 * Computes RFC 2104-compliant HMAC signature.
 *///from w ww  . j a  v a 2s .co m
private static String sign(String data, String key, String signatureMethod) throws SignatureException {
    String signature = "";
    try {
        Mac mac = Mac.getInstance(signatureMethod);
        mac.init(new SecretKeySpec(key.getBytes(), signatureMethod));
        signature = new String(Base64.encodeBase64(mac.doFinal(data.getBytes(UTF_8_Encoding))));
    } catch (Exception e) {
        throw new SignatureException("Failed to generate signature: " + e.getMessage(), e);
    }
    return signature;
}