cs138.common
Class HashGenerator

java.lang.Object
  extended by cs138.common.HashGenerator
All Implemented Interfaces:
java.io.Serializable

public class HashGenerator
extends java.lang.Object
implements java.io.Serializable

Used to generate hash values (represented as BigIntegers) from Strings. Parameters for hashing include the maximum byte length of the hash value, the radix of the hash value's representation, and the name of algorithm used to generate the hash value. The algorithms available are the same algorithms available to the java.security.MessageDigest. The values generated by the hash method will always be positive and reside between 0 and the maximum unsigned number representable in the byte length of the hash. The space used by the BigInteger which is actually used represent the value of the hash is always larger than the hash byte length.

Author:
allan
See Also:
Serialized Form

Field Summary
static java.lang.String DEFAULT_ALGO
          The default MessageDigest algorithm used.
 
Method Summary
 java.math.BigInteger add(java.math.BigInteger a, java.math.BigInteger b)
          Adds the specified integers and reduces the result modulus the value returned by getModulus().
static java.math.BigInteger divideEvenly(java.math.BigInteger target, java.math.BigInteger divisor)
          Attempts divide the specified target by the specified divisor.
 int getBitLength()
          Returns the number of bits required to store the unsigned representation of the largest hash value generated by this HashFactory.
 int getByteLength()
          Returns the number of bytes required to store the unsigned representation of the largest hash value generated by this HashFactory.
static HashGenerator getHashFactory(int byteLength, int radix)
          Creates and returns a HashFactory object using the default MessageDigest algorithm and the specified byte length and radix.
static HashGenerator getHashFactory(int byteLength, int radix, java.lang.String algorithm)
          Creates and returns a HashFactory object using the specified MessageDigest algorithm, byte length, and radix.
 int getMaximumDigits()
          Returns the number of digits used to represent the largest hash value generated by this HashFactory.
 java.math.BigInteger getMaximumValue()
          Returns the largest hash value generated by this HashFactory.
 java.math.BigInteger getModulus()
          Returns the modulus value for the space of values generated by this HashFactory.
 int getRadix()
          Returns the radix of the hash values generated by this HashFactory.
 java.math.BigInteger hash(java.lang.String string)
          Generates the hash value of the specified string.
 java.util.List<java.math.BigInteger> saltedHashes(java.lang.String string, int number)
          Generates a list of hash values from the specified string by appending the numbers 0 through number - 1 to the string and calling hash() on each produced string.
 java.lang.String toString(java.math.BigInteger value)
          Returns the String representation of the hash value in the radix of this HashFactory.
 
Methods inherited from class java.lang.Object
equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

DEFAULT_ALGO

public static final java.lang.String DEFAULT_ALGO
The default MessageDigest algorithm used.

See Also:
Constant Field Values
Method Detail

getHashFactory

public static HashGenerator getHashFactory(int byteLength,
                                           int radix)
Creates and returns a HashFactory object using the default MessageDigest algorithm and the specified byte length and radix. Not all byte lengths and radixes are compatible. For instance, numbers with a radix of 16 can be represented in any number of bytes, but numbers with a radix of 8 can only be represented in a number of bytes that is a multiple of 3.

Parameters:
byteLength - The maximum byte length of the generated hashes.
radix - The radix of the generated hashes.
Returns:
A HashFactory using the default MessageDigest algorithm.
Throws:
java.lang.IllegalArgumentException - When the specified radix and byte length are incompatible.

getHashFactory

public static HashGenerator getHashFactory(int byteLength,
                                           int radix,
                                           java.lang.String algorithm)
                                    throws java.security.NoSuchAlgorithmException
Creates and returns a HashFactory object using the specified MessageDigest algorithm, byte length, and radix. Not all byte lengths and radixes are compatible. For instance, numbers with a radix of 16 can be represented in any number of bytes, but numbers with a radix of 8 can only be represented in a number of bytes that is a multiple of 3.

Parameters:
byteLength - The maximum byte length of the generated hashes.
radix - The radix of the generated hashes.
algorithm - The name of the MessageDigest algorithm to use.
Returns:
A HashFactory that generates hashes using the given parameters.
Throws:
java.security.NoSuchAlgorithmException - When a MessageDigest algorithm with the specified name does not exist.
java.lang.IllegalArgumentException - When the specified radix and byte length are incompatible.

hash

public java.math.BigInteger hash(java.lang.String string)
Generates the hash value of the specified string. If the hash byte length for the factory is smaller than the hash values generated by the factory's MessageDigest algorithm, the hash is truncated to fit the desired number of bytes.

Parameters:
string - The string to hash.
Returns:
An integer between 0 and getMaximumValue().

saltedHashes

public java.util.List<java.math.BigInteger> saltedHashes(java.lang.String string,
                                                         int number)
Generates a list of hash values from the specified string by appending the numbers 0 through number - 1 to the string and calling hash() on each produced string.

Parameters:
string - The string to generate hashes with.
number - The number of hashes to generate.
Returns:
A list of salted hash values.

divideEvenly

public static java.math.BigInteger divideEvenly(java.math.BigInteger target,
                                                java.math.BigInteger divisor)
                                         throws java.lang.IllegalArgumentException
Attempts divide the specified target by the specified divisor. If the divisor does not divide the target evenly, an IllegalArgumentException will be thrown. The result of dividing the target by the divisor is returned otherwise.

Parameters:
target - The number to be divided.
divisor - The number to divide the target with.
Returns:
The quotient of target and divisor.
Throws:
java.lang.IllegalArgumentException - If divisor does not divide target evenly.

add

public java.math.BigInteger add(java.math.BigInteger a,
                                java.math.BigInteger b)
Adds the specified integers and reduces the result modulus the value returned by getModulus().

Parameters:
a - One integer.
b - The other integer.
Returns:
The result of adding the integers and reducing modulus getModulus().

getMaximumDigits

public int getMaximumDigits()
Returns the number of digits used to represent the largest hash value generated by this HashFactory.

Returns:
The number of digits in the largest hash value.

getRadix

public int getRadix()
Returns the radix of the hash values generated by this HashFactory.

Returns:
The radix of the hash values.

getByteLength

public int getByteLength()
Returns the number of bytes required to store the unsigned representation of the largest hash value generated by this HashFactory.

Returns:
The number of bytes in the unsigned representation of hash values.

getMaximumValue

public java.math.BigInteger getMaximumValue()
Returns the largest hash value generated by this HashFactory.

Returns:
The largest hash value.

getModulus

public java.math.BigInteger getModulus()
Returns the modulus value for the space of values generated by this HashFactory.

Returns:
The modulus of the space of hash values.

toString

public java.lang.String toString(java.math.BigInteger value)
Returns the String representation of the hash value in the radix of this HashFactory. It uses the BigInteger.toString(int) method.

Parameters:
value - A hash value.
Returns:
A string representing the specified hash value in the HashFactory's radix.

getBitLength

public int getBitLength()
Returns the number of bits required to store the unsigned representation of the largest hash value generated by this HashFactory.

Returns:
The number of bits in the unsigned representation of hash values.