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:Main.java

private static String computeSignature(String baseString, String keyString)
        throws GeneralSecurityException, UnsupportedEncodingException {
    Mac mac = Mac.getInstance("HmacSHA256");
    SecretKeySpec secret = new SecretKeySpec(keyString.getBytes("UTF-8"), "HmacSHA256");
    mac.init(secret);//from  w  w w .  j a v  a  2  s . c o m
    byte[] byteData = mac.doFinal(baseString.getBytes("UTF-8"));

    BigInteger hash = new BigInteger(1, byteData);
    String hmac = hash.toString(16);

    if (hmac.length() % 2 != 0) {
        hmac = "0" + hmac;
    }

    return hmac;
}

From source file:Main.java

public static String createHMACWithMD5(String source, String key)
        throws NoSuchAlgorithmException, InvalidKeyException {
    Mac hmac = Mac.getInstance("HmacMD5");
    SecretKeySpec secretKey = new SecretKeySpec(key.getBytes(), hmac.getAlgorithm());
    hmac.init(secretKey);//  w  w w .  j a v a  2  s  . c o m

    byte[] signature = hmac.doFinal(source.getBytes());
    return Base64.encodeToString(signature, Base64.DEFAULT);
}

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  . j  a  v a 2s  .c o m
    return asHex(sha256_HMAC.doFinal(data.getBytes("utf-8")));
}

From source file:Main.java

public static String computeHmacSha1(String value, String keyString) throws InvalidKeyException,
        IllegalStateException, UnsupportedEncodingException, NoSuchAlgorithmException {
    SecretKeySpec key = new SecretKeySpec((keyString).getBytes("UTF-8"), "HmacSHA1");
    Mac mac = Mac.getInstance("HmacSHA1");
    mac.init(key);//from w ww.ja v a  2 s.c  om

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

    return convToHex(bytes);
}

From source file:Main.java

public static byte[] hmacSha1(String key, String value)
        throws UnsupportedEncodingException, NoSuchAlgorithmException, InvalidKeyException {
    String type = "HmacSHA1";
    SecretKeySpec secret = new SecretKeySpec(key.getBytes(), type);
    Mac mac = Mac.getInstance(type);
    mac.init(secret);/* w  w  w . ja  va 2 s  . c  om*/
    return mac.doFinal(value.getBytes());
}

From source file:Main.java

private static byte[] encryptHMAC(String data, String secret) throws IOException {
    byte[] bytes = null;
    try {/*www  . ja v a2  s  .co  m*/
        SecretKey secretKey = new SecretKeySpec(secret.getBytes("UTF-8"), "HmacMD5");
        Mac mac = Mac.getInstance(secretKey.getAlgorithm());
        mac.init(secretKey);
        bytes = mac.doFinal(data.getBytes("UTF-8"));
    } catch (GeneralSecurityException gse) {
        String msg = getStringFromException(gse);
        throw new IOException(msg);
    }
    return bytes;
}

From source file:com.jeet.s3.util.HashUtil.java

public static String generateFileHash(File file) {
    String hash = "";
    try {//from w  ww.  j  a v  a 2 s  . c  o  m
        Mac sha256_HMAC = Mac.getInstance("HmacSHA256");
        SecretKeySpec secret_key = new SecretKeySpec(Constants.HASH_SECRET.getBytes(), "HmacSHA256");
        sha256_HMAC.init(secret_key);

        hash = Base64.encodeBase64String(sha256_HMAC.doFinal(IOUtils.toByteArray(new FileInputStream(file))));
    } catch (Exception ex) {
        System.out.println("Error in generating hash.");
    }
    return hash;
}

From source file:Main.java

public static String doSignWithSHA1(String toSign, String keyString) throws Exception {
    SecretKeySpec key = new SecretKeySpec((keyString).getBytes(UTF_8), HMAC_SHA1);
    Mac mac = Mac.getInstance(HMAC_SHA1);
    mac.init(key);/*w  w  w .ja  v a2s. c  o  m*/
    byte[] bytes = mac.doFinal(toSign.getBytes(UTF_8));
    return Base64.encodeToString(bytes, Base64.CRLF).replace(CARRIAGE_RETURN, EMPTY_STRING);
}

From source file:Main.java

/**
 * This method generates a byte array corresponding to the
 * signed secret using value a signing operator
 * /* w  ww  . j  a  v  a 2 s .c o m*/
 * @param   value   the value used to sign the key
 * @param   secretKey   the key to sign
 * @return   a byte array corresponding to the raw HMAC-SHA1 hash
 */
public static byte[] hmacSha1(byte[] value, byte[] secretKey) throws RuntimeException {
    try {
        SecretKeySpec signingKey = new SecretKeySpec(secretKey, HMAC_ALGORITHM);
        Mac mac = Mac.getInstance(HMAC_ALGORITHM);
        mac.init(signingKey);
        byte[] rawHmac = mac.doFinal(value);
        return (rawHmac);
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
}

From source file:Main.java

public static String sign(String paramString1, String paramString2, String paramString3) {
    if (paramString3 == null)
        paramString3 = "";
    try {//  w w  w. j  av a  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 "";
}