Top Description Fields Constructors Methods
org.apache.sshd.common.config.keys.loader.openssh.kdf

public Class BCrypt

extends Object
Class Inheritance
Imports
java.io.UnsupportedEncodingException, java.nio.charset.StandardCharsets, java.security.DigestException, .MessageDigest, .NoSuchAlgorithmException, .SecureRandom

BCrypt implements OpenBSD-style Blowfish password hashing using the scheme described in "A Future-Adaptable Password Scheme" by Niels Provos and David Mazieres.

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.

Author
Damien Miller
Version
0.2

Field Summary

Modifier and TypeField 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[]
P

private static final int[]
private int[]
S

private static final int[]

Constructor Summary

AccessConstructor and Description
public

Method Summary

Modifier and TypeMethod and Description
private static byte

Returns:

the decoded value of x
char64
(char
the base64-encoded value
x
)

Look up the 3 bits base64-encoded by the specified character, range-checking againt conversion table

public static boolean

Returns:

true if the passwords match, false otherwise
checkpw
(String
the plaintext password to verify
plaintext
,
String
the previously-hashed password
hashed
)

Check that a plaintext password matches a previously hashed one

public byte[]

Returns:

an array containing the binary hashed password
crypt_raw
(byte[]
the 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 bytes
decode_base64
(String
the 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 string
encode_base64
(byte[]
the 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 value
gensalt
(int
the 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 value
gensalt
(int
the 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 value
gensalt
()

Generate 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

Returns:

the hashed password
hashpw
(String
the password to hash
password
,
String
the salt to hash with (perhaps generated using BCrypt.gensalt)
salt
)

Hash a password using the OpenBSD bcrypt scheme

private void
init_key()

Initialise the Blowfish key schedule

private void
key(byte[]
an array containing the key
key
)

Key the Blowfish cipher

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 data
streamtoword
(byte[]
the 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

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

base64_codeback to summary
private static final char[] base64_code
BCRYPT_SALT_LENback to summary
private static final int BCRYPT_SALT_LEN
bf_crypt_ciphertextback to summary
private static final int[] bf_crypt_ciphertext
BLOWFISH_NUM_ROUNDSback to summary
private static final int BLOWFISH_NUM_ROUNDS
GENSALT_DEFAULT_LOG2_ROUNDSback to summary
private static final int GENSALT_DEFAULT_LOG2_ROUNDS
index_64back to summary
private static final byte[] index_64
openbsd_ivback to summary
private static final int[] openbsd_iv
Pback to summary
private int[] P
P_origback to summary
private static final int[] P_orig
Sback to summary
private int[] S
S_origback to summary
private static final int[] S_orig

Constructor Detail

BCryptback to summary
public BCrypt()

Method Detail

char64back to summary
private static byte char64(char x)

Look up the 3 bits base64-encoded by the specified character, range-checking againt conversion table

Parameters
x:char

the base64-encoded value

Returns:byte

the decoded value of x

checkpwback to summary
public static boolean checkpw(String plaintext, String hashed)

Check that a plaintext password matches a previously hashed one

Parameters
plaintext:String

the plaintext password to verify

hashed:String

the previously-hashed password

Returns:boolean

true if the passwords match, false otherwise

crypt_rawback 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

Parameters
password:byte[]

the password to hash

salt:byte[]

the binary salt to hash with the password

log_rounds:int

the binary logarithm of the number of rounds of hashing to apply

cdata:int[]

the plaintext to encrypt

Returns:byte[]

an array containing the binary hashed password

decode_base64back 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.

Parameters
s:String

the string to decode

maxolen:int

the maximum number of bytes to decode

Returns:byte[]

an array containing the decoded bytes

Exceptions
IllegalArgumentException:
if maxolen is invalid
ekskeyback 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

Parameters
data:byte[]

salt information

key:byte[]

password information

encipherback to summary
private final void encipher(int[] lr, int off)

Blowfish encipher a single 64-bit block encoded as two 32-bit halves

Parameters
lr:int[]

an array containing the two 32-bit half blocks

off:int

the position in the array of the blocks

encode_base64back 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.

Parameters
d:byte[]

the byte array to encode

len:int

the number of bytes to encode

Returns:String

base64-encoded string

Exceptions
IllegalArgumentException:
if the length is invalid
gensaltback to summary
public static String gensalt(int log_rounds, SecureRandom random)

Generate a salt for use with the BCrypt.hashpw() method

Parameters
log_rounds:int

the log2 of the number of rounds of hashing to apply - the work factor therefore increases as 2**log_rounds.

random:SecureRandom

an instance of SecureRandom to use

Returns:String

an encoded salt value

gensaltback to summary
public static String gensalt(int log_rounds)

Generate a salt for use with the BCrypt.hashpw() method

Parameters
log_rounds:int

the log2 of the number of rounds of hashing to apply - the work factor therefore increases as 2**log_rounds.

Returns:String

an encoded salt value

gensaltback 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

Returns:String

an encoded salt value

hashback to summary
public void hash(byte[] hpass, byte[] hsalt, byte[] output)

Compatibility with new OpenBSD function.

Parameters
hpass:byte[]

The hash password bytes

hsalt:byte[]

The hash salt bytes

output:byte[]

Target hash output buffer

hashpwback to summary
public static String hashpw(String password, String salt)

Hash a password using the OpenBSD bcrypt scheme

Parameters
password:String

the password to hash

salt:String

the salt to hash with (perhaps generated using BCrypt.gensalt)

Returns:String

the hashed password

init_keyback to summary
private void init_key()

Initialise the Blowfish key schedule

keyback to summary
private void key(byte[] key)

Key the Blowfish cipher

Parameters
key:byte[]

an array containing the key

pbkdfback to summary
public void pbkdf(byte[] password, byte[] salt, int rounds, byte[] output)

Compatibility with new OpenBSD function.

Parameters
password:byte[]

The password bytes

salt:byte[]

The salt bytes

rounds:int

Number of hash rounds

output:byte[]

Hash output buffer

streamtowordback to summary
private static int streamtoword(byte[] data, int[] offp)

Cycically extract a word of key material

Parameters
data:byte[]

the string to extract the data from

offp:int[]

a "pointer" (as a one-entry array) to the current offset into data

Returns:int

the next word of material from data