org.sf.rhash
public final class RHash extends Object
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 and Description |
---|
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. |
Modifier and Type | Method and Description |
---|---|
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.
|
String |
getMagnet(String filename)
Returns magnet link for given filename.
|
String |
getMagnet(String filename,
HashType... types)
Returns magnet link that includes specified filename
and hashes for given algorithms.
|
static String |
getMagnetFor(String filename,
HashType... types)
Produces magnet link for specified file with given hashes.
|
static String |
getMagnetFor(String filename,
Set<HashType> types)
Produces magnet link for specified file with given hashes.
|
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. |
public RHash(HashType... types)
RHash
to compute
message digests for given types.types
- types of hashing algorithmsNullPointerException
- 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 typesNullPointerException
- if argument is null
IllegalArgumentException
- if argument is empty setpublic 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 processNullPointerException
- 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 processNullPointerException
- 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 useNullPointerException
- 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 processNullPointerException
- if any of arguments is null
public static Digest computeHash(HashType type, File file) throws IOException
type
- type of hash algorithmfile
- the file to processNullPointerException
- if any of arguments is null
IOException
- if an I/O error occurs while hashingpublic static String getMagnetFor(String filename, HashType... types) throws IOException
filename
- the file to generate magnet fortypes
- types of hashing algorithmsIOException
public static String getMagnetFor(String filename, Set<HashType> types) throws IOException
filename
- the file to generate magnet fortypes
- set of hashing typesIOException
public 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 hashNullPointerException
- 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 hashedNullPointerException
- 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 hashedNullPointerException
- 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 hashedIOException
- if an I/O error occursNullPointerException
- 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 typeDigest
for processed dataNullPointerException
- if type
is null
IllegalStateException
- if this RHash
is not finishedIllegalArgumentException
- 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 dataIllegalStateException
- if this RHash
is not finishedpublic String getMagnet(String filename, HashType... types)
RHash
are included.filename
- file name to include in magnet, may be null
IllegalStateException
- if this RHash
is not finishedpublic String getMagnet(String filename)
RHash
.filename
- file name to include in magnet, may be null
IllegalStateException
- if this RHash
is not finished