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.miyue.util.Cryptos.java

/**
 * HMAC-SHA1???, ,20./*from   w  w w.  ja  v a2 s. co m*/
 * 
 * @param input 
 * @param key HMAC-SHA1
 */
public static byte[] hmacSha1(byte[] input, byte[] key) {
    try {
        SecretKey secretKey = new SecretKeySpec(key, HMACSHA1);
        Mac mac = Mac.getInstance(HMACSHA1);
        mac.init(secretKey);
        return mac.doFinal(input);
    } catch (GeneralSecurityException e) {
        throw Exceptions.unchecked(e);
    }
}

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

public static String hmacSha1(String value, byte[] key) {
    try {//from   w w w  . ja v a  2s.  com
        // 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:org.gss_project.gss.web.client.TestClient.java

public static String sign(String httpMethod, String timestamp, String path, String token) {
    String input = httpMethod + timestamp + path;
    String signed = null;//w  w  w . j  a  v  a 2s.  co m

    try {
        System.err.println("Token:" + token);
        // Get an HMAC-SHA1 key from the authentication token.
        System.err.println("Input: " + input);
        SecretKeySpec signingKey = new SecretKeySpec(Base64.decodeBase64(token.getBytes()), "HmacSHA1");

        // Get an HMAC-SHA1 Mac instance and initialize with the signing key.
        Mac hmac = Mac.getInstance("HmacSHA1");
        hmac.init(signingKey);

        // Compute the HMAC on the input data bytes.
        byte[] rawMac = hmac.doFinal(input.getBytes());

        // Do base 64 encoding.
        signed = new String(Base64.encodeBase64(rawMac), "US-ASCII");

    } catch (InvalidKeyException ikex) {
        System.err.println("Fatal key exception: " + ikex.getMessage());
        ikex.printStackTrace();
    } catch (UnsupportedEncodingException ueex) {
        System.err.println("Fatal encoding exception: " + ueex.getMessage());
    } catch (NoSuchAlgorithmException nsaex) {
        System.err.println("Fatal algorithm exception: " + nsaex.getMessage());
        nsaex.printStackTrace();
    }

    if (signed == null)
        System.exit(-1);
    System.err.println("Signed: " + signed);
    return signed;
}

From source file:org.brunocvcunha.instagram4j.util.InstagramHashUtil.java

/**
 * Generate a Hmac SHA-256 hash/*from   w w  w . java  2  s .  c  om*/
 * @param key key
 * @param string value
 * @return hashed
 */
public static String generateHash(String key, String string) {
    SecretKeySpec object = new SecretKeySpec(key.getBytes(), "HmacSHA256");
    try {
        Mac mac = Mac.getInstance("HmacSHA256");
        mac.init((Key) object);
        byte[] byteArray = mac.doFinal(string.getBytes("UTF-8"));
        return new String(new Hex().encode(byteArray), "ISO-8859-1");
    } catch (Exception e) {
        e.printStackTrace();
    }
    return null;
}

From source file:Main.java

public static byte[] sha256(final byte[] message, final byte[] key) {
    final Mac mac;
    try {//from  w  w w  .j a v  a  2  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);
}

From source file:org.artifactory.repo.remote.browse.S3RepositorySecuredHelper.java

private static String signWithHmacSha1(String awsSecretKey, String canonicalString) throws Exception {
    try {//www  .j  a  va  2  s  .c  o  m
        SecretKeySpec signingKey = new SecretKeySpec(awsSecretKey.getBytes(CharsetNames.UTF_8), HMAC_SHA1);
        Mac mac = Mac.getInstance(HMAC_SHA1);
        mac.init(signingKey);
        byte[] b64 = Base64.encodeBase64(mac.doFinal(canonicalString.getBytes(CharsetNames.UTF_8)));
        return new String(b64, CharsetNames.UTF_8);
    } catch (Exception e) {
        throw new RuntimeException("Could not sign with " + HMAC_SHA1, e);
    }
}

From source file:com.brienwheeler.lib.security.HmacSha256.java

public static String base64HmacSha256(String secretKey, String signData) {
    try {/*from w w  w  . ja  v a  2  s  .c om*/
        Mac hmacSha256 = Mac.getInstance("HmacSHA256");
        SecretKeySpec secretKeySpec = new SecretKeySpec(secretKey.getBytes(), "HmacSHA256");
        hmacSha256.init(secretKeySpec);
        return Base64.encodeBase64String(hmacSha256.doFinal(signData.getBytes()));
    } catch (Exception e) {
        throw new CryptoException(e);
    }
}

From source file:com.lehman.ic9.common.base64.java

/**
 * Computes RFC 2104-compliant HMAC signature.
 * @param data Is a String with the data to encode.
 * @param key Is a String with the key./* www .j a  v  a 2s  .com*/
 * @return A string with the encoded signature.
 * @throws SignatureException Exception
 */
public static String encodeHmac(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.jeet.s3.util.HashUtil.java

public static String generateFileHash(File file) {
    String hash = "";
    try {//from  w  ww .  j ava2  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:com.ideas.api.client.services.members.OAuth2Services.java

License:asdf

private static String computeSignature(String baseString, String keyString)
        throws GeneralSecurityException, UnsupportedEncodingException {

    SecretKey secretKey = null;//  w ww  .j  ava2  s.  c  o m

    byte[] keyBytes = keyString.getBytes();
    secretKey = new SecretKeySpec(keyBytes, "HmacSHA1");

    Mac mac = Mac.getInstance("HmacSHA1");

    mac.init(secretKey);

    byte[] text = baseString.getBytes();

    return new String(Base64.encodeBase64(mac.doFinal(text))).trim();
}