encrypt method

String encrypt({
  1. required String inp,
  2. required String iv,
  3. String? aad,
  4. int tagLength = 128,
})

Encrypts (with iv) and return in base 64.

IV should be base-64 encoded. Input should be a valid UTF-8 string.

aad is optional, ChaCha20-Poly1305 is secure without it.

Implementation

String encrypt(
    {required String inp,
    required String iv,
    String? aad,
    int tagLength = 128}) {
  var key = base64Decode(_key32);
  var ivLocal = base64Decode(iv);
  var localInput = utf8.encode(inp);
  var aadLocal = aad != null ? base64Decode(aad) : Uint8List(0);

  var cipherparams =
      AEADParameters(KeyParameter(key), tagLength, ivLocal, aadLocal);
  var cipher = ChaCha20Poly1305(ChaCha7539Engine(), Poly1305());
  cipher.init(true, cipherparams);

  var inter = cipher.process(localInput as Uint8List);
  return base64.encode(inter);
}