encryptSymmetricRaw function

Uint8List encryptSymmetricRaw(
  1. Uint8List buffer,
  2. String passphrase
)

Encrypts the given data using NaCl SecretBox and returns a Uint8List containing nonce[24] + cipherText[]. The 24 first bytes represent the nonce, and the rest of the buffer contains the cipher text.

Implementation

Uint8List encryptSymmetricRaw(Uint8List buffer, String passphrase) {
  final key = utf8.encode(passphrase);
  final keyDigest =
      sha256.convert(key); // Hash the passphrase to get a 32 byte key
  final box = SecretBox(keyDigest.bytes);
  final encrypted = box.encrypt(buffer);

  return Uint8List.fromList(encrypted.toList());
}