|
|||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Objectorg.pfsw.security.crypto.CheckedHashAlgorithm
public class CheckedHashAlgorithm
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.
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 |
---|
public static final CheckedHashAlgorithm MD5
public static final CheckedHashAlgorithm SHA_1
public static final CheckedHashAlgorithm SHA_2_256
public static final CheckedHashAlgorithm SHA_2_384
public static final CheckedHashAlgorithm SHA_2_512
public static final int UNKNOWN_LENGTH
Constructor Detail |
---|
protected CheckedHashAlgorithm(java.lang.String algorithmName) throws java.security.NoSuchAlgorithmException
algorithmName
- The name of the algorithm (e.g. @see CryptoConstants.ALGORITHM_NAME_SHA_2_512
).
java.security.NoSuchAlgorithmException
- If the specified algorithm is not supported.Method Detail |
---|
public static void checkAlgorithm(java.lang.String algorithmName) throws java.security.NoSuchAlgorithmException
algorithmName
- The name of the algorithm to check (e.g. "SHA-1")
java.security.NoSuchAlgorithmException
- Will be thrown if the algorithm is not supported.public static boolean isSupportedAlgorithm(java.lang.String algorithmName)
algorithmName
- The name of the algorithm to check (e.g. "SHA-256")public static CheckedHashAlgorithm create(java.lang.String algorithmName) throws java.security.NoSuchAlgorithmException
algorithmName
- The name of the algorithm to create (e.g. "UTF-16")
java.security.NoSuchAlgorithmException
- Will be thrown if the algorithm is not supported.public static CheckedHashAlgorithm find(java.lang.String algorithmName)
algorithmName
- The name of the hash algorithm.public static CheckedHashAlgorithm findOrCreate(java.lang.String algorithmName) throws java.security.NoSuchAlgorithmException
algorithmName
- The name of the hash algorithm.
java.security.NoSuchAlgorithmException
- If the specified algorithm is not supported.public static CheckedHashAlgorithm[] getPredefinedAlgorithms()
public static int detectBitLength(java.lang.String algorithmName)
algorithmName
- The algorithm name (e.g. "SHA-256").
UNKNOWN_LENGTH
.public java.lang.String getName()
public java.lang.String getPrefix()
Example: algorithm "SHA-512" returns prefix "{SHA512}"
public int getByteLength()
UNKNOWN_LENGTH
if it is unknown.public int getBitLength()
UNKNOWN_LENGTH
if it is unknown.public byte[] computeUTF8Hash(java.lang.String text)
text
- The text to be hashed (must not be null).
CryptoRuntimeExcption
- If the anything goes wrong.public byte[] computeHash(java.lang.String text, org.pfsw.text.CheckedCharsetName encoding)
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.
CryptoRuntimeExcption
- If the anything goes wrong.public byte[] computeHash(byte[] content)
content
- The content to be hashed (must not be null).
CryptoRuntimeExcption
- If the anything goes wrong.public java.security.MessageDigest getMessageDigest()
public boolean equals(java.lang.Object obj)
equals
in class java.lang.Object
public int hashCode()
hashCode
in class java.lang.Object
public java.lang.String toString()
toString
in class java.lang.Object
protected void setAlgorithmName(java.lang.String algorithmName)
protected void setByteLength(int byteLength)
protected int detectBitLength()
UNKNOWN_LENGTH
if not detectable.
|
|||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |