org.pfsw.security.crypto
Class CheckedHashAlgorithm

java.lang.Object
  extended by org.pfsw.security.crypto.CheckedHashAlgorithm
All Implemented Interfaces:
java.io.Serializable

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

This is a helper class that represents a hash algorithm that has already been checked if it is supported by the platform.

It helps to avoid the annoying try-catch code-pollution every time you use a valid algorithm, but the API requires the handling of NoSuchAlgorithmException even if it cannot occur at all.

See Also:
Serialized Form

Field Summary
static CheckedHashAlgorithm MD5
          The pre-defined constant for the MD hash algorithm.
static CheckedHashAlgorithm SHA_1
          The pre-defined constant for the SHA-1 hash algorithm.
static CheckedHashAlgorithm SHA_2_256
          The pre-defined constant for the SHA-256 hash algorithm.
static CheckedHashAlgorithm SHA_2_384
          The pre-defined constant for the SHA-384 hash algorithm.
static CheckedHashAlgorithm SHA_2_512
          The pre-defined constant for the SHA-512 hash algorithm.
static int UNKNOWN_LENGTH
           
 
Constructor Summary
protected CheckedHashAlgorithm(java.lang.String algorithmName)
          Creates successfully a checked algorithm object or throws an exception.
 
Method Summary
static void checkAlgorithm(java.lang.String algorithmName)
          Checks whether or not the given algorithm is supported.
 byte[] computeHash(byte[] content)
          Computes a hash value for the given input data using the this hashing algorithm.
 byte[] computeHash(java.lang.String text, org.pfsw.text.CheckedCharsetName encoding)
          Computes a hash value for the given text using the this hashing algorithm.
 byte[] computeUTF8Hash(java.lang.String text)
          Computes a hash value for the given text using the this hashing algorithm and UTF-8 encoding for the text conversion to bytes.
static CheckedHashAlgorithm create(java.lang.String algorithmName)
          Creates a new instance based on the given algorithm name.
protected  int detectBitLength()
          Tries to derive the bit length from the algorithm's name.
static int detectBitLength(java.lang.String algorithmName)
          Tries to extract the bit length part of the given algorithm name and return it as an integer.
 boolean equals(java.lang.Object obj)
           
static CheckedHashAlgorithm find(java.lang.String algorithmName)
          Looks for a predefined CheckedHashAlgorithm constant with the given algorithm name and returns it.
static CheckedHashAlgorithm findOrCreate(java.lang.String algorithmName)
          Looks for a predefined CheckedHashAlgorithm constant with the given algorithm name and returns it.
 int getBitLength()
          Returns the bit length of this algorithm.
 int getByteLength()
          Returns the byte length of this algorithm.
 java.security.MessageDigest getMessageDigest()
          Returns a MessageDigest for the specified algorithm name.
 java.lang.String getName()
          Returns the uppercase name of the hashing algorithm.
static CheckedHashAlgorithm[] getPredefinedAlgorithms()
          Returns an array of all CheckedHashAlgorithm constants defined by this class.
 java.lang.String getPrefix()
          Returns the algorithm as prefix (i.e. surrounded by curly braces).
 int hashCode()
           
static boolean isSupportedAlgorithm(java.lang.String algorithmName)
          Returns whether or not the given algorithm is supported.
protected  void setAlgorithmName(java.lang.String algorithmName)
           
protected  void setByteLength(int byteLength)
           
 java.lang.String toString()
           
 
Methods inherited from class java.lang.Object
clone, finalize, getClass, notify, notifyAll, wait, wait, wait
 

Field Detail

MD5

public static final CheckedHashAlgorithm MD5
The pre-defined constant for the MD hash algorithm.


SHA_1

public static final CheckedHashAlgorithm SHA_1
The pre-defined constant for the SHA-1 hash algorithm.


SHA_2_256

public static final CheckedHashAlgorithm SHA_2_256
The pre-defined constant for the SHA-256 hash algorithm.


SHA_2_384

public static final CheckedHashAlgorithm SHA_2_384
The pre-defined constant for the SHA-384 hash algorithm.


SHA_2_512

public static final CheckedHashAlgorithm SHA_2_512
The pre-defined constant for the SHA-512 hash algorithm.


UNKNOWN_LENGTH

public static final int UNKNOWN_LENGTH
See Also:
Constant Field Values
Constructor Detail

CheckedHashAlgorithm

protected CheckedHashAlgorithm(java.lang.String algorithmName)
                        throws java.security.NoSuchAlgorithmException
Creates successfully a checked algorithm object or throws an exception.

Parameters:
algorithmName - The name of the algorithm (e.g. @see CryptoConstants.ALGORITHM_NAME_SHA_2_512).
Throws:
java.security.NoSuchAlgorithmException - If the specified algorithm is not supported.
Method Detail

checkAlgorithm

