Crypt.sha512 constructor

Crypt.sha512(
  1. String key, {
  2. int? rounds,
  3. String? salt,
})

Creates a crypt using the SHA-512 algorithm.

Implements the SHA-512 password hashing as specified by "Unix crypt using SHA-256 and SHA-512", by Ulrich Drepper, version: 0.4 2008-4-3. www.akkadia.org/drepper/SHA-crypt.txt

The key is the value being hashed.

If rounds is not provided, the default of 5000 is used and the rounds is not explicitly included in the result. Rounds less than 1000 results in 1000 being used. Rounds greater than 999,999,999 results in 999,999,999 being used. These numbers are defined by the specification.

If the salt is not provided, a random 16-character salt is generated. Otherwise the provided value is used as the salt, up to 16 characters. If a longer salt is provided, the extra characters are ignored. Shorter salts (especially the empty string) are not recommended, since they reduce the security of the generated hash. An empty string is a valid salt, but obviously should not be used.

Throws an ArgumentError if the salt contains a dollar sign.

Throws UnsupportedError if a cryptographically secure random number is required to generate the salt (i.e. cryptographicallySecureSalts is true) and it is not supported by the platform this is running on.

Implementation

Crypt.sha512(String key, {int? rounds, String? salt}) {
  final c = _sha256sha512Algorithm(crypto.sha512, 64, key,
      providedRounds: rounds, providedSalt: salt);

  final result = StringBuffer();

  _encode_3bytes(result, c[0], c[21], c[42]);
  _encode_3bytes(result, c[22], c[43], c[1]);
  _encode_3bytes(result, c[44], c[2], c[23]);
  _encode_3bytes(result, c[3], c[24], c[45]);
  _encode_3bytes(result, c[25], c[46], c[4]);
  _encode_3bytes(result, c[47], c[5], c[26]);
  _encode_3bytes(result, c[6], c[27], c[48]);
  _encode_3bytes(result, c[28], c[49], c[7]);
  _encode_3bytes(result, c[50], c[8], c[29]);
  _encode_3bytes(result, c[9], c[30], c[51]);
  _encode_3bytes(result, c[31], c[52], c[10]);
  _encode_3bytes(result, c[53], c[11], c[32]);
  _encode_3bytes(result, c[12], c[33], c[54]);
  _encode_3bytes(result, c[34], c[55], c[13]);
  _encode_3bytes(result, c[56], c[14], c[35]);
  _encode_3bytes(result, c[15], c[36], c[57]);
  _encode_3bytes(result, c[37], c[58], c[16]);
  _encode_3bytes(result, c[59], c[17], c[38]);
  _encode_3bytes(result, c[18], c[39], c[60]);
  _encode_3bytes(result, c[40], c[61], c[19]);
  _encode_3bytes(result, c[62], c[20], c[41]);
  _encode_3bytes(result, c[63]);

  _hash = result.toString();
  _type = idSha512;
}