ostore.util
Class SHA1Hash

java.lang.Object
  |
  +--ostore.util.SHA1Hash
All Implemented Interfaces:
Cloneable, Comparable, Digestible, QuickSerializable, SecureHash

public class SHA1Hash
extends Object
implements SecureHash

SHA1Hash is the hash class used by all entities which are SHA1 hashes. (In the prototype, this means Guids and VHashes).

Version:
$Id: SHA1Hash.java,v 1.23 2002/04/06 22:52:44 eaton Exp $
Author:
Chris WElls

Field Summary
protected  byte[] _bytes
          These are the bytes in this Hash.
static SHA1Hash NULL_HASH
          This is the null hash for SHA1
static byte[] NULL_HASH_VALUE
          This is the value of the null hash for SHA1
static int NUM_BYTES
          The number of bytes all SHA1Hashs are composed of
static int TYPE_CODE
           
 
Constructor Summary
SHA1Hash()
          default constructor (creates null hash)
SHA1Hash(byte[] bytes)
          hashes the given buffer to create a new SHA1Hash
SHA1Hash(byte[] data, int[] offset)
          Reads the bytes of the value of this hash in from the input byte array.
SHA1Hash(byte[] bytes, int offset, int len)
          hashes the given buffer to create a new SHA1Hash
SHA1Hash(Digestible d)
          hashes over the input object
SHA1Hash(SHA1Hash shash)
          copy constructor (creates new hash with value of old hash)
SHA1Hash(SHA1HashBuffer buffer)
          Computes and returns the digest of the specified buffer.
SHA1Hash(String s)
          hashes over the bytes of the input String
 
Method Summary
 void add_to_digest(SecureHashBuffer buffer)
          Adds this object's state to the specified SecureHashBuffer using the various add methods.
 byte[] bytes()
          Returns the bytes contained in this hash
 void calculate(byte[] bytes, SecureVerify verification)
          This routine calculates the root hash for a given object and its slice of its verification tree (basically, this routine does what verify does, but instead of comparing the last hash to this hash and returning the result, it sets this hash's value to the last hash's value)
 void calculate(QuickSerializable qs, SecureVerify verify)
          This routine calculates the root hash for a given object and its slice of its verification tree (basically, this routine does what verify does, but instead of comparing the last hash to this hash and returning the result, it sets this hash's value to the last hash's value)
 Object clone()
          Create an exact copy of this hash
 int compareTo(Object o)
          This routine compares this SHA1Hash to another Object
static void concatenateHashes(SHA1Hash hash1, SHA1Hash hash2, byte[] buf)
          This routine concatenates two hashes and stores the result in the input byte array.
 boolean equals(Object other)
          Compares this SecureHash with another hash, and returns true iff they are the same type of hash and have the same byte representation
 String fullString()
          Converts all bytes of this SecureHash to a human-readable String
 void hash(byte[] bytes)
          Hashes over the input bytes
 SecureVerify[] hash(byte[][] array)
          Creates a hierarchical hash over an array of byte arrays, and returns an array of arrays, the i^th element of which contains the hashes along the tree of hashes to the root hash for the i^th byte array in the input array; This hash is set to the value of that root.
 void hash(byte[] bytes, int offset, int len)
          Hashes over the input bytes, starting at offset, going for len
 void hash(Digestible o)
          Hashes over a Digestible object
 SecureVerify[] hash(QuickSerializable[] array)
          Creates a hierarchical hash over an array of QuickSerializable objects, and returns an array of arrays, the i^th element of which contains the hashes along the tree of hashes to the root hash for the i^th object in the input array; This hash is set to the value of that root.
 void hash(String str)
          Hashes over the bytes of the input String
 int hashCode()
          Calculates a Java hash code for this SecureHash object (by taking its lower order bytes)
 boolean isNull()
          Determines if this SecureHash is a NULL hash or not.
 long lower64bits()
          Calculates a Java hash code for this SecureHash object (by taking its lower order bytes)
static void main(String[] args)
          Used to test hashing by creating hierarchical hashes
 SecureHash nullHash()
          Returns the implementation's NULL hash (typically all zeroes).
 int size()
          Return the number of bytes used in the representation of this hash (for example, SHA1 uses 20 bytes)
 void to_bytes(byte[] data, int[] offset)
          Write the bytes of this hash to the input byte array
 String toString()
          Converts a few bytes of this SecureHash to a human-readable String
 int type_code()
          Return the type code of a SHA1Hash
 boolean verify(byte[] bytes)
          Verifies that the input bytes hash to this hash value
 boolean verify(byte[] bytes, int offset, int len)
          Verifies that the input bytes hash to this hash value
 boolean verify(byte[] bytes, SecureVerify verification)
          Verifies that the input byte array, coupled with the input verification information, hashes to this hash's value
 boolean verify(Digestible o)
          Verifies a QuickSerializable object
 boolean verify(QuickSerializable o, SecureVerify verification)
          Verifies that the input object, coupled with the input verification information, hashes to this hash's value
 boolean verify(String str)
          Verifies the input String
 
