decryptString method

Future<String> decryptString(
  1. SecretBox secretBox
)

Decrypts a string.

The decrypted bytes are converted to string using utf8 codec.

Example

In this example, we use Chacha20.poly1305Aead:

import 'package:cryptography/cryptography.dart';

Future<void> main() async {
  final cipher = Chacha20.poly1305Aead();
  final secretKey = await cipher.newSecretKey();
  final wand = await cipher.newCipherWandFromSecretKey(secretKey);

  // Encrypt
  final secretBox = await wand.encryptString('Hello, world!');
  print('Nonce: ${secretBox.nonce}');
  print('Cipher text: ${secretBox.cipherText}');
  print('MAC: ${secretBox.mac.bytes}');

  // Decrypt
  final clearText = await wand.decryptString(secretBox);
  print('Clear text: $clearText');
}

Implementation

Future<String> decryptString(SecretBox secretBox) async {
  final clearText = await decrypt(secretBox);
  try {
    return utf8.decode(clearText);
  } finally {
    // Cut the amount of possibly sensitive data in the heap.
    // This should be a cheap operation relative to decryption.
    clearText.fillRange(0, clearText.length, 0);
  }
}