encryptString method

Future<SecretBox> encryptString(
  1. String clearText, {
  2. required SecretKey secretKey,
})

Converts a string to bytes using utf8 codec and then calls encrypt.

Implementation

Future<SecretBox> encryptString(
  String clearText, {
  required SecretKey secretKey,
}) async {
  final bytes = utf8.encode(clearText) as Uint8List;
  final secretBox = await encrypt(
    bytes,
    secretKey: secretKey,
    possibleBuffer: bytes,
  );

  // Overwrite `bytes` if it was not overwritten by the cipher.
  final cipherText = secretBox.cipherText;
  if (cipherText is! Uint8List ||
      !identical(bytes.buffer, cipherText.buffer)) {
    bytes.fillRange(0, bytes.length, 0);
  }

  return secretBox;
}