encryptString function

Future<ASECombinedCipher> encryptString(
  1. String pt,
  2. ASEPublicKey pk
)

Encrypts a string using the hybrid PKE scheme It uses the public key (pk) to generate a shared secret and then uses AES-GCM to encrypt the plaintext (pt)

Implementation

Future<ASECombinedCipher> encryptString(String pt, ASEPublicKey pk) async {
  var r = PolyVec(
      List.generate(k, (_) => Poly(List.generate(n, (_) => rnd.nextInt(2)))));
  var kemCt = kemEncap(pk, r);
  var flatR = Uint8List.fromList(r.vec[0].coeffs);

  final salt = Uint8List(32);
  for (var i = 0; i < salt.length; i++) {
    salt[i] = rnd.nextInt(256);
  }

  var aesKey = await deriveAesKeyWithSalt(flatR, salt);

  final nonce = aesGcm.newNonce();
  final secretBox = await aesGcm.encrypt(
    utf8.encode(pt),
    secretKey: SecretKey(aesKey),
    nonce: nonce,
    aad: <int>[],
  );

  return ASECombinedCipher(
    kemCt,
    Uint8List.fromList(secretBox.nonce),
    Uint8List.fromList(secretBox.cipherText + secretBox.mac.bytes),
    Uint8List(0),
    salt,
  );
}