|
||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Objectorg.sf.rhash.RHash
public final class RHash
Incremental hasher. This class allows you to do incremental hashing for set of hashing algorithms.
To do hashing RHash
instance is first created
and then filled with message chunks using update()
methods. Finally, finish()
should be called to end
all calculations and generate digests, which then can be obtained
with getDigest()
method. Note, that trying to update
finished RHash
has no effect other than throwing
IllegalStateException
though you can reuse this class
by calling reset()
method, returning it to the state
which was immediately after creating.
To quickly produce message digest for a single message/file
and a single algorithm you may use convenience methods
RHash.computeHash()
.
This class is thread safe.
Constructor Summary | |
---|---|
RHash(HashType... types)
Creates new RHash to compute
message digests for given types. |
|
RHash(Set<HashType> types)
Creates new RHash to compute
message digests for given types. |
Method Summary | |
---|---|
static Digest |
computeHash(HashType type,
byte[] data)
Computes hash of given data. |
static Digest |
computeHash(HashType type,
byte[] data,
int ofs,
int len)
Computes hash of given range in data. |
static Digest |
computeHash(HashType type,
File file)
Computes hash of given string. |
static Digest |
computeHash(HashType type,
String str)
Computes hash of given string. |
static Digest |
computeHash(HashType type,
String str,
String encoding)
Computes hash of given string. |
protected void |
finalize()
Called by garbage collector to free native resources. |
void |
finish()
Finishes calculation of hash codes. |
Digest |
getDigest()
Returns digest for processed data. |
Digest |
getDigest(HashType type)
Returns digest for given hash type. |
boolean |
isFinished()
Tests whether this RHash is finished or not. |
void |
reset()
Resets this RHash to initial state. |
RHash |
update(byte[] data)
Updates this RHash with new data chunk. |
RHash |
update(byte[] data,
int ofs,
int len)
Updates this RHash with new data chunk. |
RHash |
update(File file)
Updates this RHash with data from given file. |
RHash |
update(String str)
Updates this RHash with new data chunk. |
Methods inherited from class java.lang.Object |
---|
clone, equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
Constructor Detail |
---|
public RHash(HashType... types)
RHash
to compute
message digests for given types.
types
- types of hashing algorithms
NullPointerException
- if any of arguments is null
IllegalArgumentException
- if zero hash types specifiedpublic RHash(Set<HashType> types)
RHash
to compute
message digests for given types.
types
- set of hashing types
NullPointerException
- if argument is null
IllegalArgumentException
- if argument is empty setMethod Detail |
---|
public static Digest computeHash(HashType type, byte[] data, int ofs, int len)
data
starting from data[ofs]
and ending at data[ofs+len-1]
.
type
- type of hash algorithmdata
- the bytes to processofs
- index of the first byte in array to processlen
- count of bytes to process
NullPointerException
- if either type
or data
is null
IndexOutOfBoundsException
- if ofs < 0
, len < 0
or
ofs+len > data.length
public static Digest computeHash(HashType type, byte[] data)
type
- type of hash algorithmdata
- the bytes to process
NullPointerException
- if either type
or data
is null
public static Digest computeHash(HashType type, String str, String encoding) throws UnsupportedEncodingException
type
- type of hash algorithmstr
- the string to processencoding
- encoding to use
NullPointerException
- if any of arguments is null
UnsupportedEncodingException
- if specified encoding is not supportedpublic static Digest computeHash(HashType type, String str)
type
- type of hash algorithmstr
- the string to process
NullPointerException
- if any of arguments is null
public static Digest computeHash(HashType type, File file) throws IOException
type
- type of hash algorithmfile
- the file to process
NullPointerException
- if any of arguments is null
IOException
- if an I/O error occurs while hashingpublic RHash update(byte[] data, int ofs, int len)
RHash
with new data chunk.
This method hashes bytes from data[ofs]
through data[ofs+len-1]
.
data
- data to be hashedofs
- index of the first byte to hashlen
- number of bytes to hash
NullPointerException
- if data
is null
IndexOutOfBoundsException
- if ofs < 0
, len < 0
or
ofs+len > data.length
IllegalStateException
- if finish()
was called and there were no
subsequent calls of reset()
public RHash update(byte[] data)
RHash
with new data chunk.
This method has the same effect as
update(data, 0, data.length)
data
- data to be hashed
NullPointerException
- if data
is null
IllegalStateException
- if finish()
was called and there were no
subsequent calls of reset()
public RHash update(String str)
RHash
with new data chunk.
String is encoded into a sequence of bytes using the
default platform encoding.
str
- string to be hashed
NullPointerException
- if str
is null
IllegalStateException
- if finish()
was called and there were no
subsequent calls of reset()
public RHash update(File file) throws IOException
RHash
with data from given file.
file
- file to be hashed
IOException
- if an I/O error occurs
NullPointerException
- if file
is null
IllegalStateException
- if finish()
was called and there were no
subsequent calls of reset()
public void finish()
RHash
is already finished.
public void reset()
RHash
to initial state.
The RHash
becomes available to process
new data chunks. Note, that this method returns
RHash
to the state after creating the
object, NOT the state when hashing continues.
Therefore, all previously calculated hashes are lost
and process starts from the very beginning.
public boolean isFinished()
RHash
is finished or not.
false
if this RHash
is ready to
receive new data for hashing;
true
if hash calculations are finishedpublic Digest getDigest(HashType type)
type
- hash type
Digest
for processed data
NullPointerException
- if type
is null
IllegalStateException
- if this RHash
is not finished
IllegalArgumentException
- if this RHash
was not created to calculate
hash for specified algorithmpublic Digest getDigest()
RHash
constructor, then the least
hash type (in the order induced by
compareTo()
) is used.
Digest
for processed data
IllegalStateException
- if this RHash
is not finishedprotected void finalize()
finalize
in class Object
|
||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |