Home

Awesome

Bcrypt for Delphi

Bcrypt is an algorithm designed for hashing passwords, and only passwords; i.e. it:

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.

Sample Usage

The out parameter passwordRehashNeeded indicates if the stored password hash needs to be upgraded. A hash would need to be upgraded if:

By convention BCrypt outputs a hash as string such as:

$2a$12$EA6qjRCeBi8bGgs4rhfn8udEGKmu0ayrZYCEJqf6nNIoytowKFncm

The parts of the string are:

ValueMeaningNotes
2aHash algorithm"2a" = current version of BCrypt, "2" = obsolete version of BCrypt, "1" = MD5
12cost factorWill iterate for 2<sup>12</sup>=4,096 rounds. (Default is 11)
EA6qjRCeBi8bGgs4rhfn8uSalt22 base64 encoded characters
dEGKmu0ayrZYCEJqf6nNIoytowKFncmHashed password31 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).

Speed Tests

The current (3/21/2015) hard-coded default for cost is 11. This results in 2<sup>11</sup> = 2,048 rounds during the key setup.

3/14/2015 Intel Core i5-2500 CPU @ 3.50 GHz Delphi XE6 (32-bit, Release)

CostIterationsDuration
8256 iterations22.0 ms
9512 iterations43.3 ms
101,024 iterations85.5 ms
112,048 iterations173.3 ms
124,096 iterations345.6 ms
138,192 iterations694.3 ms
1416,384 iterations1,390.5 ms
1532,768 iterations2,781.4 ms
1665,536 iterations5,564.9 ms

At the time of publication of BCrypt (1999) the default costs were:

"Of course, whatever cost people choose should be reevaluated from time to time."

We want to target between 250-500 ms per hash. To that end, when calling HashPassword the system will automatically determine a cost factor that results in a hash that takes 250-500 ms to compute. It does this by profiling the computer performance. Regardless of the results of the profiling, it will never use a cost lower than the BCRYPT_COST constant.

Speedtest results

Bcrypt variants

The version 2b is not "better", "stronger", or "more modern" than 2, or 2a (or 2x or 2y). It is simply a relic of one particular buggy implementation of bcrypt.

In fact, if you have been:

then all five versions are functionally identical - for the same input they all generate the same output.

Created by Ian Boyd 5/3/2012

Public Domain
For more information, please refer to http://unlicense.org/