decryptSymmetricRaw function

Uint8List decryptSymmetricRaw(
  1. Uint8List encryptedBuffer,
  2. String passphrase
)

Decrypts a byte array containing nonce[24] + cipherText[] using NaCl SecretBox: https://github.com/ilap/pinenacl-dart#a-secret-key-encryption-example

Implementation

Uint8List decryptSymmetricRaw(Uint8List encryptedBuffer, 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 = EncryptedMessage(
      cipherText: encryptedBuffer.sublist(24),
      nonce: encryptedBuffer.sublist(0, 24));
  return box.decrypt(encrypted);
}