Backend API. Server-side code: this runs in a Codename One backend, not in the app on the device.

public final class BCryptPasswordEncoder

  1. Object
  2. BCryptPasswordEncoder

ImplementsPasswordEncoder

bcrypt, as OpenBSD defined it and Spring’s BCryptPasswordEncoder writes it: $2a$, $2b$ or $2y$, a cost, a 16 byte salt and a 23 byte hash.

$2a$10$N9qo8uLOickgx2ZMRZoMyeIjZAgcfl7p92ldGxad68LJZdL17lhWy

Registered as {bcrypt}, so passwords hashed by a Spring application verify unchanged. It is written in Java – this runtime has no native Blowfish – and at cost 10 one check is tens of milliseconds of a host thread, which is why new passwords are encoded with {pbkdf2-sha256} instead. Only the first 72 bytes of a password take part, as in every bcrypt.

Fields

public static final int DEFAULT_STRENGTH = 10The cost Spring uses unless told otherwise.

Constructors

public BCryptPasswordEncoder()An encoder of cost DEFAULT_STRENGTH writing $2a$.
public BCryptPasswordEncoder(int strength)
public BCryptPasswordEncoder(char version, int strength)

Methods

public String encode(CharSequence rawPassword)The password as it should be stored: salted and hashed, so encoding one password twice gives two different results.
public boolean matches(CharSequence rawPassword, String encodedPassword)Whether rawPassword is the password encodedPassword was made from.
public boolean upgradeEncoding(String encodedPassword)True when the stored password was made with a lower cost than this encoder’s.

Inherited methods

Field details

DEFAULT_STRENGTH

public static final int DEFAULT_STRENGTH = 10
The cost Spring uses unless told otherwise.

Constructor details

BCryptPasswordEncoder

public BCryptPasswordEncoder()
An encoder of cost DEFAULT_STRENGTH writing $2a$.

BCryptPasswordEncoder

public BCryptPasswordEncoder(int strength)

Parameters

strength int
the cost, 4 to 31: the work doubles with every step

BCryptPasswordEncoder

public BCryptPasswordEncoder(char version, int strength)

Parameters

version char
the letter after $2: ‘a’, ‘b’ or ‘y’
strength int
the cost, 4 to 31

Method details

encode

public String encode(CharSequence rawPassword)
The password as it should be stored: salted and hashed, so encoding one password twice gives two different results.

matches

public boolean matches(CharSequence rawPassword, String encodedPassword)
Whether rawPassword is the password encodedPassword was made from.

upgradeEncoding

public boolean upgradeEncoding(String encodedPassword)
True when the stored password was made with a lower cost than this encoder’s.