Class Argon2Configuration
- All Implemented Interfaces:
de.gustavblass.commons.Copyable<Argon2Configuration>
-
Field Summary
FieldsModifier and TypeFieldDescriptionprivate static final StringThe stringargon2idin Base64 encoding.private intNumber of passes (used to tune the running time independently of the memory size).private static final org.apache.logging.log4j.Loggerstatic final doubleprivate intThe memory size m MUST be a number of kibibytes from 8*p to 2^(32)-1.static final longThe Argon2 specification requires thatm >= 8 * p, wheremis thememorysize in kibibytes andpis the degree ofparallelism.private intKnownDegree of parallelism p determines how many independent (but synchronising) computational chains (lanes) can be run.private byte[]The nonce (number used once) that must be unique for each key.private @NonNull Argon2VariantThe variant of the Argon2 key derivation function that shall be used.private @NonNull Argon2VersionThe revision of the Argon2 key derivation function. -
Constructor Summary
ConstructorsConstructorDescriptionCreates a new Argon2 configuration with the default parameters and a randomly generated salt of 32 bytes length.Argon2Configuration(byte @NonNull [] salt) Creates a new Argon2 configuration with the default parameters and the provided salt.Argon2Configuration(@NonNull Argon2Variant variant, @NonNull Argon2Version version, int memory, int iterations, int parallelism, byte @NonNull [] salt) Constructs a new Argon2 configuration with the specified parameters. -
Method Summary
Modifier and TypeMethodDescription@NonNull Argon2Configurationcopy()booleanCompares all fields of thisArgon2Configurationto the fields of another one (if it is anArgon2Configurationat all).static @NonNull Argon2ConfigurationfromBase64(@NonNull String hint) Converts a Base64-encoded string representation of anArgon2Configurationback to anArgon2Configurationobject.static @NonNull Argon2ConfigurationvoidsetIterations(int iterations) Updates the number ofiterationsof this configuration.voidsetMemory(int memory) Updates thememorysize m of this configuration.voidsetParallelism(int parallelism) Updates the degree ofparallelismp of this configuration.voidsetSalt(byte @NonNull [] salt) Updates thesaltof this configuration.voidsetVariant(@NonNull Argon2Variant variant) voidsetVersion(@NonNull Argon2Version version) @NonNull StringConverts this configuration to a PHC string, but without the password hash, i.e.@NonNull StringtoBase64()Converts this configuration to the format$base64$base64$base64$base64where the four Base64 strings are:@NonNull StringtoString()Alias fortoArgon2PasswordHint().
-
Field Details
-
LOG
private static final org.apache.logging.log4j.Logger LOG -
ARGON2ID_BASE64
-
MINIMUM_PARALLELISM_FACTOR_FOR_MEMORY
public static final long MINIMUM_PARALLELISM_FACTOR_FOR_MEMORYThe Argon2 specification requires thatm >= 8 * p, wheremis thememorysize in kibibytes andpis the degree ofparallelism.- See Also:
-
MAXIMUM_PARALLELISM
public static final double MAXIMUM_PARALLELISM -
variant
The variant of the Argon2 key derivation function that shall be used. -
version
The revision of the Argon2 key derivation function. -
memory
private int memoryThe memory size m MUST be a number of kibibytes from 8*p to 2^(32)-1. -
iterations
private int iterationsNumber of passes (used to tune the running time independently of the memory size). MUST be between 1 and 2^(32) - 1. -
parallelism
private int parallelismKnownDegree of parallelism p determines how many independent (but synchronising) computational chains (lanes) can be run. It MUST be a value from 1 to 2^(24)-1. -
salt
private byte[] saltThe nonce (number used once) that must be unique for each key. Use the same salt during decryption that was used during encryption, but never re-use a salt for different keys.
MUST NOT be longer than 2^(32)-1 and SHOULD be 32 bytes long.
-
-
Constructor Details
-
Argon2Configuration
public Argon2Configuration() throws de.gustavblass.commons.exceptions.IllegalStateExceptionCreates a new Argon2 configuration with the default parameters and a randomly generated salt of 32 bytes length.- Throws:
de.gustavblass.commons.exceptions.IllegalStateException- If no valid salt could be generated (should never happen).
-
Argon2Configuration
public Argon2Configuration(byte @NonNull [] salt) throws de.gustavblass.commons.exceptions.IllegalArgumentException Creates a new Argon2 configuration with the default parameters and the provided salt.- Parameters:
salt- The salt for this configuration. MUST NOT be null or empty. SHOULD be 32 bytes long. You will NOT be warned if your salt is insecurely short or if it is “predictable”.- Throws:
de.gustavblass.commons.exceptions.IllegalArgumentException- If the salt is null or empty.
-
Argon2Configuration
public Argon2Configuration(@NonNull @NonNull Argon2Variant variant, @NonNull @NonNull Argon2Version version, int memory, int iterations, int parallelism, byte @NonNull [] salt) throws de.gustavblass.commons.exceptions.IllegalArgumentException Constructs a new Argon2 configuration with the specified parameters.- Parameters:
variant- The variant of the Argon2 key derivation function that shall be used. Strongly recommended:Argon2Variant.ARGON2_ID.version- The revision of the Argon2 key derivation function that shall be used. Strongly recommended:Argon2Version.ARGON2_VERSION_19.memory- memory size (m) – in kibibytes – to be used by the Argon2 key derivation function. Must be a value from 8*p to 2^(32)-1. See alsoCryptor.ARGON2_MEMORY_KiB.iterations- number of passes (i) to be used by the Argon2 key derivation function (used to tune the running time independently of the memory size). MUST be between 1 and 2^(32) - 1. See alsoCryptor.ARGON2_ITERATIONS.parallelism- The degree of parallelism (p) to be used by the Argon2 key derivation function. MUST be a value from 1 to 2^(24)-1. See alsoCryptor.ARGON2_PARALLELISM.salt- The nonce (number used once) that must be unique for each key. Use the same salt during decryption that was used during encryption, but never re-use a salt for different keys. MUST NOT be longer than 2^(32)-1 and SHOULD be 32 bytes long. You will NOT be warned if your salt is insecurely short or if it is “predictable”.- Throws:
de.gustavblass.commons.exceptions.IllegalArgumentException-If:
- the number of iterations is not positive or too large.
- the degree of parallelism is not positive or too large.
- the memory size is too small, too large or not positive.
- the salt is null or empty.
-
-
Method Details
-
setVariant
- Parameters:
variant- The new Argon2 variant.
-
setVersion
- Parameters:
version- The new Argon2 version.
-
setMemory
public void setMemory(int memory) throws de.gustavblass.commons.exceptions.IllegalArgumentException Updates thememorysize m of this configuration.- Parameters:
memory- The new memory size m in kibibytes. MUST be greater than 8 * p.- Throws:
de.gustavblass.commons.exceptions.IllegalArgumentException- If the memory size is too small, too large or not positive.- Implementation Note:
- We do not need to check whether the memory is larger than the absolute limit, because
Integer.MAX_VALUEis less than 2^32 - 1 (the Argon2 limit).
-
setIterations
public void setIterations(int iterations) throws de.gustavblass.commons.exceptions.IllegalArgumentException Updates the number ofiterationsof this configuration.- Parameters:
iterations- The new number of iterations. MUST be greater than 0.- Throws:
de.gustavblass.commons.exceptions.IllegalArgumentException- If the number of iterations is not positive or too large.- Implementation Note:
- We do not need to check whether the number of iterations is too large, because
Integer.MAX_VALUEis less than 2^32 - 1 (the Argon2 limit).
-
setParallelism
public void setParallelism(int parallelism) throws de.gustavblass.commons.exceptions.IllegalArgumentException Updates the degree ofparallelismp of this configuration.- Parameters:
parallelism- The new degree of parallelism p. MUST be a value from 1 to 2^(24) - 1.- Throws:
de.gustavblass.commons.exceptions.IllegalArgumentException- If the parallelism is not positive or too large.
-
setSalt
public void setSalt(byte @NonNull [] salt) throws de.gustavblass.commons.exceptions.IllegalArgumentException Updates thesaltof this configuration.- Parameters:
salt- The new salt for this configuration. MUST NOT be null or empty. SHOULD be 32 bytes long. You will NOT be warned if your salt is insecurely short or if it is “predictable”.- Throws:
de.gustavblass.commons.exceptions.IllegalArgumentException- If the salt is null or empty.- Implementation Note:
- We do not need to check whether the salt is too large, because the maximum array length is 2^31 - 1 (per [Arrays#copyOf(byte[], int)]), which is less than 2^32 - 1 (the Argon2 limit).
-
toArgon2PasswordHint
Converts this configuration to a PHC string, but without the password hash, i.e. in the format
$argon2id$v=[version]$m=[memory],t=[iterations],p=[parallelism]$[salt]where[version]isArgon2Version.version[memory]ismemory[iterations]isiterations[parallelism]isparallelism[salt]is the base64-encodedsalt
This is useful when you want to use Argon2 for deriving a key for symmetric encryption, because when trying to decrypt the ciphertext later on, you will need to use the exact same Argon2 configuration.
Important: None of the parameters included in the returned password hint are secret, so they can safely be appended to the ciphertext. (According to Kerckhoffs' principle, the security of the encryption used must solely rely on the secrecy of the key.)
- Returns:
- This configuration as a PHC string.
-
toString
Alias fortoArgon2PasswordHint(). -
toBase64
Converts this configuration to the format
$base64$base64$base64$base64where the four Base64 strings are:- The string
argon2id v=[version]where[version]isArgon2Version.versionm=[memory],t=[iterations],p=[parallelism]withmemory,iterationsandparallelism- The Base64-encoded
salt
The resulting string can be converted back to an
Argon2Configurationusingparse(String).- Returns:
- The parameters of this configuration in Base64, separated by
$characters.
- The string
-
parse
@NonNull public static @NonNull Argon2Configuration parse(@NonNull @NonNull String hint) throws de.gustavblass.commons.exceptions.IllegalArgumentException - Parameters:
hint- Must match the format$argon2id$v=[version]$m=[memory],t=[iterations],p=[parallelism]$[salt]. Usually created bytoArgon2PasswordHint(). Refer toArgon2Configurationfor more information on the values that are allowed.- Returns:
- A new
Argon2Configurationwith the parameters from the hint. - Throws:
de.gustavblass.commons.exceptions.IllegalArgumentException- If the hint does not match the pattern or if any of the parameters are invalid.
-
fromBase64
@NonNull public static @NonNull Argon2Configuration fromBase64(@NonNull @NonNull String hint) throws de.gustavblass.commons.exceptions.IllegalArgumentException Converts a Base64-encoded string representation of anArgon2Configurationback to anArgon2Configurationobject.- Parameters:
hint-The Base64-encoded string representation of an
Argon2Configurationobject, as returned bytoBase64(). Must contain four parts, separated by$characters:argon2idforArgon2Variant.ARGON2_ID,argon2dforArgon2Variant.ARGON2_Dorargon2iforArgon2Variant.ARGON2_Iv=[version]where[version]isArgon2Version.versionm=[memory],t=[iterations],p=[parallelism]withmemory,iterationsandparallelism- The Base64-encoded
salt
- Returns:
- The
Argon2Configurationobject that was represented by the provided string. - Throws:
de.gustavblass.commons.exceptions.IllegalArgumentException- If the provided string is formatted incorrectly or if any of the parameters are invalid.
-
copy
- Specified by:
copyin interfacede.gustavblass.commons.Copyable<Argon2Configuration>- Returns:
- A deep copy of this
Argon2Configurationobject, with the exact same values, but a different reference. - Throws:
IllegalStateException- If the current values of this Argon2Configuration's fields are considered invalid by the constructor.- Implementation Note:
- Throws an unchecked IllegalStateException instead of a
checked one because
Copyable.copy()does not throw checked exceptions.
-
equals
Compares all fields of thisArgon2Configurationto the fields of another one (if it is anArgon2Configurationat all).- Overrides:
equalsin classObject- Parameters:
object- A differentArgon2Configurationthat shall be compared to this one.- Returns:
- True if all fields are identical, false otherwise.
-