cryptoSecretBoxEasy method

Uint8List cryptoSecretBoxEasy(
  1. Uint8List plaintext,
  2. Uint8List nonce,
  3. Uint8List sharedKey
)

Implementation

Uint8List cryptoSecretBoxEasy(
  Uint8List plaintext,
  Uint8List nonce,
  Uint8List sharedKey,
) {
  final ciphertextLength = plaintext.length + crypto_secretbox_MACBYTES;
  final plaintextPointer = calloc<ffi.Uint8>(plaintext.length);
  final ciphertextPointer = calloc<ffi.Uint8>(ciphertextLength);
  final noncePointer = calloc<ffi.Uint8>(crypto_secretbox_NONCEBYTES);
  final sharedKeyPointer = calloc<ffi.Uint8>(crypto_secretbox_KEYBYTES);

  try {
    // Fill nonce 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 < sharedKey.length; i++) {
      sharedKeyPointer.elementAt(i).value = sharedKey[i];
    }

    // Call crypto_secretbox_easy to encrypt the message
    final result = crypto_secretbox_easy(
      ciphertextPointer.cast<ffi.UnsignedChar>(),
      plaintextPointer.cast<ffi.UnsignedChar>(),
      plaintext.length,
      noncePointer.cast<ffi.UnsignedChar>(),
      sharedKeyPointer.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 secret box easy failed.');
      return Uint8List(0);
    }
  } finally {
    // Free allocated memory
    calloc.free(plaintextPointer);
    calloc.free(ciphertextPointer);
    calloc.free(noncePointer);
    calloc.free(sharedKeyPointer);
  }
}