org.tranche.hash
Class BigHash

java.lang.Object
  extended by org.tranche.hash.BigHash
All Implemented Interfaces:
java.lang.Comparable<BigHash>

public final class BigHash
extends java.lang.Object
implements java.lang.Comparable<BigHash>

A utility class for making and representing all of the hash information required for files on Tranche.

Author:
Jayson Falkner - jfalkner@umich.edu, James "Augie" Hill - augman85@gmail.com, Bryan Smith - bryanesmith@gmail.com

Field Summary
static int HASH_LENGTH
          The exact size of the hash.
static int HASH_STRING_LENGTH_BASE16
          BigHash length in Base16.
static int HASH_STRING_LENGTH_BASE64
          BigHash length in Base64.
static int LENGTH_LENGTH
          Length of bytes represented by BigHash.
static int LENGTH_OFFSET
          Offset of the beggingin of the Length portion of the BigHash.
static int MD5_LENGTH
          MD5 hash length in bytes.
static int MD5_OFFSET
          Offset of the beginning of the MD5 portion of the BigHash.
static int SHA1_LENGTH
          SHA-1 hash length in bytes.
static int SHA1_OFFSET
          Offset of the beginning of the SHA-1 portion of the BigHash.
static int SHA256_LENGTH
          SHA-256 hash length in bytes.
static int SHA256_OFFSET
          Offset of the beginning of the SHA-256 portion of the BigHash.
 
Constructor Summary
BigHash(byte[] bytes)
          Construct a BigHash object from an array of bytes.
BigHash(byte[] bytes, byte[] padding)
          Construct a BigHash object from an array of bytes and padding.
BigHash(byte[] bytes, int length)
          Construct a BigHash object from an array of bytes using a length that starts from the begnning of the array.
BigHash(byte[] bytes, int length, byte[] padding)
          Construct a BigHash object from an array of bytes using a length and and additional padding.
BigHash(java.io.File f)
          Construct a BigHash object from a file.
BigHash(java.io.File f, byte[] padding)
          Construct a BigHash object from a file with some additional padding.
BigHash(java.io.InputStream is, long length)
          Construct a BigHash object from a stream of a specified length.
BigHash(java.io.InputStream is, long length, byte[] padding)
          Construct a BigHash object from a stream of specified length with some additional padding.
 
Method Summary
 void add(BigHash hash)
           
 BigHash clone()
           
 int compareTo(BigHash bh)
          Similar to standard compareTo() methods, this compares to determine the equality of two BigHash objects.
static BigHash createFromBytes(byte[] bytes)
          Create a BigHash object from a set of bytes.
static BigHash createFromBytes(byte[] bytes, int offset)
          Create a BigHash object from a set of bytes with a given offset where to begin calculation of the hash.
static BigHash createHashFromString(java.lang.String hash)
          Create the hash from either a Base16 or Base64 encoded string.
 boolean equals(java.lang.Object obj)
          A customization of the equals() method to check BigHash objects.
 long getLength()
          Another method for getting BigHash length used for hashes prior to 5/1/2008.
 java.nio.ByteBuffer getMD5()
          Return MD5 portion of BigHash.
 BigHash getNext()
          Gets the next BigHash in the BigHash universe (this+1).
 BigHash getPrevious()
          Gets the previous BigHash in the BigHash universe (this - 1).
 java.nio.ByteBuffer getSHA1()
          Return SHA-1 portion of BigHash.
 java.nio.ByteBuffer getSHA256()
          Return SHA-256 portion of BigHash.
 int hashCode()
          A customization of the hashCode() method to check BigHash objects.
 java.lang.String toBase16String()
          Return a Base16 encoded representation of the BigHash as a string.
 java.lang.String toBase64String()
          Return a Base64 encoded representation of the BigHash as a string.
 byte[] toByteArray()
          Return a byte array representation of the BigHash.
 java.lang.String toString()
          Return a Base64 encoded representation of the BigHash as a string.
 java.lang.String toWebSafeString()
           
 
Methods inherited from class java.lang.Object
finalize, getClass, notify, notifyAll, wait, wait, wait
 

Field Detail

MD5_LENGTH

public static final int MD5_LENGTH
MD5 hash length in bytes.

See Also:
Constant Field Values

SHA1_LENGTH

public static final int SHA1_LENGTH
SHA-1 hash length in bytes.

See Also:
Constant Field Values

SHA256_LENGTH

public static final int SHA256_LENGTH
SHA-256 hash length in bytes.

See Also:
Constant Field Values

LENGTH_LENGTH

public static final int LENGTH_LENGTH
Length of bytes represented by BigHash.

See Also:
Constant Field Values

MD5_OFFSET

public static final int MD5_OFFSET
Offset of the beginning of the MD5 portion of the BigHash.

See Also:
Constant Field Values

SHA1_OFFSET

public static final int SHA1_OFFSET
Offset of the beginning of the SHA-1 portion of the BigHash.

See Also:
Constant Field Values

SHA256_OFFSET

