hmacSha256 function

Future<String> hmacSha256(
  1. String key,
  2. String text
)

Compute HMAC-SHA256 of text with key.

Implementation

Future<String> hmacSha256(String key, String text) async {
  final proc = await Process.run('sh', [
    '-c',
    'printf "%s" "\$1" | openssl dgst -sha256 -hmac "\$2" -hex',
    '--',
    text,
    key,
  ], stdoutEncoding: utf8);
  final output = (proc.stdout as String).trim();
  final idx = output.indexOf('= ');
  return idx >= 0 ? output.substring(idx + 2).trim() : output;
}