encryptCborPhrase function
Implementation
Future<Uint8List> encryptCborPhrase(
String phrase,
String password, [
Map<String, dynamic>? options,
]) async {
final String salt = randomAsHex(64);
final List<int> iv = randomAsU8a(16);
String kdf = 'scrypt';
int level = 8192;
int n = kdf == 'pbkdf2' ? 262144 : level;
if (options == null) {
kdf = 'scrypt';
level = 8192;
n = kdf == 'pbkdf2' ? 262144 : level;
} else {
kdf = options['kdf'] is String ? options['kdf'] : 'scrypt';
level = options['level'] is int ? options['level'] : 8192;
n = kdf == 'pbkdf2' ? 262144 : level;
}
final Map<String, dynamic> kdfParams = {
'salt': salt,
'n': n,
'r': 8,
'p': 1,
'dklen': 32
};
final List<int> encodedPassword = utf8.encode(password);
final _KeyDerivator derivator = getDerivedKey(kdf, kdfParams);
final List<int> derivedKey = derivator.deriveKey(encodedPassword);
final List<int> ciphertextBytes = _encryptPhrase(
derivator,
Uint8List.fromList(encodedPassword),
Uint8List.fromList(iv),
phrase,
);
final List<int> macBuffer = derivedKey.sublist(16, 32) +
ciphertextBytes +
iv +
ALGO_IDENTIFIER.codeUnits;
final Uint8List mac = SHA256()
.update(Uint8List.fromList(derivedKey))
.update(macBuffer)
.digest();
return cborEncode({
'ciphertext': ciphertextBytes,
'cipherparams': {
'iv': iv,
},
'cipher': ALGO_IDENTIFIER.plainToU8a(),
'kdf': kdf.plainToU8a(),
'kdfparams': cborEncode(kdfParams),
'mac': mac,
});
}