SimpleHash
implementation directly.@Deprecated public abstract class AbstractHash extends CodecSupport implements Hash, Serializable
ByteSource.Util
PREFERRED_ENCODING
Constructor and Description |
---|
AbstractHash()
Deprecated.
Creates an new instance without any of its properties set (no hashing is performed).
|
AbstractHash(Object source)
Deprecated.
Creates a hash of the specified
source with no salt using a single hash iteration. |
AbstractHash(Object source,
Object salt)
Deprecated.
Creates a hash of the specified
source using the given salt using a single hash iteration. |
AbstractHash(Object source,
Object salt,
int hashIterations)
Deprecated.
Creates a hash of the specified
source using the given salt a total of
hashIterations times. |
Modifier and Type | Method and Description |
---|---|
boolean |
equals(Object o)
Deprecated.
Returns
true if the specified object is a Hash and its byte array is identical to
this Hash's byte array, false otherwise. |
abstract String |
getAlgorithmName()
Deprecated.
Implemented by subclasses, this specifies the
MessageDigest algorithm name
to use when performing the hash. |
byte[] |
getBytes()
Deprecated.
Returns the wrapped byte array.
|
protected MessageDigest |
getDigest(String algorithmName)
Deprecated.
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 given
salt for a single iteration. |
protected byte[] |
hash(byte[] bytes,
byte[] salt,
int hashIterations)
Deprecated.
Hashes the specified byte array using the given
salt for the specified number of iterations. |
int |
hashCode()
Deprecated.
Simply returns toHex().hashCode();
|
void |
setBytes(byte[] alreadyHashedBytes)
Deprecated.
Sets the raw bytes stored by this hash instance.
|
String |
toBase64()
Deprecated.
Returns a Base64-encoded string of the underlying
byte array . |
String |
toHex()
Deprecated.
Returns a hex-encoded string of the underlying
byte array . |
String |
toString()
Deprecated.
Simple implementation that merely returns
toHex() . |
isByteSource, objectToBytes, objectToString, toBytes, toBytes, toBytes, toBytes, toBytes, toBytes, toBytes, toChars, toChars, toString, toString, toString
clone, finalize, getClass, notify, notifyAll, wait, wait, wait
getIterations, getSalt
isEmpty
public AbstractHash()
source
constructor 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 call setBytes
to have a
fully-initialized instance.public AbstractHash(Object source) throws CodecException
source
with no salt
using a single hash iteration.
It is a convenience constructor that merely executes this( source, null, 1);
.
Please see the
AbstractHash(Object,Object,int)
constructor for the types of Objects that may be passed into this constructor, as well as how to support further
types.source
- the object to be hashed.CodecException
- if the specified source
cannot be converted into a byte array (byte[]).public AbstractHash(Object source, Object salt) throws CodecException
source
using the given salt
using a single hash iteration.
It is a convenience constructor that merely executes this( source, salt, 1);
.
Please see the
AbstractHash(Object,Object,int)
constructor for the types of Objects that may be passed into this constructor, as well as how to support further
types.source
- the source object to be hashed.salt
- the salt to use for the hashCodecException
- if either constructor argument cannot be converted into a byte array.public AbstractHash(Object source, Object salt, int hashIterations) throws CodecException
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
, or
InputStream
. 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 three supported types first before passing them in to this
constructor}.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.public abstract String getAlgorithmName()
MessageDigest
algorithm name
to use when performing the hash.getAlgorithmName
in interface Hash
MessageDigest
algorithm name to use when performing the hash.public byte[] getBytes()
ByteSource
getBytes
in interface ByteSource
public void setBytes(byte[] alreadyHashedBytes)
alreadyHashedBytes
- the raw already-hashed bytes to store in this instance.protected MessageDigest getDigest(String algorithmName) throws UnknownAlgorithmException
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)
bytes
- the bytes to hash.protected byte[] hash(byte[] bytes, byte[] salt)
salt
for a single iteration.bytes
- the bytes to hashsalt
- the salt to use for the initial hashprotected byte[] hash(byte[] bytes, byte[] salt, int hashIterations) throws UnknownAlgorithmException
salt
for the specified number of iterations.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 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
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
byte array
.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 Object
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.Copyright © 2004–2017 The Apache Software Foundation. All rights reserved.