Hash-based Message Authentication Code (HMAC)
Last updated
import (
"crypto/sha256"
"crypto/hmac"
)
func CalculateHMAC(message, key []byte) []bytes {
mac := hmac.New(sha256.New, key)
mac.Write(message)
return mac.Sum(nil)
}import javax.crypto.Mac;
import java.security.NoSuchAlgorithmException;
import java.security.InvalidKeyException;
public static byte[] calculateHMAC(String message, byte[] key) throws NoSuchAlgorithmException, InvalidKeyException {
Mac hasher = Mac.getInstance("HmacSHA256");
hasher.init(new SecretKeySpec(key, "HmacSHA256"));
return hasher.doFinal(message.getBytes());
}const { createHmac } = await import('node:crypto');
async function calculateHMAC(message, key) {
const hmac = createHmac('sha256', key);
hmac.update(message);
return hmac.digest('hex');
}import hmac
def calculate_hmac(message: str, key: bytes | bytearray):
h = hmac.new(key, message, hashlib.sha256)
return h.hexdigest()
def compare_hmac_digests(a: str | bytes, b: str | bytes) -> bool:
return hmac.compare_digest(a, b)