Pbkdf2 class abstract

PBKDF2 password hashing algorithm implemented in pure Dart.

About the algorithm

  • macAlgorithm can be any MacAlgorithm (such as Hmac.sha256()).
  • iterations should be at least 100 000 for reasonable security in password hashing. The higher the better.
  • bits should be at least 128 for reasonable security in password hashing.
  • PBKDF2 is a popular choice for password hashing, but much better algorithms exists (such as Argon2id).

Example

import 'package:better_cryptography/better_cryptography.dart';

Future<void> main() async {
  final pbkdf2 = Pbkdf2(
    macAlgorithm: Hmac.sha256(),
    iterations: 100000,
    bits: 128,
  );

  // Password we want to hash
  final secretKey = SecretKey([1,2,3]);

  // A random salt
  final nonce = [4,5,6];

  // Calculate a hash that can be stored in the database
  final newSecretKey = await pbkdf2.deriveKey(
    secretKey: secretKey,
    nonce: nonce,
  );
  final newSecretKeyBytes = await newSecretKey.extractBytes();
  print('Result: $newSecretKeyBytes');
}
Inheritance
Implementers

Constructors

Pbkdf2.new({required MacAlgorithm macAlgorithm, required int iterations, required int bits})
factory
Pbkdf2.constructor()
Constructor for classes that extend this class.
const

Properties

bits int
no setter
hashCode int
The hash code for this object.
no setteroverride
iterations int
no setter
macAlgorithm MacAlgorithm
no setter
runtimeType Type
A representation of the runtime type of the object.
no setterinherited

Methods

deriveKey({required SecretKey secretKey, required List<int> nonce}) Future<SecretKey>
Generates a new secret key from a secret key and a nonce.
override
noSuchMethod(Invocation invocation) → dynamic
Invoked when a nonexistent method or property is accessed.
inherited
toString() String
A string representation of this object.
override

Operators

operator ==(Object other) bool
The equality operator.
override