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.konakart.bl.modules.payment.barclaycardsmartpayhosted.BarclaycardSmartPayHostedHMACTools.java

/**
 * Get a HMAC signature using the specified secret 
 * @param secret/*from  w w w  .j a v a  2  s .c  o  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.konakart.bl.modules.payment.barclaycardsmartpayhosted.BarclaycardSmartPayHostedHMACTools.java

/**
 * Verify the signature/*from   w w w  .j av a2s .  com*/
 * @param secret
 * @param sig
 * @param signedData
 * @return true if the signature is verified
 */
public static boolean verifyBase64EncodedSignature(String secret, String sig, String signedData) {
    if (secret == null || sig == null || signedData == null)
        return false;

    SecretKey key = getMacKey(secret);
    try {
        Mac mac = Mac.getInstance(key.getAlgorithm());
        mac.init(getMacKey(secret));
        byte[] digest = mac.doFinal(signedData.getBytes("UTF8"));
        return sig.equals(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 false;
}

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:org.b3log.symphony.util.HmacSHA1.java

public static String signString(String source, String accessSecret)
        throws InvalidKeyException, IllegalStateException {
    try {//  w  ww  .j  a  v  a  2 s. com
        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:Main.java

public static String hmacSha256Encode(String key, String data)
        throws NoSuchAlgorithmException, InvalidKeyException, UnsupportedEncodingException {
    Mac sha256_HMAC = Mac.getInstance("HmacSHA256");
    SecretKeySpec secret_key = new SecretKeySpec(key.getBytes("utf-8"), "HmacSHA256");
    sha256_HMAC.init(secret_key);//from   w  w  w .ja  va  2  s.  c o m
    return asHex(sha256_HMAC.doFinal(data.getBytes("utf-8")));
}

From source file:com.khipu.lib.java.KhipuService.java

public static String HmacSHA256(String secret, String data) {
    try {/*from  w w  w .  java  2  s.  c  om*/
        SecretKeySpec secretKeySpec = new SecretKeySpec(secret.getBytes("UTF-8"), "HmacSHA256");
        Mac mac = Mac.getInstance("HmacSHA256");
        mac.init(secretKeySpec);
        byte[] digest = mac.doFinal(data.getBytes("UTF-8"));
        return byteArrayToString(digest);
    } catch (InvalidKeyException e) {
        throw new RuntimeException("Invalid key exception while converting to HMac SHA256");
    } catch (NoSuchAlgorithmException e) {
        throw new RuntimeException("Algorithm not supported");
    } catch (UnsupportedEncodingException e) {
        throw new RuntimeException("Encoding not supported");
    }
}

From source file:com.kku.apps.pricesearch.util.SignedHelper.java

private static String sign(String apiSecretKey, String sourceString) throws Exception {
    SecretKeySpec secretKeySpec = new SecretKeySpec(apiSecretKey.getBytes(ENCODING), ALGORITHM);
    Mac mac = Mac.getInstance(ALGORITHM);
    mac.init(secretKeySpec);//from w  w  w . j a v a2 s  .  c  o  m
    byte[] data = mac.doFinal(sourceString.getBytes(ENCODING));
    Base64 encoder = new Base64();
    String signature = new String(encoder.encode(data));
    return signature;
}

From source file:Main.java

public static String sign(String paramString1, String paramString2, String paramString3) {
    if (paramString3 == null)
        paramString3 = "";
    try {//from w w  w . j  ava 2 s.  c o m
        SecretKeySpec localSecretKeySpec = new SecretKeySpec(
                (urlencode(paramString2, "UTF8") + '&' + urlencode(paramString3, "UTF8")).getBytes("UTF8"),
                "HmacSHA1");
        Mac localMac = Mac.getInstance("HmacSHA1");
        localMac.init(localSecretKeySpec);
        String str = urlencode(
                new String(Base64.encode(localMac.doFinal(paramString1.getBytes("UTF8")), 0), "UTF8"), "UTF8");
        return str;
    } catch (InvalidKeyException localInvalidKeyException) {
        return "";
    } catch (NoSuchAlgorithmException localNoSuchAlgorithmException) {
        return "";
    } catch (UnsupportedEncodingException localUnsupportedEncodingException) {
    }
    return "";
}

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

public static String hmacSha1(String value, String key) {
    try {//from  www.  j  a v  a  2  s . com
        // 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:Main.java

/***
 * Computes RFC 2104-compliant HMAC signature. This can be used to sign the Amazon S3
 * request urls//  w  ww  . j  ava  2  s  .  co  m
 * 
 * @param data The data to be signed.
 * @param key The signing key.
 * @return The Base64-encoded RFC 2104-compliant HMAC signature.
 * @throws SignatureException when signature generation fails
 */
public static String getHMac(String data, String key) throws SignatureException {

    if (data == null) {
        throw new NullPointerException("Data to be signed cannot be null");
    }

    String result = null;
    try {

        final String HMAC_SHA1_ALGORITHM = "HmacSHA1";

        // 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 &
        // initialize with the signing key
        Mac mac = Mac.getInstance(HMAC_SHA1_ALGORITHM);
        mac.init(signingKey);

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

        if (digest != null) {
            // Base 64 Encode the results
            result = Base64.encodeToString(digest, Base64.NO_WRAP);
        }

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

    return result;
}