|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Object org.apache.shiro.codec.CodecSupport org.apache.shiro.crypto.hash.AbstractHash org.apache.shiro.crypto.hash.SimpleHash
public class SimpleHash
A Hash
implementation that allows any MessageDigest
algorithm name to
be used. This class is a less type-safe variant than the other AbstractHash
subclasses
(e.g. Sha512Hash
, etc), but it does allow for any algorithm name to be specified in case the other subclass
implementations do not represent an algorithm that you may want to use.
AbstractHash
class. It subclasses
AbstractHash
only to retain backwards-compatibility.
Nested Class Summary |
---|
Nested classes/interfaces inherited from interface org.apache.shiro.util.ByteSource |
---|
ByteSource.Util |
Field Summary |
---|
Fields inherited from class org.apache.shiro.codec.CodecSupport |
---|
PREFERRED_ENCODING |
Constructor Summary | |
---|---|
SimpleHash(String algorithmName)
Creates an new instance with only its algorithmName set - no hashing is performed. |
|
SimpleHash(String algorithmName,
Object source)
Creates an algorithmName -specific hash of the specified source with no salt using a
single hash iteration. |
|
SimpleHash(String algorithmName,
Object source,
Object salt)
Creates an algorithmName -specific hash of the specified source using the given salt
using a single hash iteration. |
|
SimpleHash(String algorithmName,
Object source,
Object salt,
int hashIterations)
Creates an algorithmName -specific hash of the specified source using the given
salt a total of hashIterations times. |
Method Summary | |
---|---|
protected ByteSource |
convertSaltToBytes(Object salt)
Acquires the specified salt argument's bytes and returns them in the form of a ByteSource instance. |
protected ByteSource |
convertSourceToBytes(Object source)
Acquires the specified source argument's bytes and returns them in the form of a ByteSource instance. |
boolean |
equals(Object o)
Returns true if the specified object is a Hash and its byte array is identical to
this Hash's byte array, false otherwise. |
String |
getAlgorithmName()
Returns the MessageDigest algorithm name to use when performing the hash. |
byte[] |
getBytes()
Returns the wrapped byte array. |
protected MessageDigest |
getDigest(String algorithmName)
Returns the JDK MessageDigest instance to use for executing the hash. |
int |
getIterations()
Returns the number of hash iterations used to compute the hash. |
ByteSource |
getSalt()
Returns a salt used to compute the hash or null if no salt was used. |
protected byte[] |
hash(byte[] bytes)
Hashes the specified byte array without a salt for a single iteration. |
protected byte[] |
hash(byte[] bytes,
byte[] salt)
Hashes the specified byte array using the given salt for a single iteration. |
protected byte[] |
hash(byte[] bytes,
byte[] salt,
int hashIterations)
Hashes the specified byte array using the given salt for the specified number of iterations. |
int |
hashCode()
Simply returns toHex().hashCode(); |
boolean |
isEmpty()
Returns true if the underlying wrapped byte array is null or empty (zero length), false
otherwise. |
void |
setBytes(byte[] alreadyHashedBytes)
Sets the raw bytes stored by this hash instance. |
void |
setIterations(int iterations)
Sets the iterations used to previously compute AN ALREADY GENERATED HASH. |
void |
setSalt(ByteSource salt)
Sets the salt used to previously compute AN ALREADY GENERATED HASH. |
String |
toBase64()
Returns a Base64-encoded string of the underlying byte array . |
protected ByteSource |
toByteSource(Object o)
Converts a given object into a ByteSource instance. |
String |
toHex()
Returns a hex-encoded string of the underlying byte array . |
String |
toString()
Simple implementation that merely returns toHex() . |
Methods inherited from class org.apache.shiro.codec.CodecSupport |
---|
isByteSource, objectToBytes, objectToString, toBytes, toBytes, toBytes, toBytes, toBytes, toBytes, toBytes, toChars, toChars, toString, toString, toString |
Methods inherited from class java.lang.Object |
---|
clone, finalize, getClass, notify, notifyAll, wait, wait, wait |
Constructor Detail |
---|
public SimpleHash(String algorithmName)
algorithmName
set - no hashing is performed.
Because all other constructors in this class hash the source
constructor argument, this
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 using this constructor,
you can then immediately call setBytes
to have a fully-initialized instance.
N.B.The algorithm identified by the algorithmName
parameter must be available on the JVM. If it
is not, a UnknownAlgorithmException
will be thrown when the hash is performed (not at instantiation).
algorithmName
- the MessageDigest
algorithm name to use when
performing the hash.UnknownAlgorithmException
public SimpleHash(String algorithmName, Object source) throws CodecException, UnknownAlgorithmException
algorithmName
-specific hash of the specified source
with no salt
using a
single hash iteration.
This is a convenience constructor that merely executes this( algorithmName, source, null, 1);
.
Please see the
SimpleHashHash(algorithmName, Object,Object,int)
constructor for the types of Objects that may be passed into this constructor, as well as how to support further
types.
algorithmName
- the MessageDigest
algorithm name to use when
performing the hash.source
- the object to be hashed.
CodecException
- if the specified source
cannot be converted into a byte array (byte[]).
UnknownAlgorithmException
- if the algorithmName
is not available.public SimpleHash(String algorithmName, Object source, Object salt) throws CodecException, UnknownAlgorithmException
algorithmName
-specific hash of the specified source
using the given salt
using a single hash iteration.
It is a convenience constructor that merely executes this( algorithmName, source, salt, 1);
.
Please see the
SimpleHashHash(algorithmName, Object,Object,int)
constructor for the types of Objects that may be passed into this constructor, as well as how to support further
types.
algorithmName
- the MessageDigest
algorithm name to use when
performing the hash.source
- the source object to be hashed.salt
- the salt to use for the hash
CodecException
- if either constructor argument cannot be converted into a byte array.
UnknownAlgorithmException
- if the algorithmName
is not available.public SimpleHash(String algorithmName, Object source, Object salt, int hashIterations) throws CodecException, UnknownAlgorithmException
algorithmName
-specific hash of the specified source
using the given
salt
a total of hashIterations
times.
By default, this class only supports Object method arguments of
type byte[]
, char[]
, String
, File
,
InputStream
or ByteSource
. If either
argument is anything other than these types a CodecException
will be thrown.
If you want to be able to hash other object types, or use other salt types, you need to override the
toBytes(Object)
method to support those specific types. Your other option is to
convert your arguments to one of the default supported types first before passing them in to this
constructor}.
algorithmName
- the MessageDigest
algorithm name to use when
performing the hash.source
- the source object to be hashed.salt
- the salt to use for the hashhashIterations
- the number of times the source
argument hashed for attack resiliency.
CodecException
- if either Object constructor argument cannot be converted into a byte array.
UnknownAlgorithmException
- if the algorithmName
is not available.Method Detail |
---|
protected ByteSource convertSourceToBytes(Object source)
source
argument's bytes and returns them in the form of a ByteSource
instance.
This implementation merely delegates to the convenience toByteSource(Object)
method for generic
conversion. Can be overridden by subclasses for source-specific conversion.
source
- the source object to be hashed.
ByteSource
instance.protected ByteSource convertSaltToBytes(Object salt)
salt
argument's bytes and returns them in the form of a ByteSource
instance.
This implementation merely delegates to the convenience toByteSource(Object)
method for generic
conversion. Can be overridden by subclasses for salt-specific conversion.
salt
- the salt to be use for the hash.
ByteSource
instance.protected ByteSource toByteSource(Object o)
ByteSource
instance. Assumes the object can be converted to bytes.
o
- the Object to convert into a ByteSource
instance.
ByteSource
representation of the specified object's bytes.public String getAlgorithmName()
MessageDigest
algorithm name to use when performing the hash.
getAlgorithmName
in interface Hash
getAlgorithmName
in class AbstractHash
MessageDigest
algorithm name to use when performing the hash.public ByteSource getSalt()
Hash
null
if no salt was used.
null
if no salt was used.public int getIterations()
Hash
public byte[] getBytes()
ByteSource
getBytes
in interface ByteSource
getBytes
in class AbstractHash
public void setBytes(byte[] alreadyHashedBytes)
setBytes
in class AbstractHash
alreadyHashedBytes
- the raw already-hashed bytes to store in this instance.public void setIterations(int iterations)
iterations
- the number of hash iterations used to previously create the hash/digest.public void setSalt(ByteSource salt)
salt
- the salt used to previously create the hash/digest.protected MessageDigest getDigest(String algorithmName) throws UnknownAlgorithmException
getDigest
in class AbstractHash
algorithmName
- the algorithm to use for the hash, provided by subclasses.
algorithm
.
UnknownAlgorithmException
- if the specified algorithm name is not available.protected byte[] hash(byte[] bytes) throws UnknownAlgorithmException
hash
in class AbstractHash
bytes
- the bytes to hash.
UnknownAlgorithmException
- if the configured algorithmName
is not available.protected byte[] hash(byte[] bytes, byte[] salt) throws UnknownAlgorithmException
salt
for a single iteration.
hash
in class AbstractHash
bytes
- the bytes to hashsalt
- the salt to use for the initial hash
UnknownAlgorithmException
- if the configured algorithmName
is not available.protected byte[] hash(byte[] bytes, byte[] salt, int hashIterations) throws UnknownAlgorithmException
salt
for the specified number of iterations.
hash
in class AbstractHash
bytes
- the bytes to hashsalt
- the salt to use for the initial hashhashIterations
- the number of times the the bytes
will be hashed (for attack resiliency).
UnknownAlgorithmException
- if the algorithmName
is not available.public boolean isEmpty()
ByteSource
true
if the underlying wrapped byte array is null or empty (zero length), false
otherwise.
true
if the underlying wrapped byte array is null or empty (zero length), false
otherwise.public String toHex()
byte array
.
This implementation caches the resulting hex string so multiple calls to this method remain efficient.
However, calling setBytes
will null the cached value, forcing it to be recalculated the
next time this method is called.
toHex
in interface ByteSource
toHex
in class AbstractHash
byte array
.public String toBase64()
byte array
.
This implementation caches the resulting Base64 string so multiple calls to this method remain efficient.
However, calling setBytes
will null the cached value, forcing it to be recalculated the
next time this method is called.
toBase64
in interface ByteSource
toBase64
in class AbstractHash
byte array
.public String toString()
toHex()
.
toString
in class AbstractHash
toHex()
value.public boolean equals(Object o)
true
if the specified object is a Hash and its byte array
is identical to
this Hash's byte array, false
otherwise.
equals
in class AbstractHash
o
- the object (Hash) to check for equality.
true
if the specified object is a Hash and its byte array
is identical to
this Hash's byte array, false
otherwise.public int hashCode()
hashCode
in class AbstractHash
|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |