encryptStringWithAesGCM static method

Uint8List encryptStringWithAesGCM(
  1. String data,
  2. Uint8List key
)

Implementation

static Uint8List encryptStringWithAesGCM(String data, Uint8List key) {
  // Generate a random 96-bit nonce N
  final nonce = KeysHelper.generateRandomNonce(12);

  // Create an AES-GCM crypter
  final aesGcmCrypter = GCMBlockCipher(AESFastEngine());
  final params = AEADParameters(
    KeyParameter(key),
    128,
    nonce,
    Uint8List.fromList([]),
  );
  aesGcmCrypter.init(true, params);

  // Encrypt the data with the key F and nonce N obtaining CIPHERTEXT
  final dataToEncrypt = Uint8List.fromList(utf8.encode(data));
  final chiperText = aesGcmCrypter.process(dataToEncrypt);

  // Concatenate bytes of CIPHERTEXT and N
  final chiperTextWithNonce = nonce + chiperText;

  return Uint8List.fromList(chiperTextWithNonce);
}