decryptMessage method

Future<String> decryptMessage(
  1. List<int> key,
  2. List<int> nonceBytes,
  3. List<int> macBytes,
  4. List<int> encryptedText,
)

Implementation

Future<String> decryptMessage(List<int> key, List<int> nonceBytes,
    List<int> macBytes, List<int> encryptedText) async {
  // Initialize algorithm
  final algorithm = cryptography.Chacha20.poly1305Aead();
  // Initialize SecretBox
  final mac = cryptography.Mac(macBytes);
  final secretBox =
      cryptography.SecretBox(encryptedText, nonce: nonceBytes, mac: mac);
  // Encode Key
  final cryptography.SecretKey secretKey = cryptography.SecretKey(key);
  // Decrypt
  final clearText = await algorithm
      .decrypt(
    secretBox,
    secretKey: secretKey,
  )
      .catchError((err) {
    Scp.getInstance().log(err);
  });
  // Return text
  return utf8.decode(clearText);
}