org.pfsw.security.crypto
Class HashMechanism

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

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

Contains all information about a hashing algorithm.

See Also:
Serialized Form

Field Summary
static HashMechanism MD5
           
static HashMechanism SALTED_MD5
           
static HashMechanism SALTED_SHA_1
           
static HashMechanism SALTED_SHA_2_256
           
static HashMechanism SALTED_SHA_2_384
           
static HashMechanism SALTED_SHA_2_512
           
static HashMechanism SHA_1
           
static HashMechanism SHA_2_256
           
static HashMechanism SHA_2_384
           
static HashMechanism SHA_2_512
           
 
Constructor Summary
HashMechanism(java.lang.String hashMechanismName, CheckedHashAlgorithm algorithm, boolean isSalted)
          Creates a new instance that represents a particular hashing mechanism.
 
Method Summary
static HashMechanism create(CheckedHashAlgorithm algorithm)
          Creates a new instance that represents a particular unsalted hashing mechanism.
static HashMechanism create(CheckedHashAlgorithm algorithm, boolean isSalted)
          Creates a new instance that represents a particular hashing mechanism.
static HashMechanism create(java.lang.String hashMechanismName, CheckedHashAlgorithm algorithm, boolean isSalted)
          Creates a new instance that represents a particular hashing mechanism.
static java.lang.String createHashMechanismName(CheckedHashAlgorithm algorithm, boolean isSalted)
          Returns a name for the hashing mechanism that is defined by the given parameters.
static java.lang.String createPrefix(java.lang.String hashMechanismName)
          Returns a prefix for the provided hashing mechanism name.
 boolean equals(java.lang.Object obj)
           
static HashMechanism findHashMechanism(java.lang.String hashType)
          Tries to find a known (i.e. registered constant) hash mechanism for the given type name.
static HashMechanism findSaltedHashMechanism(java.lang.String hashType)
           
static HashMechanism findUnsaltedHashMechanism(java.lang.String hashType)
           
 CheckedHashAlgorithm getAlgorithm()
           
 java.lang.String getAlgorithmName()
           
 int getBitLength()
           
 int getByteLength()
           
protected  java.lang.String getHashMechanismName()
           
 java.lang.String getName()
          Returns the name of this mechanism.
 java.lang.String getPrefix()
           
 int hashCode()
           
 boolean isSalted()
           
protected  void setAlgorithm(CheckedHashAlgorithm newValue)
           
protected  void setHashMechanismName(java.lang.String newValue)
           
protected  void setIsSalted(boolean newValue)
           
protected  void setPrefix(java.lang.String newValue)
           
 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 HashMechanism MD5

SHA_1

public static final HashMechanism SHA_1

SHA_2_256

public static final HashMechanism SHA_2_256

SHA_2_384

public static final HashMechanism SHA_2_384

SHA_2_512

public static final HashMechanism SHA_2_512

SALTED_MD5

public static final HashMechanism SALTED_MD5

SALTED_SHA_1

public static final HashMechanism SALTED_SHA_1

SALTED_SHA_2_256

public static final HashMechanism SALTED_SHA_2_256

SALTED_SHA_2_384

public static final HashMechanism SALTED_SHA_2_384

SALTED_SHA_2_512

public static final HashMechanism SALTED_SHA_2_512
Constructor Detail

HashMechanism

public HashMechanism(java.lang.String hashMechanismName,
                     CheckedHashAlgorithm algorithm,
                     boolean isSalted)
Creates a new instance that represents a particular hashing mechanism.

Parameters:
hashMechanismName - The name of the mechanism to be used in the prefix.
algorithm - The underlying hashing algorithm.
isSalted - Defines whether or not the mechanism is salted.
Method Detail

createPrefix

public static java.lang.String createPrefix(java.lang.String hashMechanismName)
Returns a prefix for the provided hashing mechanism name.

Parameters:
hashMechanismName - The name to be used in the prefix (must not be null).

createHashMechanismName

public static java.lang.String createHashMechanismName(CheckedHashAlgorithm algorithm,
                                                       boolean isSalted)
Returns a name for the hashing mechanism that is defined by the given parameters.

Example: algorithm is "SHA-256" and isSalted = true. That returns "SSHA256".

Parameters:
algorithm - Defines the underlying hashing algorithm.
isSalted - Defines whether or not the mechanism is salted.

create

public static HashMechanism create(CheckedHashAlgorithm algorithm)
Creates a new instance that represents a particular unsalted hashing mechanism.

Parameters:
algorithm - The underlying hashing algorithm.

create

public static HashMechanism create(CheckedHashAlgorithm algorithm,
                                   boolean isSalted)
Creates a new instance that represents a particular hashing mechanism.

Parameters:
algorithm - The underlying hashing algorithm.
isSalted - Defines whether or not the mechanism is salted.

create

public static HashMechanism create(java.lang.String hashMechanismName,
                                   CheckedHashAlgorithm algorithm,
                                   boolean isSalted)
Creates a new instance that represents a particular hashing mechanism.

Parameters:
hashMechanismName - The name of the mechanism to be used in the prefix.
algorithm - The underlying hashing algorithm.
isSalted - Defines whether or not the mechanism is salted.

findUnsaltedHashMechanism

public static HashMechanism findUnsaltedHashMechanism(java.lang.String hashType)

findSaltedHashMechanism

public static HashMechanism findSaltedHashMechanism(java.lang.String hashType)

findHashMechanism

public static HashMechanism findHashMechanism(java.lang.String hashType)
Tries to find a known (i.e. registered constant) hash mechanism for the given type name.

Parameters:
hashType - The name of an hashing algorithm ("SHA-1") or the prefix (e.g. "{SSHA384}").
Returns:
The found mechanism or null if not found.

getName

public java.lang.String getName()
Returns the name of this mechanism.


getAlgorithm

public CheckedHashAlgorithm getAlgorithm()

getByteLength

public int getByteLength()

getPrefix

public java.lang.String getPrefix()

isSalted

public boolean isSalted()

getAlgorithmName

public java.lang.String getAlgorithmName()

getBitLength

public int getBitLength()

toString

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

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

getHashMechanismName

protected java.lang.String getHashMechanismName()

setAlgorithm

protected void setAlgorithm(CheckedHashAlgorithm newValue)

setHashMechanismName

protected void setHashMechanismName(java.lang.String newValue)

setPrefix

protected void setPrefix(java.lang.String newValue)

setIsSalted

protected void setIsSalted(boolean newValue)