public class DataHasher extends Object
Data hasher is stateful, all new data added is added to the same hash calculation, if this class needs to be reused for separate entities, for example hashing multiple files, the reset() method must be called to reset hash calculation.
// create hasher for default hash algorithm
DataHasher hasher = new DataHasher();
// add file to hasher for hashing
hasher.addFile(new File("test.txt"));
// get the hash code
DataHash hash = hasher.getHash();
DataHasher addData() functions always return the same hasher so it is possible to chain call to the hasher, if needed.
For example
DataHasher hasher = new DataHasher();
DataHash hash = hasher.addData("Header").addData(bodyBytes).addData(signatureBytes).getHash();
Constructor and Description |
---|
DataHasher()
Creates new data hasher for the default algorithm (SHA-256).
|
DataHasher(HashAlgorithm algorithm)
Creates new data hasher for specified algorithm.
|
DataHasher(HashAlgorithm algorithm,
boolean checkExpiration)
Creates new data hasher for specified algorithm.
|
Modifier and Type | Method and Description |
---|---|
DataHasher |
addData(byte[] data)
Adds data to the digest using the specified array of bytes, starting at an offset of 0.
|
DataHasher |
addData(byte[] data,
int offset,
int length)
Updates the digest using the specified array of bytes, starting at the specified offset.
|
DataHasher |
addData(DataHash dataHash)
Adds
DataHash.getImprint() to the digest. |
DataHasher |
addData(File file)
Adds data to the digest using the specified file, starting at the offset 0.
|
DataHasher |
addData(File file,
int bufferSize)
Adds data to the digest using the specified file, starting at the offset 0.
|
DataHasher |
addData(InputStream inStream)
Adds data to the digest using the specified input stream of bytes, starting at an offset of 0.
|
DataHasher |
addData(InputStream inStream,
int bufferSize)
Adds data to the digest using the specified input stream of bytes, starting at an offset of 0.
|
DataHash |
getHash()
Gets the final hash value for the digest.
|
DataHasher |
reset()
Resets the hash calculation.
|
public DataHasher(HashAlgorithm algorithm)
algorithm
- HashAlgorithm
describing the algorithm to be used in hashing.IllegalArgumentException
- when hash algorithm is unknown or the algorithm isn't implemented.NullPointerException
- when input algorithm is null.public DataHasher(HashAlgorithm algorithm, boolean checkExpiration)
algorithm
- HashAlgorithm
describing the algorithm to be used in hashing.checkExpiration
- boolean flag to check, if algorithm is deprecated or obsolete.IllegalArgumentException
- when hash algorithm is unknown, isn't implemented, is deprecated or is obsolete.NullPointerException
- when input algorithm is null.public DataHasher()
IllegalArgumentException
- when hash algorithm is unknown or the algorithm isn't implemented.public final DataHasher addData(byte[] data, int offset, int length)
data
- the array of bytes.offset
- the offset to start from in the array of bytes.length
- the number of bytes to use, starting at the offset.DataHasher
object for chaining calls.IllegalStateException
- when hash is already been calculated.public final DataHasher addData(byte[] data)
data
- the array of bytes.DataHasher
object for chaining calls.NullPointerException
- when input data is null.public final DataHasher addData(InputStream inStream)
inStream
- input stream of bytes.DataHasher
object for chaining calls.HashException
- when hash calculation fails.public final DataHasher addData(File file)
file
- input file.DataHasher
object for chaining calls.HashException
- when hash calculation fails.public final DataHasher addData(DataHash dataHash)
DataHash.getImprint()
to the digest.dataHash
- input digest.DataHasher
object for chaining calls.NullPointerException
- when input value is null.public final DataHasher addData(InputStream inStream, int bufferSize)
inStream
- input stream of bytes.bufferSize
- maximum allowed buffer size for reading data.DataHasher
object for chaining calls.HashException
- when hash calculation fails.NullPointerException
- when input stream is null.public final DataHasher addData(File file, int bufferSize)
file
- input file.bufferSize
- size of buffer for reading data.DataHasher
object for chaining calls.HashException
- when hash calculation fails.NullPointerException
- when input file is null.public final DataHash getHash()
This will not reset hash calculation.
HashException
- when exception occurs during hash calculation.public final DataHasher reset()
DataHasher
object for chaining calls.Copyright © 2024 Guardtime. All rights reserved.