java.lang.Object
page.codeberg.friedolyn.crypto.HashingResult
- All Implemented Interfaces:
Copyable<HashingResult>
The result of a hashing operation. Does not contain the
input text that was hashed, but only the hash itself and the
Argon2
parameters that were used to
compute it.- Implementation Note:
- This class is not a record, because the constructor needs to check that the provided hash is
not
nullor empty and throw anIllegalArgumentExceptionif that's the case. While that could be done in a record as well, we cannot addthrows IllegalArgumentExceptionto the record's constructor signature, risking that callers of the constructor will not handle the exception. JavaDoc is not a sufficient replacement here!
-
Field Summary
FieldsModifier and TypeFieldDescriptionprivate final @NonNull Argon2ConfigurationThe Argon2 parameters that were used to compute the hash.private final byte[] -
Constructor Summary
ConstructorsConstructorDescriptionHashingResult(byte[] hash, @NonNull Argon2Configuration argon2Configuration) Constructs a newHashingResultobject with the providedArgon2 hashand theArgon2 parametersthat were used to compute the hash. -
Method Summary
Modifier and TypeMethodDescription@NonNull HashingResultcopy()Creates a deep copy of thisHashingResultobject.@NonNull FuzzyBooleanequals(@NonNull HashingResult other) Compares all fields of thisHashingResultobject to the fields of anotherHashingResultobject.@NonNull Argon2Configurationstatic @NonNull HashingResultstatic @NonNull HashingResultparseBase64(@NonNull String hash) @NonNull StringtoBase64()Converts thehashand theargon2Configurationto a string representation that contains all the information necessary toconvertit back to aHashingResultobject.@NonNull StringtoString()Converts thehashand theargon2Configurationto a string representation that contains all the information necessary toconvertit back to aHashingResultobject.booleanVerifies thatthis hashis the hash of the provided input text.
-
Field Details
-
hash
-
argon2Configuration
The Argon2 parameters that were used to compute the hash.
-
-
Constructor Details
-
HashingResult
public HashingResult(byte[] hash, @NonNull @NonNull Argon2Configuration argon2Configuration) throws IllegalArgumentException Constructs a newHashingResultobject with the providedArgon2 hashand theArgon2 parametersthat were used to compute the hash.- Parameters:
hash- TheArgon2 hashof the input text.argon2Configuration- The Argon2 parameters that were used to compute the hash.- Throws:
IllegalArgumentException- If the provided hash isnullor empty.
-
-
Method Details
-
getArgon2Configuration
- Returns:
- A deep copy of the object instead of the exact same reference, for security reasons.
-
verify
Verifies thatthis hashis the hash of the provided input text.- Parameters:
expectedInput- The string that is assumed to have been the input forthis hash. Must not be blank.- Returns:
trueif this hash corresponds to the provided input text,falseotherwise.- Throws:
IllegalArgumentException- If the provided input text is blank.
-
toBase64
Converts thehashand theargon2Configurationto a string representation that contains all the information necessary toconvertit back to aHashingResultobject. The string representation is in the format$[base64]$[base64]$[base64]$[base64]$[hash]with:- The string
argon2id, Base64-encoded. -
v=[version](Base64-encoded) where[version]isArgon2Version.version. -
m=[memory],t=[iterations],p=[parallelism](Base64-encoded) with,invalid reference
Argon2Configuration#getMemory()andinvalid reference
Argon2Configuration#getIterations().invalid reference
Argon2Configuration#getParallelism() - The Base64-encoded
.
invalid reference
salt - The base64-encoded
hashof the input text.
- Returns:
- The hash as a string with all necessary information to verify it.
- See Also:
- The string
-
toString
Converts thehashand theargon2Configurationto a string representation that contains all the information necessary toconvertit back to aHashingResultobject. The string representation is in the format
$argon2id$v=[version]$m=[memory],t=[iterations],p=[parallelism]$[salt]where[version]isArgon2Version.version[memory]isinvalid reference
Argon2Configuration#getMemory()[iterations]isinvalid reference
Argon2Configuration#getIterations()[parallelism]isinvalid reference
Argon2Configuration#getParallelism()[salt]is the base64-encodedinvalid reference
Argon2Configuration#getSalt()[hash]is the base64-encodedhashof the input text.
-
parse
@NonNull public static @NonNull HashingResult parse(@NonNull @NonNull String hash) throws IllegalArgumentException - Parameters:
hash- The string representation of aHashingResultobject, in the format$argon2id$v=[version]$m=[memory],t=[iterations],p=[parallelism]$[salt]where[version]isArgon2Version.version[memory]isinvalid reference
Argon2Configuration#getMemory()[iterations]isinvalid reference
Argon2Configuration#getIterations()[parallelism]isinvalid reference
Argon2Configuration#getParallelism()[salt]is the base64-encodedinvalid reference
Argon2Configuration#getSalt()[hash]is the base64-encodedhashof the input text.
- Returns:
- The
HashingResultobject that was represented by the provided string. - Throws:
IllegalArgumentException- If the provided string is not a validHashingResultstring representation or if theArgon2 configurationcannot beparsed.
-
parseBase64
@NonNull public static @NonNull HashingResult parseBase64(@NonNull @NonNull String hash) throws IllegalArgumentException - Parameters:
hash- The base64 string representation of aHashingResultobject, in the format$[base64]$[base64]$[base64]$[base64]$[hash]with:- The string
argon2id, Base64-encoded. -
v=[version](Base64-encoded) where[version]isArgon2Version.version. -
m=[memory],t=[iterations],p=[parallelism](Base64-encoded) with,invalid reference
Argon2Configuration#getMemory()andinvalid reference
Argon2Configuration#getIterations().invalid reference
Argon2Configuration#getParallelism() - The Base64-encoded
.
invalid reference
salt - The base64-encoded
hashof the input text.
- The string
- Returns:
- The
HashingResultobject that was represented by the provided string. - Throws:
IllegalArgumentException- If any of the parts specified bytoBase64()are missing/invalid or if there are unexpected parts.
-
copy
Creates a deep copy of thisHashingResultobject.- Specified by:
copyin interfaceCopyable<HashingResult>- Returns:
- A new
HashingResultobject with the samehashandArgon2 configurationas this object.
-
equals
Compares all fields of thisHashingResultobject to the fields of anotherHashingResultobject.- Parameters:
other- The otherHashingResultobject to compare this object to.- Returns:
-
FuzzyBoolean.TRUEif thehashesand theArgon2 configurationsof both objects are equal. -
FuzzyBoolean.FALSEif theArgon2 configurationsof both objects are equal, but thehashesare different. -
FuzzyBoolean.MAYBEif theArgon2 configurationsof both objects are different. If we compute the Argon2 hash of a string with two different Argon2 configurations, we will always get two different hashes. Therefore, if the two configurations differ, this does not permit us to draw any conclusions about the hash inputs.
-
-