hmac method
Generate a hash-based message authentication code (HMAC)
data
is the data to authenticate
key
is the secret key for authentication
algorithm
is the hash algorithm to use (default: SHA-256)
Returns the HMAC as a hex string
Implementation
String hmac(
String data,
String key, {
String algorithm = 'sha256',
}) {
final dataBytes = utf8.encode(data);
final keyBytes = utf8.encode(key);
late Digest digest;
switch (algorithm.toLowerCase()) {
case 'sha256':
digest = Hmac(sha256, keyBytes).convert(dataBytes);
break;
case 'sha512':
digest = Hmac(sha512, keyBytes).convert(dataBytes);
break;
default:
throw ArgumentError('Unsupported hash algorithm: $algorithm');
}
return digest.toString();
}