encryptAes static method

Future<EncryptedContent> encryptAes(
  1. String data,
  2. Uint8List key,
  3. String name, [
  4. String? ivStr,
])

Implementation

static Future<EncryptedContent> encryptAes(
  String data,
  Uint8List key,
  String name, [
  String? ivStr,
]) async {
  Uint8List iv;
  if (ivStr != null) {
    iv = base64decodeUnpadded(ivStr);
  } else {
    iv = Uint8List.fromList(uc.secureRandomBytes(16));
  }
  // we need to clear bit 63 of the IV
  iv[8] &= 0x7f;

  final keys = deriveKeys(key, name);

  final plain = Uint8List.fromList(utf8.encode(data));
  final ciphertext = await uc.aesCtr.encrypt(plain, keys.aesKey, iv);

  final hmac = Hmac(sha256, keys.hmacKey).convert(ciphertext);

  return EncryptedContent(
    iv: base64.encode(iv),
    ciphertext: base64.encode(ciphertext),
    mac: base64.encode(hmac.bytes),
  );
}