Class SCryptPasswordEncoder

java.lang.Object
com.codename1.backend.security.crypto.SCryptPasswordEncoder
All Implemented Interfaces:
PasswordEncoder

public final class SCryptPasswordEncoder extends Object implements 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.

  • 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

      public SCryptPasswordEncoder(SCryptPasswordEncoder.Format format)
      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)
      format with the cost parameters a new hash is written with.
  • Method Details

    • defaultsForSpringSecurity_v5_8

      public static SCryptPasswordEncoder 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 * r bytes, a stored value may ask for.
    • setMaxParallelism

      public void setMaxParallelism(int maxParallelism)
      The largest p a stored value may ask for. Default 16.
    • encode

      public String encode(CharSequence rawPassword)
      Description copied from interface: PasswordEncoder
      The password as it should be stored: salted and hashed, so encoding one password twice gives two different results.
      Specified by:
      encode in interface PasswordEncoder
    • matches

      public boolean matches(CharSequence rawPassword, String encodedPassword)
      Description copied from interface: PasswordEncoder
      Whether rawPassword is the password encodedPassword was made from.
      Specified by:
      matches in interface PasswordEncoder
    • upgradeEncoding

      public boolean upgradeEncoding(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.
      Specified by:
      upgradeEncoding in interface PasswordEncoder
    • isReadable

      public boolean isReadable(String encodedPassword)
      Whether encodedPassword is a value of this layout within the memory and parallelism limits. What a migration asks before importing a row.