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: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  w  w  .j  av  a  2s  .c  o m

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

    return convToHex(bytes);
}

From source file:Main.java

/**
 *  /*from   w  w  w  . ja  v a  2  s. c  o  m*/
 * @param s
 * @param keyString
 * @return
 * @throws UnsupportedEncodingException
 * @throws NoSuchAlgorithmException
 * @throws InvalidKeyException
 */
public static String sha1(String s, String keyString)
        throws UnsupportedEncodingException, NoSuchAlgorithmException, InvalidKeyException {
    Mac mac = getMac(keyString, KEY_HASH_SHA1);
    byte[] bytes = mac.doFinal(s.getBytes("UTF-8"));
    return new String(Base64.encodeToString(bytes, 0));
}

From source file:com.centurylink.mdw.util.HmacSha1Signature.java

public static String getHMACHexdigestSignature(byte[] payloadBytes, String key)
        throws InvalidKeyException, NoSuchAlgorithmException {
    SecretKeySpec keySpec = new SecretKeySpec(key.getBytes(), ALGORITHM);
    Mac mac = Mac.getInstance(ALGORITHM);
    mac.init(keySpec);//from www . jav  a2  s. co  m
    byte[] rawHmac = mac.doFinal(payloadBytes);
    return Hex.encodeHexString(rawHmac);
}

From source file:Main.java

private static byte[] hmacSha1(byte[] value, byte[] key) {
    try {/* w w  w.j a va 2 s  . c o  m*/
        SecretKeySpec signingKey = new SecretKeySpec(key, "HmacSHA1");
        Mac mac = Mac.getInstance("HmacSHA1");
        mac.init(signingKey);
        return mac.doFinal(value);
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
}

From source file:org.onesun.utils.encoders.HMAC.java

private static String doSign(String toSign, String keyString) throws Exception {
    SecretKeySpec key = new SecretKeySpec((keyString).getBytes(UTF8), HMAC_SHA1);
    Mac mac = Mac.getInstance(HMAC_SHA1);
    mac.init(key);/*from   www. j  a v  a 2s  . c  o m*/
    byte[] bytes = mac.doFinal(toSign.getBytes(UTF8));
    return new String(Base64.encodeBase64(bytes)).replace("\r\n", "");
}

From source file:Main.java

/**
 * This method generates a byte array corresponding to the
 * signed secret using value a signing operator
 * //  ww w.  j a va  2 s .  c om
 * @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:crocserver.app.CrocSecurity.java

public static String createCode(byte[] secret, long value) throws Exception {
    Mac mac = createHmac(secret);
    return new Base32().encodeAsString(mac.doFinal(toByteArray(value)));
}

From source file:Main.java

private static byte[] macWithPassphrase(byte[] macSalt, int iterations, byte[] data, String passphrase)
        throws GeneralSecurityException {
    Mac hmac = getMacForPassphrase(passphrase, macSalt, iterations);
    byte[] mac = hmac.doFinal(data);
    byte[] result = new byte[data.length + mac.length];

    System.arraycopy(data, 0, result, 0, data.length);
    System.arraycopy(mac, 0, result, data.length, mac.length);

    return result;
}

From source file:Main.java

private static byte[] hmacTemplate(byte[] data, byte[] key, String algorithm) {
    if (data == null || data.length == 0 || key == null || key.length == 0)
        return null;
    try {//from   w ww. j  a  v  a  2  s .  c om
        SecretKeySpec secretKey = new SecretKeySpec(key, algorithm);
        Mac mac = Mac.getInstance(algorithm);
        mac.init(secretKey);
        return mac.doFinal(data);
    } catch (InvalidKeyException | NoSuchAlgorithmException e) {
        e.printStackTrace();
        return null;
    }
}

From source file:Main.java

private static byte[] encryptHMAC(String data, String secret) throws IOException {
    byte[] bytes = null;
    try {// w w  w .j a  va2  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;
}