newMacSink method

Future<MacSink> newMacSink({
  1. required SecretKey secretKey,
  2. List<int> nonce = const <int>[],
  3. List<int> aad = const <int>[],
})

Constructs a sink for calculating a Mac.

The parameter secretKey must be non-empty.

The parameter nonce can be const <int>[].

The parameter aad is Associated Authenticated Data (AAD). It can be empty. If it's non-empty and the algorithm does not support AAD, the the method throws ArgumentError.

Example

import 'package:cryptography/cryptography.dart';

void main() {
  final secretKey = SecretKey([1,2,3]);

  // Create a sink
  final sink = await Hmac.sha256().newMacSink(
    secretKey: secretKey,
  );

  // Add chunks of data
  sink.add([4,5,6]);
  sink.add([7,8]);

  // Close
  sink.close();

  // We now have a MAC
  final mac = await sink.mac();

  print('MAC: ${mac.bytes');
}

Implementation

Future<MacSink> newMacSink({
  required SecretKey secretKey,
  List<int> nonce = const <int>[],
  List<int> aad = const <int>[],
}) async {
  final secretKeyBytes = await secretKey.extractBytes();
  if (secretKeyBytes.isEmpty) {
    throw ArgumentError.value(
      secretKey,
      'secretKey',
      'SecretKey bytes must be non-empty',
    );
  }
  if (aad.isNotEmpty && !supportsAad) {
    throw ArgumentError.value(
      aad,
      'aad',
      'AAD is not supported',
    );
  }
  return _MacSink(
    this,
    secretKey: secretKey,
    nonce: nonce,
    aad: aad,
  );
}