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
null
or empty and throw anIllegalArgumentException
if that's the case. While that could be done in a record as well, we cannot addthrows IllegalArgumentException
to 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 Argon2Configuration
The Argon2 parameters that were used to compute the hash.private final byte[]
-
Constructor Summary
ConstructorsConstructorDescriptionHashingResult
(byte[] hash, @NonNull Argon2Configuration argon2Configuration) Constructs a newHashingResult
object with the providedArgon2 hash
and theArgon2 parameters
that were used to compute the hash. -
Method Summary
Modifier and TypeMethodDescription@NonNull HashingResult
copy()
Creates a deep copy of thisHashingResult
object.@NonNull FuzzyBoolean
equals
(@NonNull HashingResult other) Compares all fields of thisHashingResult
object to the fields of anotherHashingResult
object.@NonNull Argon2Configuration
static @NonNull HashingResult
static @NonNull HashingResult
parseBase64
(@NonNull String hash) @NonNull String
toBase64()
Converts thehash
and theargon2Configuration
to a string representation that contains all the information necessary toconvert
it back to aHashingResult
object.@NonNull String
toString()
Converts thehash
and theargon2Configuration
to a string representation that contains all the information necessary toconvert
it back to aHashingResult
object.boolean
Verifies thatthis hash
is 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 newHashingResult
object with the providedArgon2 hash
and theArgon2 parameters
that were used to compute the hash.- Parameters:
hash
- TheArgon2 hash
of the input text.argon2Configuration
- The Argon2 parameters that were used to compute the hash.- Throws:
IllegalArgumentException
- If the provided hash isnull
or empty.
-
-
Method Details
-
getArgon2Configuration
- Returns:
- A deep copy of the object instead of the exact same reference, for security reasons.
-
verify
Verifies thatthis hash
is 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:
true
if this hash corresponds to the provided input text,false
otherwise.- Throws:
IllegalArgumentException
- If the provided input text is blank.
-
toBase64
Converts thehash
and theargon2Configuration
to a string representation that contains all the information necessary toconvert
it back to aHashingResult
object. 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) withinvalid reference
Argon2Configuration#getMemory()
invalid reference
Argon2Configuration#getIterations()
invalid reference
Argon2Configuration#getParallelism()
- The Base64-encoded
invalid reference
salt
- The base64-encoded
hash
of the input text.
- Returns:
- The hash as a string with all necessary information to verify it.
- See Also:
- The string
-
toString
Converts thehash
and theargon2Configuration
to a string representation that contains all the information necessary toconvert
it back to aHashingResult
object. 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-encodedhash
of the input text.
-
parse
@NonNull public static @NonNull HashingResult parse(@NonNull @NonNull String hash) throws IllegalArgumentException - Parameters:
hash
- The string representation of aHashingResult
object, 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-encodedhash
of the input text.
- Returns:
- The
HashingResult
object that was represented by the provided string. - Throws:
IllegalArgumentException
- If the provided string is not a validHashingResult
string representation or if theArgon2 configuration
cannot beparsed
.
-
parseBase64
@NonNull public static @NonNull HashingResult parseBase64(@NonNull @NonNull String hash) throws IllegalArgumentException - Parameters:
hash
- The base64 string representation of aHashingResult
object, 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) withinvalid reference
Argon2Configuration#getMemory()
invalid reference
Argon2Configuration#getIterations()
invalid reference
Argon2Configuration#getParallelism()
- The Base64-encoded
invalid reference
salt
- The base64-encoded
hash
of the input text.
- The string
- Returns:
- The
HashingResult
object 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 thisHashingResult
object.- Specified by:
copy
in interfaceCopyable<HashingResult>
- Returns:
- A new
HashingResult
object with the samehash
andArgon2 configuration
as this object.
-
equals
Compares all fields of thisHashingResult
object to the fields of anotherHashingResult
object.- Parameters:
other
- The otherHashingResult
object to compare this object to.- Returns:
-
FuzzyBoolean.TRUE
if thehashes
and theArgon2 configurations
of both objects are equal. -
FuzzyBoolean.FALSE
if theArgon2 configurations
of both objects are equal, but thehashes
are different. -
FuzzyBoolean.MAYBE
if theArgon2 configurations
of 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.
-
-