Crypt.sha256 constructor
Creates a crypt using the SHA-256 algorithm.
Implements the SHA-256 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.sha256(String key, {int? rounds, String? salt}) {
final c = _sha256sha512Algorithm(crypto.sha256, 32, key,
providedRounds: rounds, providedSalt: salt);
final result = StringBuffer();
_encode_3bytes(result, c[0], c[10], c[20]);
_encode_3bytes(result, c[21], c[1], c[11]);
_encode_3bytes(result, c[12], c[22], c[2]);
_encode_3bytes(result, c[3], c[13], c[23]);
_encode_3bytes(result, c[24], c[4], c[14]);
_encode_3bytes(result, c[15], c[25], c[5]);
_encode_3bytes(result, c[6], c[16], c[26]);
_encode_3bytes(result, c[27], c[7], c[17]);
_encode_3bytes(result, c[18], c[28], c[8]);
_encode_3bytes(result, c[9], c[19], c[29]);
_encode_3bytes(result, c[31], c[30]);
_hash = result.toString();
_type = idSha256;
}