|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Objectorg.pf.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
|
Method Summary | |
---|---|
static void |
checkAlgorithm(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(String text,
org.pf.text.CheckedCharsetName encoding)
Computes a hash value for the given text using the this hashing algorithm. |
byte[] |
computeUTF8Hash(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(String algorithmName)
Creates a new instance based on the given algorithm name. |
static int |
detectBitLength(String algorithmName)
Tries to extract the bit length part of the given algorithm name and return it as an integer. |
boolean |
equals(Object obj)
|
static CheckedHashAlgorithm |
find(String algorithmName)
Looks for a predefined CheckedHashAlgorithm constant with the given algorithm name and returns it. |
static CheckedHashAlgorithm |
findOrCreate(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. |
MessageDigest |
getMessageDigest()
Returns a MessageDigest for the specified algorithm name. |
String |
getName()
Returns the uppercase name of the hashing algorithm. |
static CheckedHashAlgorithm[] |
getPredefinedAlgorithms()
Returns an array of all CheckedHashAlgorithm constants defined by this class. |
String |
getPrefix()
Returns the algorithm as prefix (i.e. surrounded by curly braces). |
int |
hashCode()
|
static boolean |
isSupportedAlgorithm(String algorithmName)
Returns whether or not the given algorithm is supported. |
String |
toString()
|
Methods inherited from class java.lang.Object |
---|
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
Method Detail |
---|
public static void checkAlgorithm(String algorithmName) throws NoSuchAlgorithmException
algorithmName
- The name of the algorithm to check (e.g. "SHA-1")
NoSuchAlgorithmException
- Will be thrown if the algorithm is not supported.public static boolean isSupportedAlgorithm(String algorithmName)
algorithmName
- The name of the algorithm to check (e.g. "SHA-256")public static CheckedHashAlgorithm create(String algorithmName) throws NoSuchAlgorithmException
algorithmName
- The name of the algorithm to create (e.g. "UTF-16")
NoSuchAlgorithmException
- Will be thrown if the algorithm is not supported.public static CheckedHashAlgorithm find(String algorithmName)
algorithmName
- The name of the hash algorithm.public static CheckedHashAlgorithm findOrCreate(String algorithmName) throws NoSuchAlgorithmException
algorithmName
- The name of the hash algorithm.
NoSuchAlgorithmException
- If the specified algorithm is not supported.public static CheckedHashAlgorithm[] getPredefinedAlgorithms()
public static int detectBitLength(String algorithmName)
algorithmName
- The algorithm name (e.g. "SHA-256").
UNKNOWN_LENGTH
.public String getName()
public 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(String text)
text
- The text to be hashed (must not be null).
CryptoRuntimeExcption
- If the anything goes wrong.public byte[] computeHash(String text, org.pf.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 MessageDigest getMessageDigest()
public boolean equals(Object obj)
equals
in class Object
public int hashCode()
hashCode
in class Object
public String toString()
toString
in class Object
|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |