One-way string hashing for salted passwords using the Unix crypt format.

This package implements the SHA-256 crypt hash and SHA-512 crypt hash, as specified by "Unix crypt using SHA-256 and SHA-512" (version: 0.6 2016-08-31).

Crypt format strings

It can produce crypt formatted string like:




Where the leading "$5$" indicates this is a SHA-256 crypt, and is followed a number of fields separated by the dollar sign: a optional the number of rounds, the salt and the hash value. A leading "$6$" indicates it is a SHA-512 crypt.

When SHA-256 or SHA-512 is being used, the default number of rounds is 5000 (as defined by the specification).

Note: different systems use the crypt formatted string differently. For example, as the value of the userPassword attribute in an LDAP posixAccount entry, "{crypt}" needs to be prepended to it.


import 'package:crypt/crypt.dart';

void main() {
  // Creating crypt strings

  // Default rounds and random salt generated
  final c1 = Crypt.sha256('p@ssw0rd');

  // Random salt generated
  final c2 = Crypt.sha256('p@ssw0rd', rounds: 10000);

  // Default rounds
  final c3 = Crypt.sha256('p@ssw0rd', salt: 'abcdefghijklmnop');

  // No defaults used
  final c4 = Crypt.sha256('p@ssw0rd', rounds: 10000,
                          salt: 'abcdefghijklmnop');

  // SHA-512
  final d1 = Crypt.sha512('p@ssw0rd');


  // Comparing a value to a crypt hash

  for (final hashString in [
  ]) {
    // Parse the crypt string: this extracts the type, rounds and salt
    final h = Crypt(hashString);

    final correctValue = 'p@ssw0rd';
    final wrongValue = '123456';

    if (!h.match(correctValue)) {
      print('Error: unexpected non-match: $correctValue');

    if (h.match(wrongValue)) {
      print('Error: unexpected match: $wrongValue');

The above example produced the following output:


Features and bugs

Salt generation does not use a cryptographically secure random number generator. If this is a concern, generate your own salt and pass it in as one of the parameters.

Version 3.0.0 depends on the Dart crypto package, version 2.1.4 or newer, which has support for SHA-512. If you need to use an older version of crypto, use version 2.0.0 of this package -- but that older version won't have support for SHA-512 crypt strings.

Please file feature requests and bugs at the GitHub issue tracker.


One-way string hashing for salted passwords using the Unix crypt format.