ostore.util
Class SHA1Hash

java.lang.Object
  |
  +--ostore.util.SHA1Hash
All Implemented Interfaces:
Cloneable, Comparable, QuickSerializable, SecureHash
Direct Known Subclasses:
SHA1HashKey

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.48 2003/12/22 01:17:10 srhea Exp $
Author:
Chris WElls

Field Summary
protected  byte[] _bytes
          These are the bytes in this Hash.
static SecureHash FIRST_HASH
          This is first hash SHA1
static SecureHash LAST_HASH
          This is last hash SHA1
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
 
Constructor Summary
SHA1Hash()
          default constructor (creates null hash)
SHA1Hash(byte[] bytes)
          hashes the given buffer to create a new SHA1Hash
SHA1Hash(byte[] bytes, boolean fake)
          takes the byte array and just uses it. way to control what your guids look like
SHA1Hash(byte[] bytes, int offset, int len)
          hashes the given buffer to create a new SHA1Hash
SHA1Hash(InputBuffer buffer)
          Construct out of an input buffer.
SHA1Hash(QuickSerializable 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
 byte[] bytes()
          Returns the bytes contained in this hash
 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
 void hash(byte[] bytes, int offset, int len)
          Hashes over the input bytes, starting at offset, going for len
 void hash(QuickSerializable o)
          Hashes over a QuickSerializable object
 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)
          Measures the speed of various hash implementations.
 SecureHash nullHash()
          Returns the implementation's NULL hash (typically all zeroes).
 void serialize(OutputBuffer buffer)
          Add the object to the buffer.
 int size()
          Return the number of bytes used in the representation of this hash (for example, SHA1 uses 20 bytes)
 String toString()
          Converts a few bytes of this SecureHash to a human-readable String
 boolean verify(QuickSerializable o)
          Verifies a QuickSerializable object
 
Methods inherited from class java.lang.Object
finalize, getClass, notify, notifyAll, wait, wait, wait
 

Field Detail

NUM_BYTES

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

See Also:
Constant Field Values

NULL_HASH_VALUE

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


FIRST_HASH

public static final SecureHash FIRST_HASH
This is first hash SHA1


LAST_HASH

public static final SecureHash LAST_HASH
This is last hash 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:
bytes - the array of bytes over which to hash

SHA1Hash

public SHA1Hash(byte[] bytes,
                boolean fake)
takes the byte array and just uses it. way to control what your guids look like


SHA1Hash

public SHA1Hash(byte[] bytes,
                int offset,
                int len)
hashes the given buffer to create a new SHA1Hash

Parameters:
bytes - the array of bytes over which to hash
offset - in bytes at which to start hashing
len - of bytes over which to hash

SHA1Hash

public SHA1Hash(InputBuffer buffer)
Construct out of an input buffer.


SHA1Hash

public SHA1Hash(String s)
hashes over the bytes of the input String

Parameters:
s - string over which to hash

SHA1Hash

public SHA1Hash(QuickSerializable d)
hashes over the input object

Parameters:
d - 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

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:
other - 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:
bytes - array over which to hash
offset - in bytes[] at which to begin hashing
len - of bytes over which to hash

hash

public void hash(byte[] bytes)
Hashes over the input bytes

Specified by:
hash in interface SecureHash
Parameters:
bytes - the byte array over which to hash

hash

public void hash(QuickSerializable o)
Hashes over a QuickSerializable object

Specified by:
hash in interface SecureHash
Parameters:
o - 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:
str - 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

verify

public boolean verify(QuickSerializable o)
Verifies a QuickSerializable object

Specified by:
verify in interface SecureHash
Parameters:
o - the object over which to hash
Returns:
true iff the input object hashes to a hash value equal to this hash value

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

serialize

public void serialize(OutputBuffer buffer)
Description copied from interface: QuickSerializable
Add the object to the buffer.

Specified by:
serialize in interface QuickSerializable
Parameters:
buffer - the output buffer to add the object to

main

public static void main(String[] args)
Measures the speed of various hash implementations.