public static void checkAlgorithm(java.lang.String algorithmName)
                           throws java.security.NoSuchAlgorithmException
Checks whether or not the given algorithm is supported.

Parameters:
algorithmName - The name of the algorithm to check (e.g. "SHA-1")
Throws:
java.security.NoSuchAlgorithmException - Will be thrown if the algorithm is not supported.

isSupportedAlgorithm

public static boolean isSupportedAlgorithm(java.lang.String algorithmName)
Returns whether or not the given algorithm is supported.

Parameters:
algorithmName - The name of the algorithm to check (e.g. "SHA-256")

create

public static CheckedHashAlgorithm create(java.lang.String algorithmName)
                                   throws java.security.NoSuchAlgorithmException
Creates a new instance based on the given algorithm name.

Parameters:
algorithmName - The name of the algorithm to create (e.g. "UTF-16")
Returns:
Returns a new instance of CheckedHashAlgorithm after checking the given algorithm name
Throws:
java.security.NoSuchAlgorithmException - Will be thrown if the algorithm is not supported.

find

public static CheckedHashAlgorithm find(java.lang.String algorithmName)
Looks for a predefined CheckedHashAlgorithm constant with the given algorithm name and returns it. Returns null if not found.

Parameters:
algorithmName - The name of the hash algorithm.

findOrCreate

public static CheckedHashAlgorithm findOrCreate(java.lang.String algorithmName)
                                         throws java.security.NoSuchAlgorithmException
Looks for a predefined CheckedHashAlgorithm constant with the given algorithm name and returns it. Tries to create a new instance if not found.

Parameters:
algorithmName - The name of the hash algorithm.
Throws:
java.security.NoSuchAlgorithmException - If the specified algorithm is not supported.

getPredefinedAlgorithms

public static CheckedHashAlgorithm[] getPredefinedAlgorithms()
Returns an array of all CheckedHashAlgorithm constants defined by this class.


detectBitLength

public static int detectBitLength(java.lang.String algorithmName)
Tries to extract the bit length part of the given algorithm name and return it as an integer.

Parameters:
algorithmName - The algorithm name (e.g. "SHA-256").
Returns:
The bit length derived from the name or UNKNOWN_LENGTH.

getName

public java.lang.String getName()
Returns the uppercase name of the hashing algorithm.


getPrefix

public java.lang.String getPrefix()
Returns the algorithm as prefix (i.e. surrounded by curly braces).

Example: algorithm "SHA-512" returns prefix "{SHA512}"


getByteLength

public int getByteLength()
Returns the byte length of this algorithm.

Returns:
The hash length in bytes of this algorithm or UNKNOWN_LENGTH if it is unknown.

getBitLength

public int getBitLength()
Returns the bit length of this algorithm.

Returns:
The hash length in bits of this algorithm or UNKNOWN_LENGTH if it is unknown.

computeUTF8Hash

public byte[] computeUTF8Hash(java.lang.String text)
Computes a hash value for the given text using the this hashing algorithm and UTF-8 encoding for the text conversion to bytes. The returned byte array contains the hash. Its length depends on the algorithm.

Parameters:
text - The text to be hashed (must not be null).
Throws:
CryptoRuntimeExcption - If the anything goes wrong.

computeHash

public byte[] computeHash(java.lang.String text,
                          org.pfsw.text.CheckedCharsetName encoding)
Computes a hash value for the given text using the this hashing algorithm. The returned byte array contains the hash. Its length depends on the algorithm.

Parameters:
text - The text to be hashed (must not be null).
encoding - Specifies the character encoding to use for the string when it gets converted to bytes.
Throws:
CryptoRuntimeExcption - If the anything goes wrong.

computeHash

public byte[] computeHash(byte[] content)
Computes a hash value for the given input data using the this hashing algorithm. The returned byte array contains the hash. Its length depends on the algorithm.

Parameters:
content - The content to be hashed (must not be null).
Throws:
CryptoRuntimeExcption - If the anything goes wrong.

getMessageDigest

public java.security.MessageDigest getMessageDigest()
Returns a MessageDigest for the specified algorithm name. Since this is an already checked algorithm no exception is expected. If still an exception occurs it will be caught and a CryptoRuntimeException thrown instead (should never happen).


equals

public boolean equals(java.lang.Object obj)
Overrides:
equals in class java.lang.Object

hashCode

public int hashCode()
Overrides:
hashCode in class java.lang.Object

toString

public java.lang.String toString()
Overrides:
toString in class java.lang.Object

setAlgorithmName

protected void setAlgorithmName(java.lang.String algorithmName)

setByteLength

protected void setByteLength(int byteLength)

detectBitLength

protected int detectBitLength()
Tries to derive the bit length from the algorithm's name.

Returns:
The bit length or UNKNOWN_LENGTH if not detectable.