encrypt function

Uint8List encrypt({
  1. required Uint8List sign1,
  2. required Object? msgToAuth,
  3. required PublicKey recipient,
  4. required Uint8List domain,
  5. required Padding padding,
})

Encrypts an already-signed COSE_Sign1 to a recipient.

For most use cases, prefer seal which signs and encrypts in one step. Use this only when re-encrypting a message (from decrypt) to a different recipient without access to the original signer's key.

  • sign1: The COSE_Sign1 structure (e.g., from decrypt)
  • msgToAuth: The same additional authenticated data used during sealing
  • recipient: The xHPKE public key to encrypt to
  • domain: Application domain for HPKE key derivation
  • padding: Sender's policy for zeros after the signed envelope

Returns the serialized COSE_Encrypt0 structure. Throws if msgToAuth does not encode into the supported CBOR subset. A padded size beyond what the platform can address surfaces as the bridge's panic exception.

Implementation

Uint8List encrypt({
  required Uint8List sign1,
  required Object? msgToAuth,
  required xhpke.PublicKey recipient,
  required Uint8List domain,
  required Padding padding,
}) {
  final nativePadding = padding._native;
  try {
    return ffi.coseEncrypt(
      sign1: sign1,
      msgToAuth: _encode(msgToAuth),
      recipient: recipient.inner,
      domain: domain,
      padding: nativePadding,
    );
  } finally {
    nativePadding.dispose();
  }
}