deriveKeyBytes static method
Derives a key using Argon2 with custom parameters
password - The password to derive the key from
salt - The salt to use for key derivation
t - Time cost (iterations), must be >= 1
m - Memory cost (KB), must be >= 8*p
p - Parallelism, must be >= 1
dkLen - Length of derived key in bytes
variant - The Argon2 variant to use
Returns a Future
Implementation
static Future<Uint8List> deriveKeyBytes(
String password,
String salt, {
int t = _defaultT,
int m = _defaultM,
int p = _defaultP,
int dkLen = _defaultDkLen,
Argon2Variant variant = Argon2Variant.argon2id,
}) async {
// Validate parameters
if (t < 1) throw ArgumentError('t must be >= 1');
if (m < 8 * p) throw ArgumentError('m must be >= 8*p');
if (p < 1) throw ArgumentError('p must be >= 1');
if (dkLen < 1) throw ArgumentError('dkLen must be >= 1');
// Convert password and salt to bytes
final pwd = Uint8List.fromList(password.codeUnits);
final saltBytes = Uint8List.fromList(salt.codeUnits);
// Initialize h0
final h0 = await _initializeH0(pwd, saltBytes, t, m, p, dkLen, variant);
// Initialize memory matrix
final memory = await _initializeMemory(
h0,
pwd,
saltBytes,
t,
m,
p,
dkLen,
variant,
);
// Fill memory matrix
await _fillMemory(memory, pwd, saltBytes, t, m, p, variant);
// Finalize hash
final result = await _finalize(
memory,
h0,
pwd,
saltBytes,
t,
m,
p,
dkLen,
variant,
);
return result;
}