encryptV2 function

Future<EncryptedPrivateKeyModel> encryptV2({
  1. required List<int> data,
  2. required List<int> secret,
})

AES-GCM Encryption

Implementation

Future<EncryptedPrivateKeyModel> encryptV2(
    {required List<int> data, required List<int> secret}) async {
  final random = Random.secure();

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

  final algorithm = AesGcm.with256bits(nonceLength: AESGCMNonceSize);
  final nonce = algorithm.newNonce();

  final key = await hkdf(secret, salt);

  // Encrypt
  final secretBox = await algorithm.encrypt(
    data,
    secretKey: key,
    nonce: nonce,
  );
  List<int> combinedCipherBytes = [];
  combinedCipherBytes.addAll(secretBox.cipherText);
  combinedCipherBytes.addAll(secretBox.mac.bytes);
  return EncryptedPrivateKeyModel(
      ciphertext: bytesToHex(combinedCipherBytes),
      salt: bytesToHex(salt),
      nonce: bytesToHex(nonce));
}