Java generates base64 encoded hmac with sha256.

import javax.crypto.Mac;
import javax.crypto.spec.SecretKeySpec;
import java.util.Base64;

public class App {

    public static String getBase64HmacSha256(
        String secretKey,
        String message
    ) throws Exception {
        SecretKeySpec secretKeySpec = new SecretKeySpec(secretKey.getBytes(), "HmacSHA256");
        Mac hmacSHA256 = Mac.getInstance("HmacSHA256");
        hmacSHA256.init(secretKeySpec);
        byte[] hmac = hmacSHA256.doFinal(message.getBytes());
        return Base64.getEncoder().encodeToString(hmac);
    }
}
TOPâ–˛
Post tagged with: java