calculateMacSync method

  1. @override
Mac calculateMacSync(
  1. List<int> input, {
  2. required SecretKeyData secretKeyData,
  3. List<int> nonce = const <int>[],
  4. List<int> aad = const <int>[],
})
override

Synchronous version of calculateMac.

Throws UnsupportedError if hashAlgorithm does not support synchronous evaluation (it's not a subclass of DartHashAlgorithmMixin).

For other parameters, see documentation of calculateMac.

Implementation

@override
Mac calculateMacSync(
  List<int> input, {
  required SecretKeyData secretKeyData,
  List<int> nonce = const <int>[],
  List<int> aad = const <int>[],
}) {
  final hashAlgorithm = this.hashAlgorithm as DartHashAlgorithmMixin;
  if (aad.isNotEmpty) {
    throw ArgumentError.value(aad, 'aad', 'AAD is not supported');
  }
  var hmacKey = secretKeyData.bytes;
  if (hmacKey.isEmpty) {
    throw ArgumentError.value(
      secretKeyData,
      'secretKeyData',
      'Must be non-empty',
    );
  }

  final blockLength = hashAlgorithm.blockLengthInBytes;
  if (hmacKey.length > blockLength) {
    hmacKey = hashAlgorithm.hashSync(hmacKey).bytes;
  }

  // Inner hash
  final innerPadding = Uint8List(blockLength);
  _preparePadding(innerPadding, hmacKey, 0x36);
  final innerInput = Uint8List(innerPadding.length + input.length);
  innerInput.setAll(0, innerPadding);
  innerInput.setAll(innerPadding.length, input);
  final innerHash = hashAlgorithm.hashSync(innerInput);

  // Outer hash
  final outerPadding = Uint8List(blockLength);
  _preparePadding(outerPadding, hmacKey, 0x5c);
  final outerInput = Uint8List(outerPadding.length + innerHash.bytes.length);
  outerInput.setAll(0, outerPadding);
  outerInput.setAll(outerPadding.length, innerHash.bytes);
  final outerHash = hashAlgorithm.hashSync(outerInput);

  return Mac(outerHash.bytes);
}