Methods inherited from class java.lang.Object
finalize, getClass, notify, notifyAll, wait, wait, wait
 

Field Detail

TYPE_CODE

public static final int TYPE_CODE

NUM_BYTES

public static final int NUM_BYTES
The number of bytes all SHA1Hashs are composed of

NULL_HASH_VALUE

public static final byte[] NULL_HASH_VALUE
This is the value of the null hash for SHA1

NULL_HASH

public static final SHA1Hash NULL_HASH
This is the null hash for SHA1

_bytes

protected byte[] _bytes
These are the bytes in this Hash.
Constructor Detail

SHA1Hash

public SHA1Hash()
default constructor (creates null hash)

SHA1Hash

public SHA1Hash(SHA1Hash shash)
copy constructor (creates new hash with value of old hash)
Parameters:
shash - == old hash whose value we are to copy

SHA1Hash

public SHA1Hash(byte[] bytes)
hashes the given buffer to create a new SHA1Hash
Parameters:
the - array of bytes over which to hash

SHA1Hash

public SHA1Hash(byte[] bytes,
                int offset,
                int len)
hashes the given buffer to create a new SHA1Hash
Parameters:
the - array of bytes over which to hash
offset - in bytes at which to start hashing
number - of bytes over which to hash

SHA1Hash

public SHA1Hash(byte[] data,
                int[] offset)
Reads the bytes of the value of this hash in from the input byte array.
Parameters:
array - out of which to read the bytes of this hash
offset - in data at which to begin reading. offset[0] is incremented by NUM_BYTES on completion of this constructor.

SHA1Hash

public SHA1Hash(String s)
hashes over the bytes of the input String
Parameters:
string - over which to hash

SHA1Hash

public SHA1Hash(Digestible d)
hashes over the input object
Parameters:
object - over which to hash

SHA1Hash

public SHA1Hash(SHA1HashBuffer buffer)
Computes and returns the digest of the specified buffer.
Parameters:
buffer - A SecureHashBuffer.
Method Detail

clone

public Object clone()
             throws CloneNotSupportedException
Create an exact copy of this hash
Specified by:
clone in interface SecureHash
Overrides:
clone in class Object
Returns:
new SHA1Hash identical in value to this one
Throws:
CloneNotSupportedException - if clone() is not supported

compareTo

public int compareTo(Object o)
              throws ClassCastException
This routine compares this SHA1Hash to another Object
Specified by:
compareTo in interface Comparable
Parameters:
o - SHA1Hash
Returns:
-1 if this < o, 0 if this == o, or 1 if this > o.
Throws:
ClassCastException - if o is not a SHA1Hash

to_bytes

public void to_bytes(byte[] data,
                     int[] offset)
Write the bytes of this hash to the input byte array
Specified by:
to_bytes in interface QuickSerializable
Parameters:
array - into which to write the bytes of this hash
offset - in data at which to begin writing
Returns:
number of bytes written (always NUM_BYTES)

type_code

public int type_code()
Return the type code of a SHA1Hash
Specified by:
type_code in interface QuickSerializable
Returns:
the type code defined at the top of this class

size

public int size()
Return the number of bytes used in the representation of this hash (for example, SHA1 uses 20 bytes)
Specified by:
size in interface SecureHash
Returns:
number of bytes used in the implementation

isNull

public boolean isNull()
Determines if this SecureHash is a NULL hash or not.
Specified by:
isNull in interface SecureHash
Returns:
true iff this Hash has a NULL value

nullHash

public SecureHash nullHash()
Returns the implementation's NULL hash (typically all zeroes).
Specified by:
nullHash in interface SecureHash
Returns:
the implementation's null hash

bytes

public byte[] bytes()
Returns the bytes contained in this hash
Specified by:
bytes in interface SecureHash
Returns:
the bytes of this hash

equals

public boolean equals(Object other)
Compares this SecureHash with another hash, and returns true iff they are the same type of hash and have the same byte representation
Specified by:
equals in interface SecureHash
Overrides:
equals in class Object
Parameters:
the - object with which to compare this SecureHash
Returns:
true iff this and o contain the same byte values

hashCode

public int hashCode()
Calculates a Java hash code for this SecureHash object (by taking its lower order bytes)
Specified by:
hashCode in interface SecureHash
Overrides:
hashCode in class Object
Returns:
the Java hash code for this SecureHash

lower64bits

public long lower64bits()
Calculates a Java hash code for this SecureHash object (by taking its lower order bytes)
Returns:
the Java hash code for this SecureHash

hash

public void hash(byte[] bytes,
                 int offset,
                 int len)
Hashes over the input bytes, starting at offset, going for len
Specified by:
hash in interface SecureHash
Parameters:
byte - array over which to hash
offset - in bytes[] at which to begin hashing
number - of bytes over which to hash

hash

public void hash(byte[] bytes)
Hashes over the input bytes
Specified by:
hash in interface SecureHash
Parameters:
byte - array over which to hash

hash

