hmac method
HMAC of data under key, with the digest algorithmName names —
sha256, sha512, … or a DigestAlgorithm constant, which is the same
string with the spelling checked: hmac(DigestAlgorithm.sha256, key, data).
hmacHex returns the same as lowercase hexadecimal.
Implementation
Uint8List hmac(String algorithmName, Uint8List key, Uint8List data) {
final state = _digestState;
// Throws for an unknown algorithm, and caches the lookup.
final md = state.digestByName(algorithmName);
final ctx = state.hmacContext;
final arena = Arena();
try {
final keyPtr = arena<UnsignedChar>(key.length);
keyPtr.cast<Uint8>().asTypedList(key.length).setAll(0, key);
if (bindings.HMAC_Init_ex(ctx, keyPtr.cast(), key.length, md, nullptr) !=
1) {
throw OpenSslException('HMAC_Init_ex failed');
}
if (data.isNotEmpty) {
final dataPtr = arena<UnsignedChar>(data.length);
dataPtr.cast<Uint8>().asTypedList(data.length).setAll(0, data);
if (bindings.HMAC_Update(ctx, dataPtr.cast(), data.length) != 1) {
throw OpenSslException('HMAC_Update failed');
}
}
final outPtr = arena<UnsignedChar>(64);
final outLenPtr = arena<UnsignedInt>(1);
if (bindings.HMAC_Final(ctx, outPtr, outLenPtr) != 1) {
throw OpenSslException('HMAC_Final failed');
}
return Uint8List.fromList(
outPtr.cast<Uint8>().asTypedList(outLenPtr.value));
} finally {
arena.releaseAll();
}
}