hashpw method
Hash a password using the OpenBSD bcrypt scheme.
Returns the hashed password
Implementation
String hashpw(String password, String salt) {
String real_salt;
Int8List passwordb, saltb, hashed;
String minor = new String.fromCharCodes([0]);
int rounds, off = 0;
StringBuffer rs = new StringBuffer();
if (salt[0] != '\$' || salt[1] != '2') {
throw "Invalid salt version";
}
if (salt[2] == '\$') {
off = 3;
} else {
minor = salt[2];
if ((minor != 'a' && minor != 'b' && minor != 'y') || salt[3] != '\$') {
throw "Invalid salt revision";
}
off = 4;
}
// Extract number of rounds
if (salt[off + 2].codeUnitAt(0) > '\$'.codeUnitAt(0)) {
throw "Missing salt rounds";
}
rounds = int.parse(salt.substring(off, off + 2));
real_salt = salt.substring(off + 3, off + 25);
List<int> charCodes = new Utf8Encoder().convert(
"$password${(minor.codeUnitAt(0) >= 'a'.codeUnitAt(0) ? '\u0000' : '')}");
passwordb = new Int8List(charCodes.length);
for (var i = 0; i < charCodes.length; i++) {
passwordb[i] = charCodes[i];
}
saltb = _decodeBase64(real_salt, _bcryptSaltLen);
hashed = this._cryptRaw(passwordb, saltb, rounds);
rs.write("\$2");
if (minor.codeUnitAt(0) >= 'a'.codeUnitAt(0)) {
rs.write(minor);
}
rs.write("\$");
if (rounds < 10) {
rs.write("0");
}
rs.write(rounds.toString());
rs.write("\$");
rs.write(_encodeBase64(saltb, saltb.length));
rs.write(_encodeBase64(hashed, _bf_crypt_ciphertext.length * 4 - 1));
return rs.toString();
}