Class Argon2PasswordEncoder

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

public final class Argon2PasswordEncoder extends Object implements PasswordEncoder

Argon2 (RFC 9106) as a PasswordEncoder: argon2id, argon2i and argon2d, stored in the PHC string format every framework writes.

format stored value
Argon2PasswordEncoder.Format.PHC $argon2id$v=19$m=<KiB>,t=<passes>,p=<lanes>$<salt>$<hash> -- Spring Security, passlib, argon2-cffi
Argon2PasswordEncoder.Format.DJANGO argon2$argon2id$v=19$m=...,t=...,p=...$<salt>$<hash>

PasswordEncoderFactories.createDelegatingPasswordEncoder() registers the PHC layout as {argon2}, which reads what Spring's Argon2PasswordEncoder stores. For a Django table, register the Django layout under an id of its own:

encoders.put("django-argon2", new Argon2PasswordEncoder(Argon2PasswordEncoder.Format.DJANGO));

A stored value names its own cost. One asking for more than maxMemory KiB (128 MiB by default), more than maxIterations passes (64) or more than maxParallelism lanes (64) does not verify, so a hostile row cannot take the server's heap or a host thread for minutes.

Argon2 is computed in Java here, one lane after another; the lane count changes the result, as it must, but not the threading. A login occupies its host thread for the whole computation.

  • Nested Class Summary

    Nested Classes
    Modifier and Type
    Class
    Description
    static enum 
    The stored layouts read and written.
  • Field Summary

    Fields
    Modifier and Type
    Field
    Description
    static final int
    argon2d: data-dependent memory access.
    static final int
    argon2i: data-independent memory access.
    static final int
    argon2id: the hybrid RFC 9106 recommends.
  • Constructor Summary

    Constructors
    Constructor
    Description
    The PHC layout with Spring Security 5.8's defaults for a new hash (argon2id, 16 byte salt, 32 byte hash, p = 1, m = 16384, t = 2).
    Argon2PasswordEncoder(int saltLength, int hashLength, int parallelism, int memory, int iterations)
    Spring's constructor: the cost a new argon2id hash is written with, memory in KiB.
    format, writing new hashes with that framework's defaults.
    Argon2PasswordEncoder(Argon2PasswordEncoder.Format format, int saltLength, int hashLength, int parallelism, int memory, int iterations)
    format with the cost a new argon2id hash is written with, memory in KiB.
  • Method Summary

    Modifier and Type
    Method
    Description
    Spring's defaults as of Spring Security 5.8.
    encode(CharSequence rawPassword)
    The password as it should be stored: salted and hashed, so encoding one password twice gives two different results.
    boolean
    isReadable(String encodedPassword)
    Whether encodedPassword is a value of this layout within the memory, pass and lane limits.
    boolean
    matches(CharSequence rawPassword, String encodedPassword)
    Whether rawPassword is the password encodedPassword was made from.
    void
    setMaxIterations(int maxIterations)
    The most passes a stored value may ask for.
    void
    setMaxMemory(long maxMemoryKib)
    The most memory, in KiB, a stored value may ask for.
    void
    setMaxParallelism(int maxParallelism)
    The most lanes a stored value may ask for.
    boolean
    upgradeEncoding(String encodedPassword)
    True when the stored value is not argon2id or was written with less memory or fewer passes than this encoder writes; always true for the Django layout, a format to migrate away from.

    Methods inherited from class Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Field Details

    • ARGON2_D

      public static final int ARGON2_D
      argon2d: data-dependent memory access.
      See Also:
    • ARGON2_I

      public static final int ARGON2_I
      argon2i: data-independent memory access.
      See Also:
    • ARGON2_ID

      public static final int ARGON2_ID
      argon2id: the hybrid RFC 9106 recommends.
      See Also:
  • Constructor Details

    • Argon2PasswordEncoder

      public Argon2PasswordEncoder()
      The PHC layout with Spring Security 5.8's defaults for a new hash (argon2id, 16 byte salt, 32 byte hash, p = 1, m = 16384, t = 2).
    • Argon2PasswordEncoder

      public Argon2PasswordEncoder(Argon2PasswordEncoder.Format format)
      format, writing new hashes with that framework's defaults.
    • Argon2PasswordEncoder

      public Argon2PasswordEncoder(int saltLength, int hashLength, int parallelism, int memory, int iterations)
      Spring's constructor: the cost a new argon2id hash is written with, memory in KiB.
    • Argon2PasswordEncoder

      public Argon2PasswordEncoder(Argon2PasswordEncoder.Format format, int saltLength, int hashLength, int parallelism, int memory, int iterations)
      format with the cost a new argon2id hash is written with, memory in KiB.
  • Method Details

    • defaultsForSpringSecurity_v5_8

      public static Argon2PasswordEncoder defaultsForSpringSecurity_v5_8()
      Spring's defaults as of Spring Security 5.8.
    • setMaxMemory

      public void setMaxMemory(long maxMemoryKib)
      The most memory, in KiB, a stored value may ask for. Default 131072.
    • setMaxIterations

      public void setMaxIterations(int maxIterations)
      The most passes a stored value may ask for. Default 64.
    • setMaxParallelism

      public void setMaxParallelism(int maxParallelism)
      The most lanes a stored value may ask for. Default 64.
    • 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 is not argon2id or was written with less memory or fewer passes than this encoder writes; always true for the Django layout, a format to migrate away from.
      Specified by:
      upgradeEncoding in interface PasswordEncoder
    • isReadable

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