public class ExtendedHasher
extends java.lang.Object
Internally the following is done until the required number of bits are achieved:
output = Hn( input ) || Hn( input || 0x01 ) || Hn( input || 0x02 ) ... where Hn( x ) is n iterations of hash function H applied to data x
The requested output is created by truncating the above result. Any additional bytes that are created but not needed are saved and returned on the next request for output.
If several calls are made to request output without resetting the input, the internal counter (that is appended to the input before hashing) will not be reset. This ensures that unique results are obtained even if the extended hasher is not reset.
| Constructor and Description |
|---|
ExtendedHasher()
The default constructor; creates and initializes an
ExtendedHasher object that uses the 'SHA1' hashing
algorithm. |
ExtendedHasher(java.security.MessageDigest messageDigest)
A constructor; creates and initializes an
ExtendedHasher
object that uses the provided MessageDigest to do the
hashing. |
ExtendedHasher(java.lang.String algorithm)
A constructor; creates and initializes an
ExtendedHasher
object that uses the specified hashing algorithm to do the hashing. |
| Modifier and Type | Method and Description |
|---|---|
static byte[] |
concatenate(byte[] ba1,
byte[] ba2)
Deprecated.
since 6.1; use
ByteArray.append(byte[])
instead |
byte[] |
getOutput(int numBytes,
int iterations)
Hashes the input byte array the specified number of times and returns the
requested number of bytes.
|
void |
setInput(byte[] input)
Sets the input byte array to be used by the extended hasher, and resets
all internal data in preparation for a new hashing session.
|
static byte[] |
tail(byte[] ba,
int offset)
Deprecated.
since 6.1; use
ByteArray.tail(int) instead |
static byte[] |
truncate(byte[] ba,
int length)
Deprecated.
since 6.1; use
ByteArray.truncate(int) instead |
void |
wipe()
Wipes all internal data.
|
public ExtendedHasher()
ExtendedHasher object that uses the 'SHA1' hashing
algorithm.
public ExtendedHasher(java.lang.String algorithm)
throws java.security.NoSuchAlgorithmException
ExtendedHasher
object that uses the specified hashing algorithm to do the hashing.
algorithm - [FIPS 140-2 control input] a hashing algorithmjava.security.NoSuchAlgorithmException - [FIPS 140-2 status output] thrown if the hashing algorithm
is not recognizedpublic ExtendedHasher(java.security.MessageDigest messageDigest)
throws java.lang.IllegalArgumentException
ExtendedHasher
object that uses the provided MessageDigest to do the
hashing.
messageDigest - [FIPS 140-2 control input] a MessageDigest that
will be used for all hashingjava.lang.IllegalArgumentException - [FIPS 140-2 status output] thrown if the parameter is
nullpublic void setInput(byte[] input)
throws java.lang.IllegalArgumentException
input - [FIPS 140-2 data input] the input byte arrayjava.lang.IllegalArgumentException - [FIPS 140-2 status output] thrown if the input byte array is
nullpublic byte[] getOutput(int numBytes,
int iterations)
throws java.lang.IllegalArgumentException,
java.lang.NullPointerException
Internally this does the following:
output = Hn( input ) || Hn( input || 0x01 ) || Hn( input || 0x02 ) ... where Hn( x ) is n iterations of hash function H applied to data x
It hashes the input the specified number of times and then checks if the result contains at least the amount of requested data. If not it does the process again, this time appending byte 0x01 to the input, and then appending this result to the previous result. This process continues with the byte that is appended increasing by one each round until the desired number of bytes are achieved.
numBytes - [FIPS 140-2 control input] the number of bytes requestediterations - [FIPS 140-2 control input] the number of times to hash the
inputjava.lang.IllegalArgumentException - [FIPS 140-2 status output] thrown if the requested number
of bytes is less than zero, or if the requested number of
iterations is less than one (at least one hashing round
must be done)java.lang.NullPointerException - [FIPS 140-2 status output] thrown if the input data has
not been set yetFips140ErrorStateException - [FIPS 140-2 status output] thrown if the Toolkit is not
allowed to perform cryptographic operationspublic final void wipe()
public static final byte[] concatenate(byte[] ba1,
byte[] ba2)
ByteArray.append(byte[])
insteadThe resulting array will contain a copy of the contents of the first byte array followed by a copy of the contents of the second.
ba1 - the first byte arrayba2 - the second byte arraypublic static final byte[] truncate(byte[] ba,
int length)
throws java.lang.IllegalArgumentException
ByteArray.truncate(int) insteadAll bytes after the indicated length are discarded.
ba - the byte array to truncatelength - the new length for the truncated array in bytesjava.lang.IllegalArgumentException - thrown if length is less than zero, or greater than the
length of the byte arraypublic static final byte[] tail(byte[] ba,
int offset)
throws java.lang.IllegalArgumentException
ByteArray.tail(int) insteadAll bytes before the indicated offset are discarded.
ba - the byte array take the tail ofoffset - the offset at which to begin the new byte arrayjava.lang.IllegalArgumentException - thrown if the offset is less than zero, or greater than
the length of the byte array