Bcrypt is an algorithm designed for hashing passwords, and only passwords; i.e. it is:
- not a high-speed, generic, hashing algorithm
- not a key derivation function (see PBDKF2, scrypt)
- computationally and memory expensive
- limited to passwords of 55 bytes
It was first described by Niels Provos and David Mazières in 1999 for OpenBSD.
It uses the Blowfish encryption algorithm, but with an "expensive key setup" modification, contained in the function EksBlowfishSetup
.
-
To hash a password:
hash := TBCrypt.HashPassword('correct battery horse staple'); //using default cost factor
-
To hash a password specifying your own cost factor:
hash := TBCrypt.HashPassword('correct battery horse staple', 14); //specify cost factor 14
-
To verify a password:
isPasswordValid := TBCrypt.CheckPassword('correct battery horse stapler', expectedHash);
By convention BCrypt outputs a hash as string such as:
$2a$11$EA6qjRCeBi8bGgs4rhfn8udEGKmu0ayrZYCEJqf6nNIoytowKFncm
The parts of the string are:
Value | Meaning | Notes |
---|---|---|
2a | Hash algorithm | "2a" = current version of BCrypt, "2" = obsolete version of BCrypt, "1" = MD5 |
11 | cost factor | Will iterate for 211=2,048 rounds. (Default is 11) |
Ro0CUfOqk6cXEKf3dyaM7O | Salt | 22 base64 encoded characters |
hSCvnwM9s4wIX9JeLapehKK5YdLxKcm | Hashed password | 31 base64 encoded characters |
Because the cost factor is stored with the hash output, bcrypt hashes are backwards and forwards compatible with changing the number of rounds. It also makes BCrypt extraordinarily convenient in that a random salt is automatically generated and stored for you (you don't have to worry about storing or retrieving it).
The current (3/21/2015) hard-coded default for cost is 11. This results in 211 = 2,048 rounds during the key setup.
3/14/2015 Intel Core i5-2500 CPU @ 3.50 GHz Delphi XE6 (32-bit, Release)
Cost | Iterations | Duration |
---|---|---|
8 | 256 iterations | 22.0 ms |
9 | 512 iterations | 43.3 ms |
10 | 1,024 iterations | 85.5 ms |
11 | 2,048 iterations | 173.3 ms |
12 | 4,096 iterations | 345.6 ms |
13 | 8,192 iterations | 694.3 ms |
14 | 16,384 iterations | 1,390.5 ms |
15 | 32,768 iterations | 2,781.4 ms |
16 | 65,536 iterations | 5,564.9 ms |
At the time of publication (1999), the default cost was 6 for a normal user and 8 for the superuser.
Created by Ian Boyd 5/3/2012
Public Domain
For more information, please refer to http://unlicense.org/
Note: There is now also Scrypt for Delphi.