hmac function

Future<Uint8List> hmac(
  1. dynamic key,
  2. String message
)

Implementation

Future<Uint8List> hmac(dynamic key, String message) async {
  Uint8List keyBuffer;

  if (key is String) {
    keyBuffer = Uint8List.fromList(utf8.encode(key));
  } else if (key is Uint8List) {
    keyBuffer = key;
  } else {
    throw ArgumentError('Invalid key type');
  }

  Uint8List messageBuffer = Uint8List.fromList(utf8.encode(message));

  Hmac hmac = Hmac(sha256, keyBuffer);
  Digest digest = hmac.convert(messageBuffer);

  return Uint8List.fromList(digest.bytes);
}