cryptoBoxEasy method

Uint8List cryptoBoxEasy(
  1. Uint8List plaintext,
  2. Uint8List nonce,
  3. KeyPair keyPair
)

Implementation

Uint8List cryptoBoxEasy(
  Uint8List plaintext,
  Uint8List nonce,
  KeyPair keyPair,
) {
  final ciphertextLength = plaintext.length + crypto_box_MACBYTES;
  final ciphertextPointer = calloc<ffi.Uint8>(ciphertextLength);
  final plaintextPointer = calloc<ffi.Uint8>(plaintext.length);
  final noncePointer = calloc<ffi.Uint8>(crypto_secretbox_NONCEBYTES);
  final pkPointer = calloc<ffi.Uint8>(crypto_box_PUBLICKEYBYTES);
  final skPointer = calloc<ffi.Uint8>(crypto_box_SECRETKEYBYTES);

  // Fill nonce, publicKey, and secretKey with appropriate values
  for (var i = 0; i < plaintext.length; i++) {
    plaintextPointer.elementAt(i).value = plaintext[i];
  }
  for (var i = 0; i < nonce.length; i++) {
    noncePointer.elementAt(i).value = nonce[i];
  }
  for (var i = 0; i < keyPair.pk.length; i++) {
    pkPointer.elementAt(i).value = keyPair.pk[i];
  }
  for (var i = 0; i < keyPair.sk.length; i++) {
    skPointer.elementAt(i).value = keyPair.sk[i];
  }

  try {
    // Call crypto_box_easy to encrypt the message
    final result = crypto_box_easy(
      ciphertextPointer.cast<ffi.UnsignedChar>(),
      plaintextPointer.cast<ffi.UnsignedChar>(),
      plaintext.length,
      noncePointer.cast<ffi.UnsignedChar>(),
      pkPointer.cast<ffi.UnsignedChar>(),
      skPointer.cast<ffi.UnsignedChar>(),
    );

    if (result == 0) {
      final ciphertextList = ciphertextPointer.asTypedList(ciphertextLength);
      // Clone the original list
      return Uint8List.fromList(List.from(ciphertextList));
    } else {
      debugPrint('[Lazysodium] Crypto box easy failed.');
      return Uint8List(0);
    }
  } finally {
    // Free allocated memory
    calloc.free(plaintextPointer);
    calloc.free(ciphertextPointer);
    calloc.free(noncePointer);
    calloc.free(pkPointer);
    calloc.free(skPointer);
  }
}