org.sf.rhash
Class RHash

java.lang.Object
  extended by org.sf.rhash.RHash

public final class RHash
extends Object

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.
 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.
 
Methods inherited from class java.lang.Object
clone, equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

RHash

public RHash(HashType... types)
Creates new RHash to compute message digests for given types.

Parameters:
types - types of hashing algorithms
Throws:
NullPointerException - if any of arguments is null
IllegalArgumentException - if zero hash types specified

RHash

public RHash(Set<HashType> types)
Creates new RHash to compute message digests for given types.

Parameters:
types - set of hashing types
Throws:
NullPointerException - if argument is null
IllegalArgumentException - if argument is empty set
Method Detail

computeHash

public static Digest computeHash(HashType type,
                                 byte[] data,
                                 int ofs,
                                 int len)
Computes hash of given range in data. This method calculates message digest for byte subsequence in array data starting from data[ofs] and ending at data[ofs+len-1].

Parameters:
type - type of hash algorithm
data - the bytes to process
ofs - index of the first byte in array to process
len - count of bytes to process
Returns:
message digest for specified subarray
Throws:
NullPointerException - if either type or data is null
IndexOutOfBoundsException - if ofs < 0, len < 0 or ofs+len > data.length

computeHash

public static Digest computeHash(HashType type,
                                 byte[] data)
Computes hash of given data.

Parameters:
type - type of hash algorithm
data - the bytes to process
Returns:
message digest for specified array
Throws:
NullPointerException - if either type or data is null

computeHash

public static Digest computeHash(HashType type,
                                 String str,
                                 String encoding)
                          throws UnsupportedEncodingException
Computes hash of given string. String is encoded into a sequence of bytes using the specified charset.

Parameters:
type - type of hash algorithm
str - the string to process
encoding - encoding to use
Returns:
message digest for specified string
Throws:
NullPointerException - if any of arguments is null
UnsupportedEncodingException - if specified encoding is not supported

computeHash

public static Digest computeHash(HashType type,
                                 String str)
Computes hash of given string. String is encoded into a sequence of bytes using the default platform encoding.

Parameters:
type - type of hash algorithm
str - the string to process
Returns:
message digest for specified string
Throws:
NullPointerException - if any of arguments is null

computeHash

public static Digest computeHash(HashType type,
                                 File file)
                          throws IOException
Computes hash of given string.

Parameters:
type - type of hash algorithm
file - the file to process
Returns:
data hash
Throws:
NullPointerException - if any of arguments is null
IOException - if an I/O error occurs while hashing

getMagnetFor

public static String getMagnetFor(String filename,
                                  HashType... types)
                           throws IOException
Produces magnet link for specified file with given hashes.

Parameters:
filename - the file to generate magnet for
types - types of hashing algorithms
Throws:
IOException

getMagnetFor

public static String getMagnetFor(String filename,
                                  Set<HashType> types)
                           throws IOException
Produces magnet link for specified file with given hashes.

Parameters:
filename - the file to generate magnet for
types - set of hashing types
Throws:
IOException

update

public RHash update(byte[] data,
                    int ofs,
                    int len)
Updates this RHash with new data chunk. This method hashes bytes from data[ofs] through data[ofs+len-1].

Parameters:
data - data to be hashed
ofs - index of the first byte to hash
len - number of bytes to hash
Returns:
this object
Throws:
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()

update

public RHash update(byte[] data)
Updates this RHash with new data chunk. This method has the same effect as
update(data, 0, data.length)

Parameters:
data - data to be hashed
Returns:
this object
Throws:
NullPointerException - if data is null
IllegalStateException - if finish() was called and there were no subsequent calls of reset()

update

public RHash update(String str)
Updates this RHash with new data chunk. String is encoded into a sequence of bytes using the default platform encoding.

Parameters:
str - string to be hashed
Returns:
this object
Throws:
NullPointerException - if str is null
IllegalStateException - if finish() was called and there were no subsequent calls of reset()

update

public RHash update(File file)
             throws IOException
Updates this RHash with data from given file.

Parameters:
file - file to be hashed
Returns:
this object
Throws:
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()

finish

public void finish()
Finishes calculation of hash codes. Does nothing if RHash is already finished.


reset

public void reset()
Resets this 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.


isFinished

public boolean isFinished()
Tests whether this RHash is finished or not.

Returns:
false if this RHash is ready to receive new data for hashing; true if hash calculations are finished

getDigest

public Digest getDigest(HashType type)
Returns digest for given hash type.

Parameters:
type - hash type
Returns:
Digest for processed data
Throws:
NullPointerException - if type is null
IllegalStateException - if this RHash is not finished
IllegalArgumentException - if this RHash was not created to calculate hash for specified algorithm

getDigest

public Digest getDigest()
Returns digest for processed data. If more than one hashing type was passed to the RHash constructor, then the least hash type (in the order induced by compareTo()) is used.

Returns:
Digest for processed data
Throws:
IllegalStateException - if this RHash is not finished

getMagnet

public String getMagnet(String filename,
                        HashType... types)
Returns magnet link that includes specified filename and hashes for given algorithms. Only hashes that were computed by this RHash are included.

Parameters:
filename - file name to include in magnet, may be null
Returns:
magnet link
Throws:
IllegalStateException - if this RHash is not finished

getMagnet

public String getMagnet(String filename)
Returns magnet link for given filename. Magnet includes all hashes that were computed by this RHash.

Parameters:
filename - file name to include in magnet, may be null
Returns:
magnet link
Throws:
IllegalStateException - if this RHash is not finished

finalize

protected void finalize()
Called by garbage collector to free native resources.

Overrides:
finalize in class Object