Class StandardByteDigester
- Object
-
- org.jasypt.digest.StandardByteDigester
-
- All Implemented Interfaces:
ByteDigester
public final class StandardByteDigester extends Object implements ByteDigester
Standard implementation of the
ByteDigesterinterface. This class lets the user specify the algorithm (and provider) to be used for creating digests, the size of the salt to be applied, the number of times the hash function will be applied (iterations) and the salt generator to be used.This class is thread-safe.
ConfigurationThe algorithm, provider, salt size iterations and salt generator can take values in any of these ways:
- Using its default values.
- Setting a
DigesterConfigobject which provides new configuration values. - Calling the corresponding setAlgorithm(...), setProvider(...), setProviderName(...), setSaltSizeBytes(...), setIterations(...) or setSaltGenerator(...) methods.
- First, the default values are considered.
- Then, if a
DigesterConfigobject has been set with setConfig, the non-null values returned by its getX methods override the default values. - Finally, if the corresponding setX method has been called on the digester itself for any of the configuration parameters, the values set by these calls override all of the above.
InitializationBefore it is ready to create digests, an object of this class has to be initialized. Initialization happens:
- When initialize is called.
- When digest or matches are called for the first time, if initialize has not been called before.
UsageA digester may be used in two different ways:
- For creating digests, by calling the digest method.
- For matching digests, this is, checking whether a digest corresponds adequately to a digest (as in password checking) or not, by calling the matches method.
- A salt of the specified size is generated (see
SaltGenerator). If salt size is zero, no salt will be used. - The salt bytes are added to the message.
- The hash function is applied to the salt and message altogether, and then to the results of the function itself, as many times as specified (iterations).
- If specified by the salt generator (see
SaltGenerator.includePlainSaltInEncryptionResults()), the undigested salt and the final result of the hash function are concatenated and returned as a result.
-
DIGEST = |S|..(ssb)..|S|X|X|X|...|X|
- S: salt bytes (plain, not digested). (OPTIONAL).
- ssb: salt size in bytes.
- X: bytes resulting from hashing (see below).
-
|X|X|X|...|X| =
H(H(H(..(it)..H(Z|Z|Z|...|Z|))))
- H: Hash function (algorithm).
- it: Number of iterations.
- Z: Input for hashing (see below).
-
|Z|Z|Z|...|Z| =
|S|..(ssb)..|S|M|M|M...|M|
- S: salt bytes (plain, not digested).
- ssb: salt size in bytes.
- M: message bytes.
To learn more about the mechanisms involved in digest creation, read PKCS #5: Password-Based Cryptography Standard.
- Since:
- 1.0
- Author:
- Daniel Fernández
-
-
Field Summary
Fields Modifier and Type Field Description static StringDEFAULT_ALGORITHMDefault digest algorithm will be MD5static intDEFAULT_ITERATIONSThe minimum recommended iterations for hashing are 1000static intDEFAULT_SALT_SIZE_BYTESThe minimum recommended size for salt is 8 bytes
-
Constructor Summary
Constructors Constructor Description StandardByteDigester()Creates a new instance of StandardByteDigester.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description byte[]digest(byte[] message)Performs a digest operation on a byte array message.voidinitialize()Initialize the digester.booleanisInitialized()Returns true if the digester has already been initialized, false if not.
Initialization happens:booleanmatches(byte[] message, byte[] digest)Checks a message against a given digest.voidsetAlgorithm(String algorithm)Sets the algorithm to be used for digesting, like MD5 or SHA-1.voidsetConfig(DigesterConfig config)Sets aDigesterConfigobject for the digester.voidsetInvertPositionOfPlainSaltInEncryptionResults(boolean invertPositionOfPlainSaltInEncryptionResults)Whether the plain (not hashed) salt bytes are to be appended after the digest operation result bytes.voidsetInvertPositionOfSaltInMessageBeforeDigesting(boolean invertPositionOfSaltInMessageBeforeDigesting)Whether the salt bytes are to be appended after the message ones before performing the digest operation on the whole.voidsetIterations(int iterations)Set the number of times the hash function will be applied recursively.voidsetProvider(java.security.Provider provider)Sets the security provider to be asked for the digest algorithm.voidsetProviderName(String providerName)Sets the name of the security provider to be asked for the digest algorithm.voidsetSaltGenerator(SaltGenerator saltGenerator)Sets the salt generator to be used.voidsetSaltSizeBytes(int saltSizeBytes)Sets the size of the salt to be used to compute the digest.voidsetUseLenientSaltSizeCheck(boolean useLenientSaltSizeCheck)Whether digest matching operations will allow matching digests with a salt size different to the one configured in the "saltSizeBytes" property.
-
-
-
Field Detail
-
DEFAULT_ALGORITHM
public static final String DEFAULT_ALGORITHM
Default digest algorithm will be MD5- See Also:
- Constant Field Values
-
DEFAULT_SALT_SIZE_BYTES
public static final int DEFAULT_SALT_SIZE_BYTES
The minimum recommended size for salt is 8 bytes- See Also:
- Constant Field Values
-
DEFAULT_ITERATIONS
public static final int DEFAULT_ITERATIONS
The minimum recommended iterations for hashing are 1000- See Also:
- Constant Field Values
-
-
Method Detail
-
setConfig
public void setConfig(DigesterConfig config)
Sets a
DigesterConfigobject for the digester. If this config object is set, it will be asked values for:- Algorithm
- Security Provider (or provider name)
- Salt size
- Hashing iterations
- Salt generator
- Location of the salt in relation to the encrypted message (default: before)
The non-null values it returns will override the default ones, and will be overriden by any values specified with a setX method.
- Parameters:
config- the DigesterConfig object to be used as the source for configuration parameters.
-
setAlgorithm
public void setAlgorithm(String algorithm)
Sets the algorithm to be used for digesting, like MD5 or SHA-1.
This algorithm has to be supported by your security infrastructure, and it should be allowed as an algorithm for creating java.security.MessageDigest instances.
If you are specifying a security provider with
setProvider(Provider)orsetProviderName(String), this algorithm should be supported by your specified provider.If you are not specifying a provider, you will be able to use those algorithms provided by the default security provider of your JVM vendor. For valid names in the Sun JVM, see Java Cryptography Architecture API Specification & Reference.
- Parameters:
algorithm- the name of the algorithm to be used.
-
setSaltSizeBytes
public void setSaltSizeBytes(int saltSizeBytes)
Sets the size of the salt to be used to compute the digest. This mechanism is explained in PKCS #5: Password-Based Cryptography Standard.
If salt size is set to zero, then no salt will be used.
- Parameters:
saltSizeBytes- the size of the salt to be used, in bytes.
-
setIterations
public void setIterations(int iterations)
Set the number of times the hash function will be applied recursively.
The hash function will be applied to its own results as many times as specified: h(h(...h(x)...))This mechanism is explained in PKCS #5: Password-Based Cryptography Standard.
- Parameters:
iterations- the number of iterations.
-
setSaltGenerator
public void setSaltGenerator(SaltGenerator saltGenerator)
Sets the salt generator to be used. If no salt generator is specified, an instance of
RandomSaltGeneratorwill be used.- Parameters:
saltGenerator- the salt generator to be used.- Since:
- 1.2
-
setProviderName
public void setProviderName(String providerName)
Sets the name of the security provider to be asked for the digest algorithm. This security provider has to be registered beforehand at the JVM security framework.
The provider can also be set with the
setProvider(Provider)method, in which case it will not be necessary neither registering the provider beforehand, nor calling thissetProviderName(String)method to specify a provider name.Note that a call to
setProvider(Provider)overrides any value set by this method.If no provider name / provider is explicitly set, the default JVM provider will be used.
- Parameters:
providerName- the name of the security provider to be asked for the digest algorithm.- Since:
- 1.3
-
setProvider
public void setProvider(java.security.Provider provider)
Sets the security provider to be asked for the digest algorithm. The provider does not have to be registered at the security infrastructure beforehand, and its being used here will not result in its being registered.
If this method is called, calling
setProviderName(String)becomes unnecessary.If no provider name / provider is explicitly set, the default JVM provider will be used.
- Parameters:
provider- the provider to be asked for the chosen algorithm- Since:
- 1.3
-
setInvertPositionOfSaltInMessageBeforeDigesting
public void setInvertPositionOfSaltInMessageBeforeDigesting(boolean invertPositionOfSaltInMessageBeforeDigesting)
Whether the salt bytes are to be appended after the message ones before performing the digest operation on the whole. The default behaviour is to insert those bytes before the message bytes, but setting this configuration item to true allows compatibility with some external systems and specifications (e.g. LDAP {SSHA}).
If this parameter is not explicitly set, the default behaviour (insertion of salt before message) will be applied.
- Parameters:
invertPositionOfSaltInMessageBeforeDigesting- whether salt will be appended after the message before applying the digest operation on the whole, instead of inserted before it (which is the default).- Since:
- 1.7
-
setInvertPositionOfPlainSaltInEncryptionResults
public void setInvertPositionOfPlainSaltInEncryptionResults(boolean invertPositionOfPlainSaltInEncryptionResults)
Whether the plain (not hashed) salt bytes are to be appended after the digest operation result bytes. The default behaviour is to insert them before the digest result, but setting this configuration item to true allows compatibility with some external systems and specifications (e.g. LDAP {SSHA}).
If this parameter is not explicitly set, the default behaviour (insertion of plain salt before digest result) will be applied.
- Parameters:
invertPositionOfPlainSaltInEncryptionResults- whether plain salt will be appended after the digest operation result instead of inserted before it (which is the default).- Since:
- 1.7
-
setUseLenientSaltSizeCheck
public void setUseLenientSaltSizeCheck(boolean useLenientSaltSizeCheck)
Whether digest matching operations will allow matching digests with a salt size different to the one configured in the "saltSizeBytes" property. This is possible because digest algorithms will produce a fixed-size result, so the remaining bytes from the hashed input will be considered salt.
This will allow the digester to match digests produced in environments which do not establish a fixed salt size as standard (for example, SSHA password encryption in LDAP systems).
The value of this property will not affect the creation of digests, which will always have a salt of the size established by the "saltSizeBytes" property. It will only affect digest matching.
Setting this property to true is not compatible with
SaltGeneratorimplementations which return false for theirSaltGenerator.includePlainSaltInEncryptionResults()property.Also, be aware that some algorithms or algorithm providers might not support knowing the size of the digests beforehand, which is also incompatible with a lenient behaviour.
If this parameter is not explicitly set, the default behaviour (NOT lenient) will be applied.
- Parameters:
useLenientSaltSizeCheck- whether the digester will allow matching of digests with different salt sizes than established or not (default is false).- Since:
- 1.7
-
isInitialized
public boolean isInitialized()
Returns true if the digester has already been initialized, false if not.
Initialization happens:- When initialize is called.
- When digest or matches are called for the first time, if initialize has not been called before.
Once a digester has been initialized, trying to change its configuration (algorithm, provider, salt size, iterations or salt generator) will result in an AlreadyInitializedException being thrown.
- Returns:
- true if the digester has already been initialized, false if not.
-
initialize
public void initialize()
Initialize the digester.
This operation will consist in determining the actual configuration values to be used, and then initializing the digester with them.
These values are decided by applying the following priorities:- First, the default values are considered.
- Then, if a
DigesterConfigobject has been set with setConfig, the non-null values returned by its getX methods override the default values. - Finally, if the corresponding setX method has been called on the digester itself for any of the configuration parameters, the values set by these calls override all of the above.
Once a digester has been initialized, trying to change its configuration will result in an AlreadyInitializedException being thrown.
- Throws:
EncryptionInitializationException- if initialization could not be correctly done (for example, if the digest algorithm chosen cannot be used).
-
digest
public byte[] digest(byte[] message)
Performs a digest operation on a byte array message.
The steps taken for creating the digest are:
- A salt of the specified size is generated (see
SaltGenerator). - The salt bytes are added to the message.
- The hash function is applied to the salt and message altogether, and then to the results of the function itself, as many times as specified (iterations).
- If specified by the salt generator (see
SaltGenerator.includePlainSaltInEncryptionResults()), the undigested salt and the final result of the hash function are concatenated and returned as a result.
-
DIGEST = |S|..(ssb)..|S|X|X|X|...|X|
- S: salt bytes (plain, not digested). (OPTIONAL).
- ssb: salt size in bytes.
- X: bytes resulting from hashing (see below).
-
|X|X|X|...|X| =
H(H(H(..(it)..H(Z|Z|Z|...|Z|))))
- H: Hash function (algorithm).
- it: Number of iterations.
- Z: Input for hashing (see below).
-
|Z|Z|Z|...|Z| =
|S|..(ssb)..|S|M|M|M...|M|
- S: salt bytes (plain, not digested).
- ssb: salt size in bytes.
- M: message bytes.
If a random salt generator is used, two digests created for the same message will always be different (except in the case of random salt coincidence). Because of this, in this case the result of the digest method will contain both the undigested salt and the digest of the (salt + message), so that another digest operation can be performed with the same salt on a different message to check if both messages match (all of which will be managed automatically by the matches method).
- Specified by:
digestin interfaceByteDigester- Parameters:
message- the byte array to be digested- Returns:
- the digest result
- Throws:
EncryptionOperationNotPossibleException- if the digest operation fails, ommitting any further information about the cause for security reasons.EncryptionInitializationException- if initialization could not be correctly done (for example, if the digest algorithm chosen cannot be used).
- A salt of the specified size is generated (see
-
matches
public boolean matches(byte[] message, byte[] digest)Checks a message against a given digest.
This method tells whether a message corresponds to a specific digest or not by getting the salt with which the digest was created and applying it to a digest operation performed on the message. If new and existing digest match, the message is said to match the digest.
This method will be used, for instance, for password checking in authentication processes.
A null message will only match a null digest.
- Specified by:
matchesin interfaceByteDigester- Parameters:
message- the message to be compared to the digest.digest- the digest.- Returns:
- true if the specified message matches the digest, false if not.
- Throws:
EncryptionOperationNotPossibleException- if the digest matching operation fails, ommitting any further information about the cause for security reasons.EncryptionInitializationException- if initialization could not be correctly done (for example, if the digest algorithm chosen cannot be used).
-
-