public void hash(Digestible o)
Hashes over a Digestible object
Specified by:
hash in interface SecureHash
Parameters:
the - object over which to hash

hash

public void hash(String str)
Hashes over the bytes of the input String
Specified by:
hash in interface SecureHash
Parameters:
the - String over which to hash

concatenateHashes

public static void concatenateHashes(SHA1Hash hash1,
                                     SHA1Hash hash2,
                                     byte[] buf)
This routine concatenates two hashes and stores the result in the input byte array.
Parameters:
hash1 - the first hash to concatenate
hash2 - the second hash to concatenate
buf - the byte array in which to storet the concatenation

hash

public SecureVerify[] hash(byte[][] array)
Creates a hierarchical hash over an array of byte arrays, and returns an array of arrays, the i^th element of which contains the hashes along the tree of hashes to the root hash for the i^th byte array in the input array; This hash is set to the value of that root.
Specified by:
hash in interface SecureHash
Parameters:
array - of byte arrays.
Returns:
array of verification information

hash

public SecureVerify[] hash(QuickSerializable[] array)
Creates a hierarchical hash over an array of QuickSerializable objects, and returns an array of arrays, the i^th element of which contains the hashes along the tree of hashes to the root hash for the i^th object in the input array; This hash is set to the value of that root.
Specified by:
hash in interface SecureHash
Parameters:
array - of QuickSerializable objects.
Returns:
array of arrays of verification information

verify

public boolean verify(byte[] bytes,
                      int offset,
                      int len)
Verifies that the input bytes hash to this hash value
Specified by:
verify in interface SecureHash
Parameters:
byte - array containing the bytes to verify
offset - in bytes[] at which to begin hashing
number - of bytes over which to hash
Returns:
true iff the input bytes hash to a hash value equal to this hash value

verify

public boolean verify(byte[] bytes)
Verifies that the input bytes hash to this hash value
Specified by:
verify in interface SecureHash
Parameters:
byte - array to verify
Returns:
true iff the input bytes hash to a hash value equal to this hash value

verify

public boolean verify(Digestible o)
Verifies a QuickSerializable object
Specified by:
verify in interface SecureHash
Parameters:
the - object over which to hash
Returns:
true iff the input object hashes to a hash value equal to this hash value

verify

public boolean verify(String str)
Verifies the input String
Specified by:
verify in interface SecureHash
Parameters:
the - String to verify
Returns:
true iff the input String hashes to a hash value equal to this hash value

verify

public boolean verify(byte[] bytes,
                      SecureVerify verification)
Verifies that the input byte array, coupled with the input verification information, hashes to this hash's value
Specified by:
verify in interface SecureHash
Parameters:
bytes - to verify
verification - verification info with which to verify the bytes against this SHA1Hash
Returns:
true iff the bytes hash to the value contained in this hash

verify

public boolean verify(QuickSerializable o,
                      SecureVerify verification)
Verifies that the input object, coupled with the input verification information, hashes to this hash's value
Specified by:
verify in interface SecureHash
Parameters:
bytes - to verify
verification - verification info with which to verify the bytes against this SHA1Hash
Returns:
true iff the bytes hash to the value contained in this hash

calculate

public void calculate(byte[] bytes,
                      SecureVerify verification)
This routine calculates the root hash for a given object and its slice of its verification tree (basically, this routine does what verify does, but instead of comparing the last hash to this hash and returning the result, it sets this hash's value to the last hash's value)
Specified by:
calculate in interface SecureHash
Parameters:
bytes - bytes to calc a root hash for
verification - verification info to use in the calculation

calculate

public void calculate(QuickSerializable qs,
                      SecureVerify verify)
This routine calculates the root hash for a given object and its slice of its verification tree (basically, this routine does what verify does, but instead of comparing the last hash to this hash and returning the result, it sets this hash's value to the last hash's value)
Specified by:
calculate in interface SecureHash
Parameters:
qs - object to calc a root hash for
verify - verification information to use in the calculation

toString

public String toString()
Converts a few bytes of this SecureHash to a human-readable String
Specified by:
toString in interface SecureHash
Overrides:
toString in class Object
Returns:
human-readable String representation of this Hash

fullString

public String fullString()
Converts all bytes of this SecureHash to a human-readable String
Specified by:
fullString in interface SecureHash
Returns:
human-readable String representation of this Hash containing every nibble of this Hash's value

add_to_digest

public void add_to_digest(SecureHashBuffer buffer)
Description copied from interface: Digestible
Adds this object's state to the specified SecureHashBuffer using the various add methods.
Specified by:
add_to_digest in interface Digestible
Following copied from interface: ostore.util.Digestible
See Also:
SecureHashBuffer.add( byte ), SecureHashBuffer.add( byte[] ), SecureHashBuffer.add( byte[], int, int ), SecureHashBuffer.add( Digestible )

main

public static void main(String[] args)
Used to test hashing by creating hierarchical hashes
Parameters:
arguments; - should contain one entry which is the number of byte arrays over which to create a hierarchical hash