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:com.amazonaws.tvm.Utilities.java

public static String sign(String content, String key) {
    try {/*from   ww  w.j  a v a  2 s.  co m*/
        byte[] data = content.getBytes(Constants.ENCODING_FORMAT);
        Mac mac = Mac.getInstance(Constants.SIGNATURE_METHOD);
        mac.init(new SecretKeySpec(key.getBytes(Constants.ENCODING_FORMAT), Constants.SIGNATURE_METHOD));
        char[] signature = Hex.encodeHex(mac.doFinal(data));
        return new String(signature);
    } catch (Exception exception) {
        log.log(Level.SEVERE, "Exception during sign", exception);
    }
    return null;
}

From source file:Pusher.java

/**
 * Returns a HMAC/SHA256 representation of the given string
 * @param data//from  w ww .j  a  v  a  2  s .  com
 * @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.emc.atmos.api.RestUtil.java

public static String sign(String string, byte[] hashKey) {
    try {//from  ww w .  j  av  a  2 s  .co  m
        // Compute the signature hash
        l4j.debug("Hashing: \n" + string);

        byte[] input = string.getBytes("UTF-8");

        Mac mac = Mac.getInstance("HmacSHA1");
        SecretKeySpec key = new SecretKeySpec(hashKey, "HmacSHA1");
        mac.init(key);

        byte[] hashBytes = mac.doFinal(input);

        // Encode the hash in Base64.
        String hash = new String(Base64.encodeBase64(hashBytes), "UTF-8");

        l4j.debug("Hash: " + hash);

        return hash;
    } catch (Exception e) {
        throw new RuntimeException("Error signing string:\n" + string + "\n", e);
    }
}

From source file:air.com.snagfilms.utils.Utils.java

public static String getfilmriseParameters() {

    StringBuilder strBlr = null;/*from   www  .ja v a  2 s  . c o m*/
    try {

        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'");
        dateFormat.setTimeZone(TimeZone.getTimeZone("UTC"));
        long longs = System.currentTimeMillis();
        String timestamp = dateFormat.format(longs);
        ;
        String stringToSign = timestamp; // Query uses this string

        // Compute the signature and base64 encode it.
        String algorithm = "HmacSHA1";
        SecretKeySpec key = new SecretKeySpec(SECRET_KEY.getBytes(), algorithm);
        Mac mac = Mac.getInstance(algorithm);
        mac.init(key);

        String signature = new String(Base64.encodeBase64(mac.doFinal(stringToSign.getBytes())));
        System.out.println(signature);// required for Query
        signature = URLEncoder.encode(signature, "UTF-8");

        strBlr = new StringBuilder();
        strBlr.append(Constants.ACCESS_KEY);
        strBlr.append("=");
        strBlr.append(ACCESS_KEY);

        strBlr.append("&");
        strBlr.append(Constants.TIME_STAMP);
        strBlr.append("=");
        strBlr.append(timestamp);

        strBlr.append("&");
        strBlr.append(Constants.SIGNATURE);
        strBlr.append("=");
        strBlr.append(signature);

        strBlr.append("&");
        strBlr.append(Constants.SITE);
        strBlr.append("=");
        strBlr.append(Constants.filmrise);

        strBlr.append("&");
        strBlr.append(Constants.DEVICE);
        strBlr.append("=");
        strBlr.append("android");

        return strBlr.toString();

    } catch (Exception e) {

    }
    return null;
}

From source file:ch.rasc.wampspring.cra.DefaultAuthenticationHandler.java

public static String generateHMacSHA256(final String key, final String data)
        throws InvalidKeyException, NoSuchAlgorithmException {
    Assert.notNull(key, "key is required");
    Assert.notNull(data, "data is required");

    final Mac hMacSHA256 = Mac.getInstance("HmacSHA256");
    byte[] hmacKeyBytes = key.getBytes(StandardCharsets.UTF_8);
    final SecretKeySpec secretKey = new SecretKeySpec(hmacKeyBytes, "HmacSHA256");
    hMacSHA256.init(secretKey);/*from  w ww. ja  va2  s. co  m*/
    byte[] dataBytes = data.getBytes(StandardCharsets.UTF_8);
    byte[] res = hMacSHA256.doFinal(dataBytes);

    return DatatypeConverter.printBase64Binary(res);
}

From source file:com.monitor.baseservice.utils.XCodeUtil.java

public static String hmacSha1(String value, String key) {
    try {/* www .  j  av a 2s .  c  om*/
        // 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());

        return Base64.encodeBase64URLSafeString(rawHmac);
    } catch (NoSuchAlgorithmException e) {
        throw new RuntimeException(e);
    } catch (InvalidKeyException e) {
        throw new RuntimeException(e);
    }
}

From source file:com.annuletconsulting.homecommand.server.HomeCommand.java

private static String getSignature(String timeStamp) {
    if (sharedKey != null)
        try {//from  w  ww .j  av a  2s .  c  om
            byte[] data = timeStamp.getBytes(ENCODING_FORMAT);
            Mac mac = Mac.getInstance(SIGNATURE_METHOD);
            mac.init(new SecretKeySpec(sharedKey.getBytes(ENCODING_FORMAT), SIGNATURE_METHOD));
            char[] signature = Hex.encodeHex(mac.doFinal(data));
            return new String(signature);
        } catch (Exception exception) {
            exception.printStackTrace();
        }
    return "Error in getSignature()";
}

From source file:org.apache.abdera2.common.security.HashHelper.java

public static boolean hmacval(Key key, String alg, byte[] mat, byte[] dat) {
    try {/*from  w w  w.  j  a v  a 2 s  .  c  o m*/
        Mac mac = Mac.getInstance(alg);
        mac.init(key);
        byte[] sig = mac.doFinal(mat);
        return Arrays.equals(sig, dat);
    } catch (Throwable t) {
        throw ExceptionHelper.propogate(t);
    }
}

From source file:jp.primecloud.auto.api.ApiFilter.java

/**
 *
 * HMAC-SHA256???/*from  ww  w . jav a2  s . c o  m*/
 *
 * @param plainText ?
 * @param keyText
 * @return
 * @throws InvalidKeyException
 * @throws NoSuchAlgorithmException
 * @throws UnsupportedEncodingException
 */
private static String encodeSHA256(String plainText, String keyText)
        throws NoSuchAlgorithmException, InvalidKeyException, UnsupportedEncodingException {
    SecretKey secretKey = new SecretKeySpec(keyText.getBytes("UTF-8"), "HmacSHA256");
    Mac mac = Mac.getInstance("HmacSHA256");
    mac.init(secretKey);

    byte[] plainBytes = plainText.getBytes("UTF-8");
    byte[] encodedBytes = mac.doFinal(plainBytes);
    byte[] hexBytes = new Hex().encode(encodedBytes);

    return new String(hexBytes, "UTF-8");
}

From source file:com.flazr.Utils.java

public static byte[] sha256(byte[] message, byte[] key) {
    Mac mac;
    try {//from  w w w  . ja  v  a2  s. c  o m
        mac = Mac.getInstance("HmacSHA256");
        mac.init(new SecretKeySpec(key, "HmacSHA256"));
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
    return mac.doFinal(message);
}