Compute HMAC-SHA512 with secret key in java

Also you can use a little wrapper by Apache Commons codec:

import static org.apache.commons.codec.digest.HmacAlgorithms.HMAC_SHA_512;

import org.apache.commons.codec.digest.HmacUtils;

public class HmacService {

    private String sharedSecret;

    public HmacService(String sharedSecret) {
        this.sharedSecret = sharedSecret;
    }

    public String calculateHmac(String data) {
        return new HmacUtils(HMAC_SHA_512, sharedSecret).hmacHex(data);
    }

    public boolean checkHmac(String data, String hmacHex) {
        return calculateHmac(data).equals(hmacHex);
    }

}

The simplest way can be -

private static final String HMAC_SHA512 = "HmacSHA512";

private static String toHexString(byte[] bytes) {
    Formatter formatter = new Formatter();
    for (byte b : bytes) {
        formatter.format("%02x", b);
    }
    return formatter.toString();
}

public static String calculateHMAC(String data, String key)
    throws SignatureException, NoSuchAlgorithmException, InvalidKeyException
{
    SecretKeySpec secretKeySpec = new SecretKeySpec(key.getBytes(), HMAC_SHA512);
    Mac mac = Mac.getInstance(HMAC_SHA512);
    mac.init(secretKeySpec);
    return toHexString(mac.doFinal(data.getBytes()));
}

public static void main(String[] args) throws Exception {
    String hmac = calculateHMAC("data", "key");
    System.out.println(hmac);
}

You can change the HMAC_SHA512 variable to any of the Mac algorithm and the code will work the same way.


Hope this helps:

import javax.crypto.Mac;
import javax.crypto.spec.SecretKeySpec;
import java.io.UnsupportedEncodingException;
import java.nio.charset.StandardCharsets;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import java.util.Base64;

public class Test1 {

    private static final String HMAC_SHA512 = "HmacSHA512";

    public static void main(String[] args) {
        Mac sha512Hmac;
        String result;
        final String key = "Welcome1";

        try {
            final byte[] byteKey = key.getBytes(StandardCharsets.UTF_8);
            sha512Hmac = Mac.getInstance(HMAC_SHA512);
            SecretKeySpec keySpec = new SecretKeySpec(byteKey, HMAC_SHA512);
            sha512Hmac.init(keySpec);
            byte[] macData = sha512Hmac.doFinal("My message".getBytes(StandardCharsets.UTF_8));

            // Can either base64 encode or put it right into hex
            result = Base64.getEncoder().encodeToString(macData);
            //result = bytesToHex(macData);
        } catch (UnsupportedEncodingException | InvalidKeyException | NoSuchAlgorithmException e) {
            e.printStackTrace();
        } finally {
            // Put any cleanup here
            System.out.println("Done");
        }
    }
}

For converting from byte array to hex refer this stackoverflow answer : here

Tags:

Java

Hmac