public static final int SHA256_OFFSET
Offset of the beginning of the SHA-256 portion of the BigHash.

See Also:
Constant Field Values

LENGTH_OFFSET

public static final int LENGTH_OFFSET
Offset of the beggingin of the Length portion of the BigHash.

See Also:
Constant Field Values

HASH_LENGTH

public static final int HASH_LENGTH

The exact size of the hash. Made up of:

Hence this constant's value is 76 bytes, the sum of its components.

See Also:
Constant Field Values

HASH_STRING_LENGTH_BASE16

public static final int HASH_STRING_LENGTH_BASE16
BigHash length in Base16.

See Also:
Constant Field Values

HASH_STRING_LENGTH_BASE64

public static final int HASH_STRING_LENGTH_BASE64
BigHash length in Base64.

See Also:
Constant Field Values
Constructor Detail

BigHash

public BigHash(byte[] bytes)
Construct a BigHash object from an array of bytes.

Parameters:
bytes -

BigHash

public BigHash(byte[] bytes,
               int length)
Construct a BigHash object from an array of bytes using a length that starts from the begnning of the array.

Parameters:
bytes -
length -

BigHash

public BigHash(byte[] bytes,
               byte[] padding)

Construct a BigHash object from an array of bytes and padding.

Parameters:
bytes -
padding -

BigHash

public BigHash(byte[] bytes,
               int length,
               byte[] padding)
Construct a BigHash object from an array of bytes using a length and and additional padding.

Parameters:
bytes -
length -
padding -

BigHash

public BigHash(java.io.File f)
        throws java.io.FileNotFoundException
Construct a BigHash object from a file.

Parameters:
f -
Throws:
java.io.FileNotFoundException

BigHash

public BigHash(java.io.File f,
               byte[] padding)
        throws java.io.FileNotFoundException
Construct a BigHash object from a file with some additional padding.

Parameters:
f -
padding -
Throws:
java.io.FileNotFoundException

BigHash

public BigHash(java.io.InputStream is,
               long length)
Construct a BigHash object from a stream of a specified length.

Parameters:
is -
length -

BigHash

public BigHash(java.io.InputStream is,
               long length,
               byte[] padding)
Construct a BigHash object from a stream of specified length with some additional padding.

Parameters:
is -
length -
padding -
Method Detail

getMD5

public final java.nio.ByteBuffer getMD5()
Return MD5 portion of BigHash.

Returns:

getSHA1

public final java.nio.ByteBuffer getSHA1()
Return SHA-1 portion of BigHash.

Returns:

getSHA256

public final java.nio.ByteBuffer getSHA256()
Return SHA-256 portion of BigHash.

Returns:

getLength

public final long getLength()
Another method for getting BigHash length used for hashes prior to 5/1/2008.

Returns:

toByteArray

public final byte[] toByteArray()
Return a byte array representation of the BigHash.

Returns:

getNext

public final BigHash getNext()

Gets the next BigHash in the BigHash universe (this+1).

Returns:

getPrevious

public final BigHash getPrevious()

Gets the previous BigHash in the BigHash universe (this - 1).

Returns:

add

public final void add(BigHash hash)
Parameters:
hash -

clone

public BigHash clone()
Overrides:
clone in class java.lang.Object
Returns:

compareTo

public int compareTo(BigHash bh)
Similar to standard compareTo() methods, this compares to determine the equality of two BigHash objects.

Specified by:
compareTo in interface java.lang.Comparable<BigHash>
Parameters:
bh -
Returns:

createFromBytes

public static final BigHash createFromBytes(byte[] bytes)
Create a BigHash object from a set of bytes.

Parameters:
bytes -
Returns:

createFromBytes

public static final BigHash createFromBytes(byte[] bytes,
                                            int offset)
Create a BigHash object from a set of bytes with a given offset where to begin calculation of the hash.

Parameters:
bytes -
offset -
Returns:

toString

public final java.lang.String toString()
Return a Base64 encoded representation of the BigHash as a string.

Overrides:
toString in class java.lang.Object
Returns:

toBase64String

public final java.lang.String toBase64String()
Return a Base64 encoded representation of the BigHash as a string.

Returns:

toBase16String

public final java.lang.String toBase16String()
Return a Base16 encoded representation of the BigHash as a string.

Returns:

toWebSafeString

public final java.lang.String toWebSafeString()
Returns:

equals

public final boolean equals(java.lang.Object obj)
A customization of the equals() method to check BigHash objects. This method gets invoked alot because BigHash objects are the primary identifier of data on Tranche.

Overrides:
equals in class java.lang.Object

hashCode

public int hashCode()
A customization of the hashCode() method to check BigHash objects. This method gets invoked alot because BigHash objects are the primary identifier of data on Tranche.

Overrides:
hashCode in class java.lang.Object

createHashFromString

public static final BigHash createHashFromString(java.lang.String hash)
                                          throws java.lang.RuntimeException
Create the hash from either a Base16 or Base64 encoded string.

Throws:
java.lang.RuntimeException


This code is free for use both commercially and non-commercially as stated in the project's license