askarKeyCryptoBoxOpen function

AskarResult<Uint8List> askarKeyCryptoBoxOpen(
  1. LocalKeyHandle recipKey,
  2. LocalKeyHandle senderKey,
  3. Uint8List message,
  4. Uint8List nonce,
)

Implementation

AskarResult<Uint8List> askarKeyCryptoBoxOpen(
  LocalKeyHandle recipKey,
  LocalKeyHandle senderKey,
  Uint8List message,
  Uint8List nonce,
) {
  Pointer<NativeSecretBuffer> secretBufferPtr = calloc<NativeSecretBuffer>();
  Pointer<NativeByteBuffer> messageByteBufferPtr = nullptr;
  Pointer<NativeByteBuffer> nonceByteBufferPtr = nullptr;
  try {
    messageByteBufferPtr = bytesListToByteBuffer(message);
    nonceByteBufferPtr = bytesListToByteBuffer(nonce);

    final result = nativeAskarKeyCryptoBoxOpen(
      recipKey.toInt(),
      senderKey.toInt(),
      messageByteBufferPtr.ref,
      nonceByteBufferPtr.ref,
      secretBufferPtr,
    );

    final errorCode = ErrorCode.fromInt(result);

    final Uint8List value =
        (errorCode == ErrorCode.success)
            ? secretBufferToBytesList(secretBufferPtr.ref)
            : Uint8List(0);
    return AskarResult<Uint8List>(errorCode, value);
  } finally {
    freeSecretBufferPointer(secretBufferPtr);
    freeByteBufferPointer(messageByteBufferPtr);
    freeByteBufferPointer(nonceByteBufferPtr);
  }
}