askarKeyAeadEncrypt function

AskarResult<EncryptedBuffer> askarKeyAeadEncrypt(
  1. LocalKeyHandle localKeyHandle,
  2. Uint8List message, {
  3. Uint8List? nonce,
  4. Uint8List? aad,
})

Implementation

AskarResult<EncryptedBuffer> askarKeyAeadEncrypt(
  LocalKeyHandle localKeyHandle,
  Uint8List message, {
  Uint8List? nonce,
  Uint8List? aad,
}) {
  Pointer<NativeEncryptedBuffer> outPtr = calloc<NativeEncryptedBuffer>();

  Pointer<NativeByteBuffer> messagePtr = nullptr;
  Pointer<NativeByteBuffer> noncePtr = nullptr;
  Pointer<NativeByteBuffer> aadPtr = nullptr;

  try {
    messagePtr = bytesListToByteBuffer(message);
    noncePtr = bytesListToByteBuffer(nonce);
    aadPtr = bytesListToByteBuffer(aad);

    final funcResult = nativeAskarKeyAeadEncrypt(
      localKeyHandle.toInt(),
      messagePtr.ref,
      noncePtr.ref,
      aadPtr.ref,
      outPtr,
    );

    final errorCode = ErrorCode.fromInt(funcResult);

    final value =
        (errorCode == ErrorCode.success)
            ? readNativeEncryptedBuffer(outPtr.ref)
            : EncryptedBuffer(Uint8List.fromList([]), 0, 0);

    return AskarResult<EncryptedBuffer>(errorCode, value);
  } finally {
    freeEncryptedBufferPointer(outPtr);
    freeByteBufferPointer(messagePtr);
    freeByteBufferPointer(noncePtr);
    freeByteBufferPointer(aadPtr);
  }
}