Class BCrypt
This implementation does not correspondent to the 1999 published paper "A Future-Adaptable Password Scheme" of Niels Provos and David Mazières, see: https://www.usenix.org/legacy/events/usenix99/provos/provos_html/node1.html. In contrast to the paper, the order of key setup and salt setup is reversed: state <- ExpandKey(state, 0, key) state <- ExpandKey(state, 0, salt) This corresponds to the OpenBSD reference implementation of Bcrypt.
Note: There is no successful cryptanalysis (status 2015), but the amount of memory and the band width of Bcrypt may be insufficient to effectively prevent attacks with custom hardware like FPGAs, ASICs
This implementation uses some parts of Bouncy Castle's BlowfishEngine.
-
Method Summary
Modifier and TypeMethodDescriptionstatic byte[]generate(byte[] pwInput, byte[] salt, int cost) Deprecated.static byte[]generate(byte[] pwInput, byte[] salt, int cost, boolean addTerminator) Calculates the bcrypt hash of an input, optionally appending the bcrypt password terminator (a single 0x00 byte) topwInputbefore the EksBlowfishSetup password schedule.static byte[]passwordToByteArray(char[] password) Converts a character password to bytes incorporating the required trailing zero byte.static byte[]pbkdfGenerate(byte[] password, byte[] salt, int rounds, int outputLength) The OpenSSHbcrypt_pbkdfpassword-based key derivation function, as used to protect theopenssh-key-v1private key format (kdfname"bcrypt").
-
Method Details
-
passwordToByteArray
public static byte[] passwordToByteArray(char[] password) Converts a character password to bytes incorporating the required trailing zero byte.- Parameters:
password- the password to be encoded.- Returns:
- a byte representation of the password in UTF8 + trailing zero.
-
generate
public static byte[] generate(byte[] pwInput, byte[] salt, int cost) Deprecated.as a direct replacement usegenerate(byte[], byte[], int, boolean)so the terminator choice is explicit at the call site — passtrueto have the spec-required 0x00 terminator appended (equivalent to feedingpwInputthroughpasswordToByteArray(char[])),falsewhen the supplied bytes already include it. For general password hashing preferOpenBSDBCrypt, which handles termination, salt formatting and the modular crypt output line; this raw primitive remains available for test-vector validation and interop with implementations that emit the 24-byte hash directly (issue #1741).Calculates the bcrypt hash of an input, passing the password bytes through to the EksBlowfishSetup password schedule unchanged. The bcrypt specification requires the password input to be null-terminated; this overload does not append the terminator, so the caller must supplypwInputalready terminated (for example viapasswordToByteArray(char[])). Without a trailing 0x00 byte, distinct passwords whose encodings differ only by repetition (e.g."abc"and"abcabc") collide and produce identical hashes.This implements the raw bcrypt function as defined in the bcrypt specification, not the crypt encoded version implemented in OpenBSD, see
OpenBSDBCryptfor that.- Parameters:
pwInput- the password bytes (up to 72 bytes) to use for this invocation.salt- the 128 bit salt to use for this invocation.cost- the bcrypt cost parameter. The cost of the bcrypt function grows as2^cost. Legal values are 4..31 inclusive.- Returns:
- the output of the raw bcrypt operation: a 192 bit (24 byte) hash.
-
generate
public static byte[] generate(byte[] pwInput, byte[] salt, int cost, boolean addTerminator) Calculates the bcrypt hash of an input, optionally appending the bcrypt password terminator (a single 0x00 byte) topwInputbefore the EksBlowfishSetup password schedule. For general password hashing useOpenBSDBCryptrather than this primitive; this entry point is intended for callers driving the raw bcrypt function against published test vectors or interoperating with another implementation that produces the 24-byte raw hash directly.Setting
addTerminatortrueis equivalent to feeding bytes produced bypasswordToByteArray(char[])and is the right choice for any caller that has not already terminated the input. Setting itfalsepassespwInputthrough unchanged — appropriate when the supplied bytes are already terminated, or when reproducing a test vector that fixes the exact input.When
pwInput.lengthis exactly 72 (the bcrypt 72-byte input limit),addTerminatoris ignored and the input is used as-is — there is no room for an additional byte without exceeding the limit. Two 72-byte inputs sharing a common prefix may therefore collide in the same way an unterminated shorter input does.- Parameters:
pwInput- the password bytes (up to 72 bytes) to use for this invocation.salt- the 128 bit salt to use for this invocation.cost- the bcrypt cost parameter. The cost of the bcrypt function grows as2^cost. Legal values are 4..31 inclusive.addTerminator- whether to append the bcrypt password-terminator byte (0x00) topwInputbefore the password schedule. Ignored whenpwInput.lengthis exactly 72.- Returns:
- the output of the raw bcrypt operation: a 192 bit (24 byte) hash.
-
pbkdfGenerate
public static byte[] pbkdfGenerate(byte[] password, byte[] salt, int rounds, int outputLength) The OpenSSHbcrypt_pbkdfpassword-based key derivation function, as used to protect theopenssh-key-v1private key format (kdfname"bcrypt"). It layers SHA-512 over a 32-bytebcrypt_hashprimitive and distributes the output non-linearly; it is not interchangeable with PBKDF2-over-bcrypt or with the OpenBSD password hash produced bygenerate(byte[], byte[], int, boolean)/OpenBSDBCrypt.- Parameters:
password- the passphrase bytes (the OpenSSH client uses the raw UTF-8 bytes, with no trailing NUL).salt- the salt taken from the key's kdfoptions; must be non-empty.rounds- the iteration count from the key's kdfoptions; must be at least 1.outputLength- the number of key bytes to derive (the cipher key length plus IV length); must be between 1 and 1024 inclusive.- Returns:
outputLengthderived key bytes.
-
generate(byte[], byte[], int, boolean)so the terminator choice is explicit at the call site — passtrueto have the spec-required 0x00 terminator appended (equivalent to feedingpwInputthroughpasswordToByteArray(char[])),falsewhen the supplied bytes already include it.