encrypt static method

String encrypt(
  1. String plaintext, [
  2. String encrypter(
    1. String plaintext
    )?
])

Encrypt the data to store locally.

plaintext is the data to encrypt.

encrypter is an optional function to encrypt the data with. This will be used in addition to the default encryption. This will be performed before the default encryption.

Implementation

static String encrypt(String plaintext,
    [String Function(String plaintext)? encrypter]) {
  String semiPlaintext;

  // Encrypt the data with addition encrypter if provided.
  if (encrypter != null) {
    semiPlaintext = encrypter(plaintext);
  } else {
    semiPlaintext = plaintext;
  }

  // First, convert the data string to bytes.
  List<int> dataBytes = utf8.encode(semiPlaintext);

  // Then, encode those bytes.
  String base64Data = base64.encode(dataBytes);

  // Finally, return a scrambled version of that encoding.
  String scrambled = _scramble(base64Data);

  // Add salt to the scrambled data.
  String salted = _addSalt(scrambled);

  // Encode the salted data.
  base64Data = base64.encode(utf8.encode(salted));

  // Scramble the encoded data.
  scrambled = _scramble(base64Data);

  return scrambled;
}