encrypt static method

Uint8List encrypt(
  1. dynamic value,
  2. String password, {
  3. KDF? kdf,
})

Encrypts a value using AES/CBC/PKCS7 KDF is Sha256KDF if not specified

Implementation

static Uint8List encrypt(dynamic value, String password, {KDF? kdf}) {
  kdf = kdf ?? Sha256KDF();
  Uint8List valBytes;
  if (value is String) {
    valBytes = NanoHelpers.hexToBytes(value);
  } else if (value is Uint8List) {
    valBytes = value;
  } else {
    throw Exception('Seed should be a string or uint8list');
  }

  // Generate a random salt
  Uint8List salt = Uint8List(8);
  Random rng = Random.secure();
  for (int i = 0; i < 8; i++) {
    salt[i] = rng.nextInt(255);
  }

  KeyIV keyInfo = kdf.deriveKey(password, salt: salt);

  Uint8List seedEncrypted =
      AesCbcPkcs7.encrypt(valBytes, key: keyInfo.key, iv: keyInfo.iv);

  return NanoHelpers.concat(
      [NanoHelpers.stringToBytesUtf8("Salted__"), salt, seedEncrypted]);
}