org.helidb.lang.hasher
Class AbstractMessageDigestStringHasher<H extends Comparable<H>>

java.lang.Object
  extended by org.helidb.lang.hasher.AbstractMessageDigestStringHasher<H>
All Implemented Interfaces:
Hasher<String,H>
Direct Known Subclasses:
StringToBigIntegerHasher, StringToLongHasher

public abstract class AbstractMessageDigestStringHasher<H extends Comparable<H>>
extends Object
implements Hasher<String,H>

This is an abstract base class for String Hasher:s that use a MessageDigest to produce the hash.

Since:
1.0
Author:
Karl Gustafsson
In_jar:
helidb-core

Field Summary
static String DEFAULT_ALGORITHM
          The default hash algorithm.
 
Constructor Summary
protected AbstractMessageDigestStringHasher(String hashAlgorithm, int hashLength, int noOfBitsInMostSignificantByte)
          Create an instance of the hasher.
protected AbstractMessageDigestStringHasher(String hashAlgorithm, int hashLength, int noOfBitsInMostSignificantByte, String provider, Charset cs)
          Create an instance of the hasher.
 
Method Summary
protected  void assertNoMaskedBitsSet(byte b)
           
protected  byte[] createDigest(String s)
          Create a digest for the supplied string.
 int getHashLength()
          Get the size of the hash in bytes.
 H hash(String s)
          This implementation creates a digest and then uses the Hasher.fromBytes(byte[]) method to create a byte array to return.
 boolean isPreservingValues()
          Does the hashing operation not modify the hashed value at all? This is true if v.equals(hasher.hash(v)) for all values v accepted by the hasher.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 
Methods inherited from interface org.helidb.lang.hasher.Hasher
fromBytes, toBytes
 

Field Detail

DEFAULT_ALGORITHM

public static final String DEFAULT_ALGORITHM
The default hash algorithm.

See Also:
Constant Field Values
Constructor Detail

AbstractMessageDigestStringHasher

protected AbstractMessageDigestStringHasher(String hashAlgorithm,
                                            int hashLength,
                                            int noOfBitsInMostSignificantByte)
                                     throws IllegalArgumentException
Create an instance of the hasher. The instance uses the supplied hash algorithm from a provider found by looking up the hash algorithm in the standard way (commonly the SUN provider on Sun JVM:s). The Charset used for creating the hash is the platform's default charset.

Parameters:
hashAlgorithm - The name of the hash algorithm. This should be the name of an hash algorithm implemented by one of the JVM's registered security providers.
hashLength - The length of hashes created by this object.
noOfBitsInMostSignificantByte - The number of bits to keep in the most significant byte. The bits kept are the least significant bits. If this is set to 7, the most significant bit is discarded. If set to 6, the two most significant bits are discarded, etc.
Throws:
IllegalArgumentException - If the hash length is less than 1 or if the number of bits value is invalid.

AbstractMessageDigestStringHasher

protected AbstractMessageDigestStringHasher(String hashAlgorithm,
                                            int hashLength,
                                            int noOfBitsInMostSignificantByte,
                                            String provider,
                                            Charset cs)
                                     throws IllegalArgumentException
Create an instance of the hasher. The instance uses the supplied hash algorithm from the specified. The supplied Charset is used for creating the hash.

Parameters:
hashAlgorithm - The name of the hash algorithm. This should be the name of an hash algorithm implemented by the specified security provider.
hashLength - The length of hashes created by this object.
noOfBitsInMostSignificantByte - The number of bits to keep in the most significant byte. The bits kept are the least significant bits. If this is set to 7, the most significant bit is discarded. If set to 6, the two most significant bits are discarded, etc.
provider - The security provider that implements the hash algorithm. If this is null, the default provider lookup mechanism is used.
cs - The Charset used for creating the hash. If this is null, the platform's default Charset is used.
Throws:
IllegalArgumentException - If the hash length is less than 1 or if the number of bits value is invalid.
Method Detail

getHashLength

public int getHashLength()
Description copied from interface: Hasher
Get the size of the hash in bytes. This is the size of the byte array created by Hasher.toBytes(Comparable) and accepted by Hasher.fromBytes(byte[]).

Specified by:
getHashLength in interface Hasher<String,H extends Comparable<H>>
Returns:
The length of hashes created by this object.

isPreservingValues

public boolean isPreservingValues()
Description copied from interface: Hasher
Does the hashing operation not modify the hashed value at all? This is true if v.equals(hasher.hash(v)) for all values v accepted by the hasher.

Specified by:
isPreservingValues in interface Hasher<String,H extends Comparable<H>>
Returns:
false, always.

assertNoMaskedBitsSet

protected void assertNoMaskedBitsSet(byte b)
                              throws IllegalArgumentException
Throws:
IllegalArgumentException

createDigest

protected byte[] createDigest(String s)
Create a digest for the supplied string. The returned digest is truncated to the length of this object's hashes.

Parameters:
s - The string to create a digest for-
Returns:
A digest, truncated to the size of this object's hashes.

hash

public H hash(String s)
This implementation creates a digest and then uses the Hasher.fromBytes(byte[]) method to create a byte array to return.

Specified by:
hash in interface Hasher<String,H extends Comparable<H>>
Parameters:
s - The value to hash.
Returns:
The hash value.