org.helidb.lang.hasher
Class StringToLongHasher

java.lang.Object
  extended by org.helidb.lang.hasher.AbstractMessageDigestStringHasher<Long>
      extended by org.helidb.lang.hasher.StringToLongHasher
All Implemented Interfaces:
Hasher<String,Long>

public class StringToLongHasher
extends AbstractMessageDigestStringHasher<Long>

This Hasher uses a MessageDigest object with a configurable hash algorithm to produce an eight bytes long hash from a String. It does not support null values.

Any MessageDigest function may be used for creating the hash, as long as the hash it produces is at least eight bytes long. The eight bytes are extracted from the hash produced by the MessageDigest by taking its first eight bytes.

Instead of instantiating this class with the no-arguments constructor, the singleton instance INSTANCE may be used.

Since:
1.0
Author:
Karl Gustafsson
See Also:
StringToBigIntegerHasher
In_jar:
helidb-core

Field Summary
static StringToLongHasher INSTANCE
          The singleton instance.
 
Fields inherited from class org.helidb.lang.hasher.AbstractMessageDigestStringHasher
DEFAULT_ALGORITHM
 
Constructor Summary
StringToLongHasher()
          Create an instance of the hasher.
StringToLongHasher(int noOfBitsInMostSignificantByte)
          Create an instance of the hasher that uses a bit mask for the most significant byte when creating a hash.
StringToLongHasher(String hashAlgorithm, int noOfBitsInMostSignificantByte, String provider, Charset cs)
          Create a hasher.
 
Method Summary
 Long fromBytes(byte[] barr)
          Interpret the serialized hash value.
 byte[] toBytes(Long l)
          Serialize the hash to a byte array.
 
Methods inherited from class org.helidb.lang.hasher.AbstractMessageDigestStringHasher
assertNoMaskedBitsSet, createDigest, getHashLength, hash, isPreservingValues
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

INSTANCE

public static final StringToLongHasher INSTANCE
The singleton instance. This instance has the configuration that creating an object using the no-arguments constructor would give. See StringToLongHasher().

Constructor Detail

StringToLongHasher

public StringToLongHasher()
Create an instance of the hasher. The instance uses the "SHA-256" hash algorithm from the default provider (SUN on Sun JVM:s), and the Charset is the platform's default charset.

Instead of creating an object using this constructor, the singleton instance INSTANCE may be used.


StringToLongHasher

public StringToLongHasher(int noOfBitsInMostSignificantByte)
                   throws IllegalArgumentException
Create an instance of the hasher that uses a bit mask for the most significant byte when creating a hash. The instance uses the "SHA-256" hash algorithm from the default provider (SUN on Sun JVM:s), and the Charset is the platform's default charset.

Parameters:
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 number of bits to keep is not between 0 and 8 (inclusive).

StringToLongHasher

public StringToLongHasher(String hashAlgorithm,
                          int noOfBitsInMostSignificantByte,
                          String provider,
                          Charset cs)
                   throws IllegalArgumentException
Create a hasher.

Parameters:
hashAlgorithm - The name of the hash algorithm.
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 name of the security provider. Set to null to use the JVM's default provider.
cs - The charset that the String:s to hash are encoded in. Set to null to use the JVM's default charset.
Throws:
IllegalArgumentException - If the number of bits to keep is not between 0 and 8 (inclusive).
Method Detail

toBytes

public byte[] toBytes(Long l)
Description copied from interface: Hasher
Serialize the hash to a byte array.

For a given Hasher object, the byte arrays returned by this method must always be of the same size.

Parameters:
l - The hash to serialize.
Returns:
The serialized hash.

fromBytes

public Long fromBytes(byte[] barr)
Description copied from interface: Hasher
Interpret the serialized hash value.

Parameters:
barr - The serialized hash value.
Returns:
The hash value.