Argon2Context constructor
Argon2Context({})
Creates a context for Argon2 password hashing
Required Parameters:
iterationsNumber of iterations to perform.parallelismDegree of parallelism (i.e. number of threads).memorySizeKBAmount of memory (in kibibytes) to use.
Optional Parameters:
saltSalt (16 bytes recommended for password hashing). If absent, a 64 bytes random salt is generated.hashLengthDesired number of returned bytes. Default: 32.keyAdditional key.personalizationArbitrary additional data.versionAlgorithm version; Default:Argon2Version.v13,typeArgon2 type; Default:Argon2Type.argon2id.
Implementation
factory Argon2Context({
required int iterations,
required int parallelism,
required int memorySizeKB,
List<int>? key,
List<int>? salt,
List<int>? personalization,
int? hashLength,
Argon2Version version = Argon2Version.v13,
Argon2Type type = Argon2Type.argon2id,
}) {
hashLength ??= _defaultHashLength;
if (hashLength < _minDigestSize) {
throw ArgumentError('The tag length must be at least $_minDigestSize');
}
if (hashLength > _maxDigestSize) {
throw ArgumentError('The tag length must be at most $_maxDigestSize');
}
if (parallelism < _minParallelism) {
throw ArgumentError('The parallelism must be at least $_minParallelism');
}
if (parallelism > _maxParallelism) {
throw ArgumentError('The parallelism must be at most $_maxParallelism');
}
if (iterations < _minIterations) {
throw ArgumentError('The iterations must be at least $_minIterations');
}
if (iterations > _maxIterations) {
throw ArgumentError('The iterations must be at most $_maxIterations');
}
if (memorySizeKB < (parallelism << 3)) {
throw ArgumentError('The memory size must be at least 8 * parallelism');
}
if (memorySizeKB > _maxMemory) {
throw ArgumentError('The memorySizeKB must be at most $_maxMemory');
}
salt ??= randomBytes(64);
if (salt.length < _minSaltSize) {
throw ArgumentError('The salt must be at least $_minSaltSize bytes long');
}
if (salt.length > _maxSaltSize) {
throw ArgumentError('The salt must be at most $_maxSaltSize bytes long');
}
if (key != null) {
if (key.length < _minKeySize) {
throw ArgumentError('The key must be at least $_minKeySize bytes long');
}
if (key.length > _maxKeySize) {
throw ArgumentError('The key must be at most $_maxKeySize bytes long');
}
}
if (personalization != null) {
if (personalization.length < _minAD) {
throw ArgumentError('The extra data must be at least $_minAD bytes');
}
if (personalization.length > _maxAD) {
throw ArgumentError('The extra data must be at most $_maxAD');
}
}
int segments = memorySizeKB ~/ (_slices * parallelism);
int columns = _slices * segments;
int blocks = parallelism * _slices * segments;
return Argon2Context._(
salt: salt,
version: version,
type: type,
hashLength: hashLength,
passes: iterations,
slices: _slices,
lanes: parallelism,
memorySizeKB: memorySizeKB,
columns: columns,
segments: segments,
blocks: blocks,
key: key,
personalization: personalization,
);
}