encrypt function

Stream<List<int>> encrypt(
  1. Stream<List<int>> payload,
  2. List<AgeRecipient> recipients, {
  3. AgeRandom random = const AgeRandom(),
  4. SimpleKeyPair? keyPair,
})

Implementation

Stream<List<int>> encrypt(
    Stream<List<int>> payload, List<AgeRecipient> recipients,
    {AgeRandom random = const AgeRandom(), SimpleKeyPair? keyPair}) async* {
  _logger.fine('Encrypting to ${recipients.length} recipients');
  final symmetricFileKey = random.bytes(16);
  final stanzas =
      await Future.wait<AgeStanza>(recipients.map((recipient) async {
    return AgePlugin.stanzaCreate(recipient, symmetricFileKey, keyPair);
  }));
  if (stanzas.isEmpty) {
    throw Exception('Could not encrypt to any recipient!');
  }
  final header = await AgeHeader.create(stanzas, symmetricFileKey);
  final payloadNonce = random.bytes(16);
  yield (await header.serialize()).codeUnits;
  yield [0x0a];
  yield* _encryptPayload(payload,
      symmetricFileKey: symmetricFileKey, payloadNonce: payloadNonce);
}