receivedEncrypt static method

Future<List> receivedEncrypt(
  1. Cipher cipher,
  2. List args
)

A helper for implementing isolate channel RPC.

Implementation

static Future<List> receivedEncrypt(Cipher cipher, List args) async {
  SecretKey? secretKey;
  try {
    final clearText = args[0] as Uint8List;
    final secretKeyBytes = args[1] as Uint8List;
    secretKey = SecretKeyData(
      secretKeyBytes,
      overwriteWhenDestroyed: true,
    );
    final nonce = args[2] as Uint8List;
    final aad = args[3] as Uint8List;
    final secretBox = await cipher.encrypt(
      clearText,
      secretKey: secretKey,
      nonce: nonce,
      aad: aad,
    );
    return [
      null, // The first object in the returned list is error message.
      secretBox.cipherText,
      secretBox.mac.bytes,
    ];
  } catch (error, stackTrace) {
    // The first object in the returned list is error message.
    return ['Error: $error\n$stackTrace'];
  } finally {
    secretKey?.destroy();
  }
}