encryptCborPhrase function

Future<Uint8List> encryptCborPhrase(
  1. String phrase, {
  2. String? password,
  3. Map<String, dynamic>? options,
})

Implementation

Future<Uint8List> encryptCborPhrase(
  String phrase, {
  String? password,
  Map<String, dynamic>? options,
}) async {
  final String salt = randomAsHex(64);
  final List<int> iv = randomAsU8a(16);
  final String kdf = options?['kdf'] is String ? options!['kdf'] : 'scrypt';
  final int level = options?['level'] is int ? options!['level'] : 8192;
  final int n = kdf == 'pbkdf2' ? 262144 : level;

  final Map<String, dynamic> kdfParams = {
    'salt': salt,
    'n': n,
    'c': n,
    'r': 8,
    'p': 1,
    'dklen': 32,
  };

  final deriveKeyResult = await nativeDeriveKey(
    kdf: kdf,
    iv: iv,
    message: phrase,
    useCipherText: null,
    kdfParams: kdfParams,
    passphrase: password,
    salt: salt,
  );

  return cborEncode({
    'ciphertext': deriveKeyResult.cipherText,
    'cipherparams': {'iv': iv},
    'cipher': _algoIdentifier.plainToU8a(),
    'kdf': kdf.plainToU8a(),
    'kdfparams': cborEncode(kdfParams),
    'mac': deriveKeyResult.mac.toU8a(),
  });
}