This password hashing system tries to thwart off-line password cracking using a computationally-intensive hashing algorithm, based on Bruce Schneier's Blowfish cipher. The work factor of the algorithm is parameterised, so it can be increased as computers get faster.
Usage is really simple. To hash a password for the first time, call the hashpw method with a random salt, like this:
String pw_hash = BCrypt.hashpw(plain_password, BCrypt.gensalt());
To check whether a plaintext password matches one that has been hashed previously, use the checkpw method:
if (BCrypt.checkpw(candidate_password, stored_hash))
System.out.println("It matches");
else
System.out.println("It does not match");
The gensalt() method takes an optional parameter (log_rounds) that determines the computational complexity of the hashing:
String strong_salt = BCrypt.gensalt(10)
String stronger_salt = BCrypt.gensalt(12)
The amount of work increases exponentially (2**log_rounds), so each increment is twice as much work. The default log_rounds is 10, and the valid range is 4 to 30.
| Modifier and Type | Field and Description |
|---|---|
| private static final char[] | |
| private static final int | |
| private static final int[] | |
| private static final int | |
| private static final int | |
| private static final byte[] | |
| private static final int[] | |
| private int[] | |
| private static final int[] | |
| private int[] | |
| private static final int[] |
| Access | Constructor and Description |
|---|---|
| public |
| Modifier and Type | Method and Description |
|---|---|
| private static byte | Returns: the decoded value of xthe base64-encoded value x)Look up the 3 bits base64-encoded by the specified character, range-checking againt conversion table |
| public static boolean | |
| public byte[] | Returns: an array containing the binary hashed passwordthe password to hash password, byte[] the binary salt to hash with the password salt, int the binary logarithm of the number
of rounds of hashing to apply log_rounds, int[] the plaintext to encrypt cdata)Perform the central password hashing step in the bcrypt scheme |
| private static byte[] | Returns: an array containing the decoded bytesthe string to decode s, int the maximum number of bytes to decode maxolen)Decode a string encoded using bcrypt's base64 scheme to a byte array. |
| private void | ekskey(byte[]
salt information data, byte[] password information key)Perform the "enhanced key schedule" step described by Provos and Mazieres in "A Future-Adaptable Password Scheme" http://www.openbsd.org/papers/bcrypt-paper.ps |
| private final void | encipher(int[]
an array containing the two 32-bit half blocks lr, int the position in the array of the blocks off)Blowfish encipher a single 64-bit block encoded as two 32-bit halves |
| private static String | Returns: base64-encoded stringthe byte array to encode d, int the number of bytes to encode len)Encode a byte array using bcrypt's slightly-modified base64 encoding scheme. |
| public static String | Returns: an encoded salt valuethe log2 of the number of rounds of
hashing to apply - the work factor therefore increases as
2**log_rounds. log_rounds, SecureRandom an instance of SecureRandom to use random)Generate a salt for use with the BCrypt.hashpw() method |
| public static String | Returns: an encoded salt valuethe log2 of the number of rounds of
hashing to apply - the work factor therefore increases as
2**log_rounds. log_rounds)Generate a salt for use with the BCrypt.hashpw() method |
| public static String | Returns: an encoded salt valueGenerate a salt for use with the BCrypt.hashpw() method, selecting a reasonable default for the number of hashing rounds to apply |
| public void | hash(byte[]
The hash password bytes hpass, byte[] The hash salt bytes hsalt, byte[] Target hash output buffer output)Compatibility with new OpenBSD function. |
| public static String | |
| private void | |
| private void | |
| public void | pbkdf(byte[]
The password bytes password, byte[] The salt bytes salt, int Number of hash rounds rounds, byte[] Hash output buffer output)Compatibility with new OpenBSD function. |
| private static int | Returns: the next word of material from datathe string to extract the data from data, int[] a "pointer" (as a one-entry array) to the
current offset into data offp)Cycically extract a word of key material |
| base64_code | back to summary |
|---|---|
| private static final char[] base64_code | |
| BCRYPT_SALT_LEN | back to summary |
|---|---|
| private static final int BCRYPT_SALT_LEN | |
| bf_crypt_ciphertext | back to summary |
|---|---|
| private static final int[] bf_crypt_ciphertext | |
| BLOWFISH_NUM_ROUNDS | back to summary |
|---|---|
| private static final int BLOWFISH_NUM_ROUNDS | |
| GENSALT_DEFAULT_LOG2_ROUNDS | back to summary |
|---|---|
| private static final int GENSALT_DEFAULT_LOG2_ROUNDS | |
| index_64 | back to summary |
|---|---|
| private static final byte[] index_64 | |
| openbsd_iv | back to summary |
|---|---|
| private static final int[] openbsd_iv | |
| P | back to summary |
|---|---|
| private int[] P | |
| P_orig | back to summary |
|---|---|
| private static final int[] P_orig | |
| S | back to summary |
|---|---|
| private int[] S | |
| S_orig | back to summary |
|---|---|
| private static final int[] S_orig | |
| BCrypt | back to summary |
|---|---|
| public BCrypt() | |
| char64 | back to summary |
|---|---|
| private static byte char64(char x) Look up the 3 bits base64-encoded by the specified character, range-checking againt conversion table
| |
| checkpw | back to summary |
|---|---|
| public static boolean checkpw(String plaintext, String hashed) Check that a plaintext password matches a previously hashed one | |
| crypt_raw | back to summary |
|---|---|
| public byte[] crypt_raw(byte[] password, byte[] salt, int log_rounds, int[] cdata) Perform the central password hashing step in the bcrypt scheme
| |
| decode_base64 | back to summary |
|---|---|
| private static byte[] decode_base64(String s, int maxolen) throws IllegalArgumentException Decode a string encoded using bcrypt's base64 scheme to a byte array. Note that this is *not* compatible with the standard MIME-base64 encoding.
| |
| ekskey | back to summary |
|---|---|
| private void ekskey(byte[] data, byte[] key) Perform the "enhanced key schedule" step described by Provos and Mazieres in "A Future-Adaptable Password Scheme" http://www.openbsd.org/papers/bcrypt-paper.ps
| |
| encipher | back to summary |
|---|---|
| private final void encipher(int[] lr, int off) Blowfish encipher a single 64-bit block encoded as two 32-bit halves
| |
| encode_base64 | back to summary |
|---|---|
| private static String encode_base64(byte[] d, int len) throws IllegalArgumentException Encode a byte array using bcrypt's slightly-modified base64 encoding scheme. Note that this is *not* compatible with the standard MIME-base64 encoding.
| |
| gensalt | back to summary |
|---|---|
| public static String gensalt(int log_rounds, SecureRandom random) Generate a salt for use with the BCrypt.hashpw() method
| |
| gensalt | back to summary |
|---|---|
| public static String gensalt(int log_rounds) Generate a salt for use with the BCrypt.hashpw() method
| |
| gensalt | back to summary |
|---|---|
| public static String gensalt() Generate a salt for use with the BCrypt.hashpw() method, selecting a reasonable default for the number of hashing rounds to apply
| |
| hash | back to summary |
|---|---|
| public void hash(byte[] hpass, byte[] hsalt, byte[] output) Compatibility with new OpenBSD function.
| |
| hashpw | back to summary |
|---|---|
| public static String hashpw(String password, String salt) Hash a password using the OpenBSD bcrypt scheme | |
| init_key | back to summary |
|---|---|
| private void init_key() Initialise the Blowfish key schedule | |
| key | back to summary |
|---|---|
| private void key(byte[] key) Key the Blowfish cipher
| |
| pbkdf | back to summary |
|---|---|
| public void pbkdf(byte[] password, byte[] salt, int rounds, byte[] output) Compatibility with new OpenBSD function.
| |
| streamtoword | back to summary |
|---|---|
| private static int streamtoword(byte[] data, int[] offp) Cycically extract a word of key material
| |