Top Description Fields Constructors Methods
org.bouncycastle.crypto.generators

public final Class BCrypt

extends Object
Class Inheritance
Imports
org.bouncycastle.crypto.DataLengthException, org.bouncycastle.util.Arrays, .Pack, .Strings

Core of password hashing scheme Bcrypt, designed by Niels Provos and David Mazières, corresponds to the C reference implementation.

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.

Field Summary

Modifier and TypeField and Description
private static final int[]
private static final int[]
private static final int[]
private static final int[]
private static final int[]
private static final int[]
pack-priv static final int
pack-priv static final int
MAX_COST

Maximum value of cost parameter (31 == 2,147,483,648)

pack-priv static final int
MAX_PASSWORD_BYTES

Maximum size of password == max (unrestricted) size of Blowfish key

pack-priv static final int
MIN_COST

Minimum value of cost parameter, equal to log2(bytes of salt)

private final int[]
P

private static final int
private static final int
private final int[]
S

pack-priv static final int
SALT_SIZE_BYTES

Size of the salt parameter in bytes

private static final int
private static final int
private static final int

Constructor Summary

AccessConstructor and Description
private

Method Summary

Modifier and TypeMethod and Description
private final void
cyclicXorKey(byte[] key)

private final byte[]

Returns:

a 192 bit key
deriveRawKey
(int
the cost factor, treated as an exponent of 2
cost
,
byte[]
a 16 byte salt
salt
,
byte[]
the password
psw
)

Derives a raw 192 bit Bcrypt key

private byte[]
private int
F(int x)

public static byte[]

Returns:

the output of the raw bcrypt operation: a 192 bit (24 byte) hash.
generate
(byte[]
the password bytes (up to 72 bytes) to use for this invocation.
pwInput
,
byte[]
the 128 bit salt to use for this invocation.
salt
,
int
the bcrypt cost parameter. The cost of the bcrypt function grows as 2^cost. Legal values are 4..31 inclusive.
cost
)

Calculates the bcrypt hash of an input - note for processing general passwords you want to make sure the password is terminated in a manner similar to what is done by passwordToByteArray().

private void
public static byte[]

Returns:

a byte representation of the password in UTF8 + trailing zero.
passwordToByteArray
(char[]
the password to be encoded.
password
)

Converts a character password to bytes incorporating the required trailing zero byte.

private void
processTable(int xl, int xr, int[] table)

private void
processTableWithSalt(int[] table, int[] salt32Bit, int iv1, int iv2)

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

KPback to summary
private static final int[] KP
KS0back to summary
private static final int[] KS0
KS1back to summary
private static final int[] KS1
KS2back to summary
private static final int[] KS2
KS3back to summary
private static final int[] KS3
MAGIC_STRINGback to summary
private static final int[] MAGIC_STRING
MAGIC_STRING_LENGTHback to summary
pack-priv static final int MAGIC_STRING_LENGTH
MAX_COSTback to summary
pack-priv static final int MAX_COST

Maximum value of cost parameter (31 == 2,147,483,648)

MAX_PASSWORD_BYTESback to summary
pack-priv static final int MAX_PASSWORD_BYTES

Maximum size of password == max (unrestricted) size of Blowfish key

MIN_COSTback to summary
pack-priv static final int MIN_COST

Minimum value of cost parameter, equal to log2(bytes of salt)

Pback to summary
private final int[] P
P_SZback to summary
private static final int P_SZ
ROUNDSback to summary
private static final int ROUNDS
Sback to summary
private final int[] S
SALT_SIZE_BYTESback to summary
pack-priv static final int SALT_SIZE_BYTES

Size of the salt parameter in bytes

SBOX_SKback to summary
private static final int SBOX_SK
SBOX_SK2back to summary
private static final int SBOX_SK2
SBOX_SK3back to summary
private static final int SBOX_SK3

Constructor Detail

BCryptback to summary
private BCrypt()

Method Detail

cyclicXorKeyback to summary
private final void cyclicXorKey(byte[] key)
deriveRawKeyback to summary
private final byte[] deriveRawKey(int cost, byte[] salt, byte[] psw)

Derives a raw 192 bit Bcrypt key

Parameters
cost:int

the cost factor, treated as an exponent of 2

salt:byte[]

a 16 byte salt

psw:byte[]

the password

Returns:byte[]

a 192 bit key

encryptMagicStringback to summary
private byte[] encryptMagicString()
Fback to summary
private int F(int x)
generateback to summary
public static byte[] generate(byte[] pwInput, byte[] salt, int cost)

Calculates the bcrypt hash of an input - note for processing general passwords you want to make sure the password is terminated in a manner similar to what is done by passwordToByteArray().

This implements the raw bcrypt function as defined in the bcrypt specification, not the crypt encoded version implemented in OpenBSD.

Parameters
pwInput:byte[]

the password bytes (up to 72 bytes) to use for this invocation.

salt:byte[]

the 128 bit salt to use for this invocation.

cost:int

the bcrypt cost parameter. The cost of the bcrypt function grows as 2^cost. Legal values are 4..31 inclusive.

Returns:byte[]

the output of the raw bcrypt operation: a 192 bit (24 byte) hash.

initStateback to summary
private void initState()
passwordToByteArrayback to summary
public static byte[] passwordToByteArray(char[] password)

Converts a character password to bytes incorporating the required trailing zero byte.

Parameters
password:char[]

the password to be encoded.

Returns:byte[]

a byte representation of the password in UTF8 + trailing zero.

processTableback to summary
private void processTable(int xl, int xr, int[] table)
processTableWithSaltback to summary
private void processTableWithSalt(int[] table, int[] salt32Bit, int iv1, int iv2)