decryptBound method

Future<DecryptedPayload> decryptBound(
  1. Uint8List encrypted,
  2. PayloadBinding want,
  3. PeerId sender
)

Opens a ciphertext and checks that it was sealed for exactly the message it arrived as (want). A legacy ciphertext still opens and is reported as unbound.

Implementation

Future<DecryptedPayload> decryptBound(Uint8List encrypted, PayloadBinding want, PeerId sender) async {
  final box = _boxWith(await peerIdToX25519PublicKey(sender));
  if (encrypted.length > boundMagic.length && _hasMagic(encrypted)) {
    final plain = _open(box, Uint8List.sublistView(encrypted, boundMagic.length));
    if (plain != null) {
      final (got, payload) = PayloadBinding.split(plain);
      if (got != want) {
        throw BoundToAnotherMessageException(got);
      }
      return DecryptedPayload(Uint8List.fromList(payload), bound: true);
    }
    // A legacy nonce that happens to begin with the magic falls through.
  }
  final plain = _open(box, encrypted);
  if (plain == null) {
    throw PayloadDecryptException('decryption failed (wrong key or corrupted data)');
  }
  return DecryptedPayload(plain, bound: false);
}