Class SCryptPasswordEncoder
- All Implemented Interfaces:
PasswordEncoder
scrypt (RFC 7914) as a PasswordEncoder, reading and writing the layouts
the frameworks a user table comes from store it in.
| format | stored value |
|---|---|
SCryptPasswordEncoder.Format.SPRING |
$<hex params>$<base64 salt>$<base64 key> -- Spring Security's SCryptPasswordEncoder |
SCryptPasswordEncoder.Format.DJANGO |
scrypt$<N>$<salt>$<r>$<p>$<base64 key> |
SCryptPasswordEncoder.Format.WERKZEUG |
scrypt:<N>:<r>:<p>$<salt>$<hex key> |
SCryptPasswordEncoder.Format.PASSLIB |
$scrypt$ln=<log2 N>,r=<r>,p=<p>$<ab64 salt>$<ab64 key> |
PasswordEncoderFactories.createDelegatingPasswordEncoder() registers the
Spring layout as {scrypt}. For a Django or Werkzeug table, register one
under an id of its own, as with ModularPbkdf2PasswordEncoder:
encoders.put("werkzeug-scrypt", new SCryptPasswordEncoder(SCryptPasswordEncoder.Format.WERKZEUG));
A stored value names its own cost, and scrypt's cost is memory:
128 * N * r bytes. One asking for more than maxMemory (128 MB by
default) or for more than maxParallelism passes does not verify, so a
hostile row cannot take the server's heap. Werkzeug's default (N = 32768, r = 8) needs 32 MB.
scrypt is computed in Java here, one pass after another. A login occupies its host thread for the whole computation, so the encoding cost stays modest; rows are re-hashed with the default encoder as their owners sign in when this is not the encoding id.
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic enumThe stored layouts read and written. -
Constructor Summary
ConstructorsConstructorDescriptionSpring's layout with Spring's defaults for a new hash (N = 65536, r = 8, p = 1, 32 byte key, 16 byte salt).SCryptPasswordEncoder(int cpuCost, int memoryCost, int parallelization, int keyLength, int saltLength) Spring's constructor: the cost parameters a new hash is written with.format, writing new hashes with that framework's defaults.SCryptPasswordEncoder(SCryptPasswordEncoder.Format format, int cpuCost, int memoryCost, int parallelization, int keyLength, int saltLength) formatwith the cost parameters a new hash is written with. -
Method Summary
Modifier and TypeMethodDescriptionstatic SCryptPasswordEncoderSpring's defaults as of Spring Security 5.8:N = 65536, r = 8, p = 1.encode(CharSequence rawPassword) The password as it should be stored: salted and hashed, so encoding one password twice gives two different results.booleanisReadable(String encodedPassword) WhetherencodedPasswordis a value of this layout within the memory and parallelism limits.booleanmatches(CharSequence rawPassword, String encodedPassword) WhetherrawPasswordis the passwordencodedPasswordwas made from.voidsetMaxMemory(long maxMemory) The most memory,128 * N * rbytes, a stored value may ask for.voidsetMaxParallelism(int maxParallelism) The largestpa stored value may ask for.booleanupgradeEncoding(String encodedPassword) True when the stored value was written with weaker parameters than this encoder writes; always true for a layout other than Spring's.
-
Constructor Details
-
SCryptPasswordEncoder
public SCryptPasswordEncoder()Spring's layout with Spring's defaults for a new hash (N = 65536, r = 8, p = 1, 32 byte key, 16 byte salt). -
SCryptPasswordEncoder
format, writing new hashes with that framework's defaults. -
SCryptPasswordEncoder
public SCryptPasswordEncoder(int cpuCost, int memoryCost, int parallelization, int keyLength, int saltLength) Spring's constructor: the cost parameters a new hash is written with. -
SCryptPasswordEncoder
public SCryptPasswordEncoder(SCryptPasswordEncoder.Format format, int cpuCost, int memoryCost, int parallelization, int keyLength, int saltLength) formatwith the cost parameters a new hash is written with.
-
-
Method Details
-
defaultsForSpringSecurity_v5_8
Spring's defaults as of Spring Security 5.8:N = 65536, r = 8, p = 1. -
setMaxMemory
public void setMaxMemory(long maxMemory) The most memory,128 * N * rbytes, a stored value may ask for. -
setMaxParallelism
public void setMaxParallelism(int maxParallelism) The largestpa stored value may ask for. Default 16. -
encode
Description copied from interface:PasswordEncoderThe password as it should be stored: salted and hashed, so encoding one password twice gives two different results.- Specified by:
encodein interfacePasswordEncoder
-
matches
Description copied from interface:PasswordEncoderWhetherrawPasswordis the passwordencodedPasswordwas made from.- Specified by:
matchesin interfacePasswordEncoder
-
upgradeEncoding
True when the stored value was written with weaker parameters than this encoder writes; always true for a layout other than Spring's.- Specified by:
upgradeEncodingin interfacePasswordEncoder
-
isReadable
WhetherencodedPasswordis a value of this layout within the memory and parallelism limits. What a migration asks before importing a row.
-