Class AbstractCryptHash
- All Implemented Interfaces:
Serializable,Hash,org.apache.shiro.lang.util.ByteSource
These implementations must contain a salt, a salt length, can format themselves to a valid String
suitable for the /etc/shadow file.
It also defines the hex and base64 output by wrapping the output of formatToCryptString().
Implementation notice: Implementations should provide a static fromString() method.
- Since:
- 2.0
- See Also:
-
Nested Class Summary
Nested classes/interfaces inherited from interface org.apache.shiro.lang.util.ByteSource
org.apache.shiro.lang.util.ByteSource.Util -
Field Summary
Fields -
Constructor Summary
ConstructorsConstructorDescriptionAbstractCryptHash(String algorithmName, byte[] hashedData, org.apache.shiro.lang.util.ByteSource salt) Constructs anAbstractCryptHashusing the algorithm name, hashed data and salt parameters. -
Method Summary
Modifier and TypeMethodDescriptionprotected final voidprotected abstract voidAlgorithm-specific checks of the algorithm’s parameters.protected voidDefault check method for a valid salt.booleanReturnstrueif the specified object is an AbstractCryptHash and itsformatToCryptString()formatted output} is identical to this AbstractCryptHash's formatted output,falseotherwise.abstract StringThis method MUST return a single-lined string which would also be recognizable by a posix/etc/passwdfile.Implemented by subclasses, this specifies the KDF algorithm name to use when performing the hash.byte[]getBytes()Returns only the hashed data.org.apache.shiro.lang.util.ByteSourcegetSalt()Returns a salt used to compute the hash ornullif no salt was used.abstract intThe length in number of bytes of the salt which is needed for this algorithm.inthashCode()Hashes the formatted crypt string.booleanisEmpty()toBase64()Returns a Base64-encoded string of the underlyingformatToCryptString()formatted output}.toHex()Returns a hex-encoded string of the underlyingformatToCryptString()formatted output}.toString()Simple implementation that merely returnstoHex().Methods inherited from class java.lang.Object
clone, finalize, getClass, notify, notifyAll, wait, wait, waitMethods inherited from interface org.apache.shiro.crypto.hash.Hash
getIterations, matchesPassword
-
Field Details
-
DELIMITER
-
-
Constructor Details
-
AbstractCryptHash
public AbstractCryptHash(String algorithmName, byte[] hashedData, org.apache.shiro.lang.util.ByteSource salt) Constructs anAbstractCryptHashusing the algorithm name, hashed data and salt parameters.Other required parameters must be stored by the implementation.
- Parameters:
algorithmName- internal algorithm name, e.g.2yfor bcrypt andargon2idfor argon2.hashedData- the hashed data as a byte array. Does not include the salt or other parameters.salt- the salt which was used when generating the hash.- Throws:
IllegalArgumentException- if the salt is not the same size asgetSaltLength().
-
-
Method Details
-
checkValid
-
checkValidAlgorithm
Algorithm-specific checks of the algorithm’s parameters.While the salt length will be checked by default, other checks will be useful. Examples are: Argon2 checking for the memory and parallelism parameters, bcrypt checking for the cost parameters being in a valid range.
- Throws:
IllegalArgumentException- if any of the parameters are invalid.
-
checkValidSalt
Default check method for a valid salt. Can be overridden, because multiple salt lengths could be valid.By default, this method checks if the number of bytes in the salt are equal to the int returned by
getSaltLength().- Throws:
IllegalArgumentException- if the salt length does not match the returned value ofgetSaltLength().
-
getAlgorithmName
Implemented by subclasses, this specifies the KDF algorithm name to use when performing the hash.When multiple algorithm names are acceptable, then this method should return the primary algorithm name.
Example: Bcrypt hashed can be identified by
2yand2a. The method will return2yfor newly generated hashes by default, unless otherwise overridden.- Specified by:
getAlgorithmNamein interfaceHash- Returns:
- the KDF algorithm name to use when performing the hash.
-
getSaltLength
The length in number of bytes of the salt which is needed for this algorithm.- Returns:
- the expected length of the salt (in bytes).
-
getSalt
-
getBytes
Returns only the hashed data. Those are of no value on their own. If you need to serialize the hash, please refer toformatToCryptString().- Specified by:
getBytesin interfaceorg.apache.shiro.lang.util.ByteSource- Returns:
- A copy of the hashed data as bytes.
- See Also:
-
isEmpty
- Specified by:
isEmptyin interfaceorg.apache.shiro.lang.util.ByteSource
-
toHex
Returns a hex-encoded string of the underlyingformatToCryptString()formatted output}. This implementation caches the resulting hex string so multiple calls to this method remain efficient.- Specified by:
toHexin interfaceorg.apache.shiro.lang.util.ByteSource- Returns:
- a hex-encoded string of the underlying
formatToCryptString()formatted output}.
-
toBase64
Returns a Base64-encoded string of the underlyingformatToCryptString()formatted output}. This implementation caches the resulting Base64 string so multiple calls to this method remain efficient.- Specified by:
toBase64in interfaceorg.apache.shiro.lang.util.ByteSource- Returns:
- a Base64-encoded string of the underlying
formatToCryptString()formatted output}.
-
formatToCryptString
This method MUST return a single-lined string which would also be recognizable by a posix/etc/passwdfile.- Returns:
- a formatted string, e.g.
$2y$10$7rOjsAf2U/AKKqpMpCIn6e$tuOXyQ86tp2Tn9xv6FyXl2T0QYc3.G.for bcrypt.
-
equals
Returnstrueif the specified object is an AbstractCryptHash and itsformatToCryptString()formatted output} is identical to this AbstractCryptHash's formatted output,falseotherwise.- Overrides:
equalsin classObject- Parameters:
other- the object (AbstractCryptHash) to check for equality.- Returns:
trueif the specified object is a AbstractCryptHash and itsformatToCryptString()formatted output} is identical to this AbstractCryptHash's formatted output,falseotherwise.
-
hashCode
Hashes the formatted crypt string.Implementations should not override this method, as different algorithms produce different output formats and require different parameters.
- Overrides:
hashCodein classObject- Returns:
- a hashcode from the
formatted output.
-
toString
-