Class AbstractHash
java.lang.Object
org.apache.shiro.lang.codec.CodecSupport
org.apache.shiro.crypto.hash.AbstractHash
- All Implemented Interfaces:
Serializable,Hash,org.apache.shiro.lang.util.ByteSource
@Deprecated
public abstract class AbstractHash
extends org.apache.shiro.lang.codec.CodecSupport
implements Hash, Serializable
Deprecated.
Provides a base for all Shiro Hash algorithms with support for salts and multiple hash iterations.
Read
http://www.owasp.org/index.php/Hashing_Java
for a good article on the benefits of hashing, including what a 'salt' is as well as why it and multiple hash
iterations can be useful.
This class and its subclasses support hashing with additional capabilities of salting and multiple iterations via
overloaded constructors.
- Since:
- 0.9
- 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 inherited from class org.apache.shiro.lang.codec.CodecSupport
PREFERRED_ENCODING -
Constructor Summary
ConstructorsConstructorDescriptionDeprecated.Creates an new instance without any of its properties set (no hashing is performed).AbstractHash(Object source) Deprecated.Creates a hash of the specifiedsourcewith nosaltusing a single hash iteration.AbstractHash(Object source, Object salt) Deprecated.Creates a hash of the specifiedsourceusing the givensaltusing a single hash iteration.AbstractHash(Object source, Object salt, int hashIterations) Deprecated.Creates a hash of the specifiedsourceusing the givensalta total ofhashIterationstimes. -
Method Summary
Modifier and TypeMethodDescriptionbooleanDeprecated.Returnstrueif the specified object is a Hash and itsbyte arrayis identical to this Hash's byte array,falseotherwise.abstract StringDeprecated.Implemented by subclasses, this specifies theMessageDigestalgorithm name to use when performing the hash.byte[]getBytes()Deprecated.protected MessageDigestDeprecated.Returns the JDK MessageDigest instance to use for executing the hash.protected byte[]hash(byte[] bytes) Deprecated.Hashes the specified byte array without a salt for a single iteration.protected byte[]hash(byte[] bytes, byte[] salt) Deprecated.Hashes the specified byte array using the givensaltfor a single iteration.protected byte[]hash(byte[] bytes, byte[] salt, int hashIterations) Deprecated.Hashes the specified byte array using the givensaltfor the specified number of iterations.inthashCode()Deprecated.Simply returns toHex().hashCode();voidsetBytes(byte[] alreadyHashedBytes) Deprecated.Sets the raw bytes stored by this hash instance.toBase64()Deprecated.Returns a Base64-encoded string of the underlyingbyte array.toHex()Deprecated.Returns a hex-encoded string of the underlyingbyte array.toString()Deprecated.Simple implementation that merely returnstoHex().Methods inherited from class org.apache.shiro.lang.codec.CodecSupport
isByteSource, objectToBytes, objectToString, toBytes, toBytes, toBytes, toBytes, toBytes, toBytes, toBytes, toChars, toChars, toString, toString, toStringMethods inherited from class java.lang.Object
clone, finalize, getClass, notify, notifyAll, wait, wait, waitMethods inherited from interface org.apache.shiro.lang.util.ByteSource
isEmptyMethods inherited from interface org.apache.shiro.crypto.hash.Hash
getIterations, getSalt, matchesPassword
-
Constructor Details
-
AbstractHash
public AbstractHash()Deprecated.Creates an new instance without any of its properties set (no hashing is performed). Because all constructors in this class (except this one) hash thesourceconstructor argument, this default, no-arg constructor is useful in scenarios when you have a byte array that you know is already hashed and just want to set the bytes in their raw form directly on an instance. After instantiating the instance with this default, no-arg constructor, you can then immediately callsetBytesto have a fully-initialized instance. -
AbstractHash
Deprecated.Creates a hash of the specifiedsourcewith nosaltusing a single hash iteration. It is a convenience constructor that merely executesthis( source, null, 1);. Please see theAbstractHash(Object,Object,int)constructor for the types of Objects that may be passed into this constructor, as well as how to support further types.- Parameters:
source- the object to be hashed.- Throws:
org.apache.shiro.lang.codec.CodecException- if the specifiedsourcecannot be converted into a byte array (byte[]).
-
AbstractHash
Deprecated.Creates a hash of the specifiedsourceusing the givensaltusing a single hash iteration. It is a convenience constructor that merely executesthis( source, salt, 1);. Please see theAbstractHash(Object,Object,int)constructor for the types of Objects that may be passed into this constructor, as well as how to support further types.- Parameters:
source- the source object to be hashed.salt- the salt to use for the hash- Throws:
org.apache.shiro.lang.codec.CodecException- if either constructor argument cannot be converted into a byte array.
-
AbstractHash
public AbstractHash(Object source, Object salt, int hashIterations) throws org.apache.shiro.lang.codec.CodecException Deprecated.Creates a hash of the specifiedsourceusing the givensalta total ofhashIterationstimes. By default, this class only supports Object method arguments of typebyte[],char[],String,File, orInputStream. If either argument is anything other than these types aCodecExceptionwill be thrown. If you want to be able to hash other object types, or use other salt types, you need to override thetoBytes(Object)method to support those specific types. Your other option is to convert your arguments to one of the default three supported types first before passing them in to this constructor}.- Parameters:
source- the source object to be hashed.salt- the salt to use for the hashhashIterations- the number of times thesourceargument hashed for attack resiliency.- Throws:
org.apache.shiro.lang.codec.CodecException- if either Object constructor argument cannot be converted into a byte array.
-
-
Method Details
-
getAlgorithmName
Deprecated.Implemented by subclasses, this specifies theMessageDigestalgorithm name to use when performing the hash.- Specified by:
getAlgorithmNamein interfaceHash- Returns:
- the
MessageDigestalgorithm name to use when performing the hash.
-
getBytes
Deprecated.- Specified by:
getBytesin interfaceorg.apache.shiro.lang.util.ByteSource
-
setBytes
Deprecated.Sets the raw bytes stored by this hash instance. The bytes are kept in raw form - they will not be hashed/changed. This is primarily a utility method for constructing a Hash instance when the hashed value is already known.- Parameters:
alreadyHashedBytes- the raw already-hashed bytes to store in this instance.
-
getDigest
protected MessageDigest getDigest(String algorithmName) throws org.apache.shiro.crypto.UnknownAlgorithmException Deprecated.Returns the JDK MessageDigest instance to use for executing the hash.- Parameters:
algorithmName- the algorithm to use for the hash, provided by subclasses.- Returns:
- the MessageDigest object for the specified
algorithm. - Throws:
org.apache.shiro.crypto.UnknownAlgorithmException- if the specified algorithm name is not available.
-
hash
Deprecated.Hashes the specified byte array without a salt for a single iteration.- Parameters:
bytes- the bytes to hash.- Returns:
- the hashed bytes.
-
hash
Deprecated.Hashes the specified byte array using the givensaltfor a single iteration.- Parameters:
bytes- the bytes to hashsalt- the salt to use for the initial hash- Returns:
- the hashed bytes
-
hash
protected byte[] hash(byte[] bytes, byte[] salt, int hashIterations) throws org.apache.shiro.crypto.UnknownAlgorithmException Deprecated.Hashes the specified byte array using the givensaltfor the specified number of iterations.- Parameters:
bytes- the bytes to hashsalt- the salt to use for the initial hashhashIterations- the number of times the thebyteswill be hashed (for attack resiliency).- Returns:
- the hashed bytes.
- Throws:
org.apache.shiro.crypto.UnknownAlgorithmException- if thealgorithmNameis not available.
-
toHex
Deprecated.Returns a hex-encoded string of the underlyingbyte array. This implementation caches the resulting hex string so multiple calls to this method remain efficient. However, callingsetByteswill null the cached value, forcing it to be recalculated the next time this method is called.- Specified by:
toHexin interfaceorg.apache.shiro.lang.util.ByteSource- Returns:
- a hex-encoded string of the underlying
byte array.
-
toBase64
Deprecated.Returns a Base64-encoded string of the underlyingbyte array. This implementation caches the resulting Base64 string so multiple calls to this method remain efficient. However, callingsetByteswill null the cached value, forcing it to be recalculated the next time this method is called.- Specified by:
toBase64in interfaceorg.apache.shiro.lang.util.ByteSource- Returns:
- a Base64-encoded string of the underlying
byte array.
-
toString
-
equals
Deprecated.Returnstrueif the specified object is a Hash and itsbyte arrayis identical to this Hash's byte array,falseotherwise.- Overrides:
equalsin classObject- Parameters:
o- the object (Hash) to check for equality.- Returns:
trueif the specified object is a Hash and itsbyte arrayis identical to this Hash's byte array,falseotherwise.
-
hashCode
-
SimpleHashimplementation directly.