newMacSinkSync method

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

Implementation

@override
DartMacSink newMacSinkSync({
  required SecretKeyData secretKeyData,
  List<int> nonce = const <int>[],
  List<int> aad = const <int>[],
}) {
  if (aad.isNotEmpty) {
    throw ArgumentError.value(aad, 'aad', 'AAD is not supported');
  }

  final hashAlgorithm = this.hashAlgorithm;
  final blockLength = hashAlgorithm.blockLengthInBytes;

  //
  // secret
  //
  var hmacKey = secretKeyData.bytes;
  if (hmacKey.isEmpty) {
    throw ArgumentError.value(
      secretKeyData,
      'secretKeyData',
      'SecretKeyData bytes must be non-empty',
    );
  }
  if (hmacKey.length > blockLength) {
    final dartHashAlgorithm = hashAlgorithm as DartHashAlgorithmMixin;
    hmacKey = dartHashAlgorithm.hashSync(hmacKey).bytes;
  }

  //
  // inner sink
  //
  final innerSink = hashAlgorithm.newHashSink();
  final innerPadding = Uint8List(blockLength);
  _preparePadding(innerPadding, hmacKey, 0x36);
  innerSink.add(innerPadding);

  //
  // outer sink
  //
  final outerSink = hashAlgorithm.newHashSink();
  final outerPadding = Uint8List(blockLength);
  _preparePadding(outerPadding, hmacKey, 0x5c);
  outerSink.add(outerPadding);

  return _HmacSink(innerSink, outerSink);
}