receivedDecrypt static method

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

A helper for implementing isolate channel RPC.

Implementation

static Future<List> receivedDecrypt(Cipher cipher, List args) async {
  SecretKey? secretKey;
  try {
    final cipherText = args[0] as Uint8List;
    final secretKeyBytes = args[1] as Uint8List;
    secretKey = SecretKeyData(
      secretKeyBytes,
      overwriteWhenDestroyed: true,
    );
    final nonce = args[2] as Uint8List;
    final macBytes = args[3] as Uint8List;
    final mac = Mac(macBytes);
    final aad = args[4] as Uint8List;
    final secretBox = SecretBox(
      cipherText,
      nonce: nonce,
      mac: mac,
    );
    final result = await cipher.decrypt(
      secretBox,
      secretKey: secretKey,
      aad: aad,
    );
    return [
      null, // The first object in the returned list is error message.
      result,
    ];
  } on SecretBoxAuthenticationError {
    return ['AUTHENTICATION_ERROR'];
  } on SecretBoxPaddingError {
    return ['PADDING_ERROR'];
  } catch (error, stackTrace) {
    // The first object in the returned list is error message.
    return ['Error: $error\n$stackTrace'];
  } finally {
    secretKey?.destroy();
  }
}