Pbkdf2 class abstract

PBKDF2 password hashing algorithm implemented in pure Dart.

DartPbkdf2 is the pure Dart implementation of the PBKDF2 algorithm. It's used when no faster implementation is available.

In browsers, "package:cryptography" will automatically attempt to use Web Crypto API, which has very good PBKDF2 performance.

Flutter developers should add cryptography_flutter, as a dependency for the best possible PBKDF2 performance.

Things to know

  • macAlgorithm can be any MacAlgorithm (such as Hmac.sha256()).
  • iterations is the number of times output of hashing will be used as input of the next hashing iteration. The idea of password hashing algorithms is to make password hashing as slow as possible so the higher the better. A good value is usually at least 10 000.
  • bits is the number of bits you want as output. A good value may be 256 bits (32 bytes).
  • PBKDF2 is a popular choice for password hashing, but much better algorithms exists (such as Argon2id).

Example

import 'package:cryptography/cryptography.dart';

Future<void> main() async {
  final pbkdf2 = Pbkdf2(
    macAlgorithm: Hmac.sha256(),
    iterations: 10000, // 20k iterations
    bits: 256, // 256 bits = 32 bytes output
  );

  // Calculate a hash that can be stored in the database
  final newSecretKey = await pbkdf2.deriveKeyFromPassword(
    // Password given by the user.
    password: 'qwerty',

    // Nonce (also known as "salt") should be some random sequence of
    // bytes.
    //
    // You should have a different nonce for each user in the system
    // (which you store in the database along with the hash).
    // If you can't do that for some reason, choose a random value not
    // used by other applications.
    nonce: const [1,2,3],
  );

  final secretKeyBytes = await secretKey.extractBytes();
  print('Result: $secretKeyBytes');
}
Inheritance
Implementers

Constructors

Pbkdf2({required MacAlgorithm macAlgorithm, required int iterations, required int bits})
Constructs PBKDF2 with any MacAlgorithm.
factory
Pbkdf2.constructor()
Constructor for subclasses.
const
Pbkdf2.hmacSha256({required int iterations, required int bits})
Constructs PBKDF2 with Hmac.sha256.
factory

Properties

bits int
Number of bits that will be returned by deriveKey method.
no setter
hashCode int
The hash code for this object.
no setteroverride
iterations int
Number of iterations.
no setter
macAlgorithm MacAlgorithm
MAC algorithm.
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.
inherited
deriveKeyFromPassword({required String password, required List<int> nonce}) Future<SecretKey>
Generates a new secret key from a password and a nonce.
inherited
noSuchMethod(Invocation invocation) → dynamic
Invoked when a nonexistent method or property is accessed.
inherited
toString() String
A string representation of this object.
override
toSync() DartPbkdf2
Returns a pure Dart implementation of PBKDF2 with the same parameters.

Operators

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