Class HashGenerator
- java.lang.Object
-
- com.ryanchapin.util.HashGenerator
-
public class HashGenerator extends Object
TheHashGenerator
is a class used for creating hexadecimal hashes for multiple types of input data.Supported input formats:
It supports any of the hash algorithms that are supported by the Java SE 8
MessageDigest.digest()
class/method. See the MessageDigest section in the Java Cryptography Architecture Standard Algorithm Name Documentation for information about standard algorithm names.NOTE: that the unit tests in this project DO NOT test the usage of the
MD2
digest algorithm as it has not been included in openssl since openssl-0.9.8m (2010-02-25), and is not in general use anymore.The class is thread safe depending on how it is instantiated and/or called. Used in the following manner it is thread safe:
// Calling static methods String sha1Hash = HashGenerator.createHash("This is a test", "UTF-8", HashAlgorithm.SHA1SUM);
Used in the following manner thread safety must be taken into account by the calling code:
// Calling member methods on a HashGenerator Instances HashGenerator hashGenerator = new HashGenerator(HashAlgorithm.SHA1SUM); String sha1Hash = hashGenerator.createHash("This is a test", "UTF-8");
When the
createHash
methods are called on aHashGenerator
instance, synchronization must be handled by the calling code or their must only be a single thread making calls into theHashGenerator
instance.The reason for this design is to enable the user to optimize for either "built-in" synchronization (usage of the static methods), or optimize for fewer Objects on the heap to be garbage collected.
In the case where there is a high rate and volume of calls to the
HashGenerator
static methods, resulting in garbage collection causing performance issues, the programmer can opt to instantiate aHashGenerator
. Then calls to the instance can be limited to a single thread, or the calling code can wrap theHashGenerator
in synchronized methods.The
HashGenerator
can be used to hash sensitive data as all intermediary data generated internally is explicitly wiped before the method returns to the calling code.To use the HashGenerator to hash passwords, use the methods
createHash(char[])
orcreateHash(char[], HashAlgorithm)
as this enables the caller to wipe the character array input by overwriting every element in the array with0x0
after creating a hash.DO NOT USE
String
as input data for hashing passwords asString
objects cannot be deterministically overwritten or garbage collected by the JVM.To hash PINs or other sensitive numeric data use any of the methods which accept primitive types as input and make sure to use and pass in primitive types and not their corollary wrapper classes.
- Since:
- 1.0.0
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static class
HashGenerator.DataType
Data types supported by the HashGenerator.static class
HashGenerator.HashAlgorithm
Supported hashing algorithms.
-
Constructor Summary
Constructors Constructor Description HashGenerator()
Default constructor.HashGenerator(HashGenerator.HashAlgorithm hashAlgo)
Initializes a newHashGenerator
instance such that subsequent calls can be made passing in only the data to be hashed.
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description static String
bytesToHex(byte[] hashBytes)
Generates a hexadecimal String representation of the hashed bytes.String
createHash(byte input)
Generates a hexadecimal hash of a byte and/or its wrapper class.static String
createHash(byte input, HashGenerator.HashAlgorithm hashAlgorithm)
Generates a hexadecimal hash of a byte.String
createHash(char input)
Generates a hexadecimal hash of a char and/or its wrapper class.String
createHash(char[] input)
Generates a hexadecimal hash of a character array.static String
createHash(char[] input, HashGenerator.HashAlgorithm hashAlgorithm)
Generates a hexadecimal hash of a character array.static String
createHash(char input, HashGenerator.HashAlgorithm hashAlgorithm)
Generates a hexadecimal hash of a char and/or its wrapper class.String
createHash(double input)
Generates a hexadecimal hash of a double and/or its wrapper class.static String
createHash(double input, HashGenerator.HashAlgorithm hashAlgorithm)
Generates a hexadecimal hash of a double and/or its wrapper class.String
createHash(float input)
Generates a hexadecimal hash of a long and/or its wrapper class.static String
createHash(float input, HashGenerator.HashAlgorithm hashAlgorithm)
Generates a hexadecimal hash of a long and/or its wrapper class.String
createHash(int input)
Generates a hexadecimal hash of a int and/or its wrapper class.static String
createHash(int input, HashGenerator.HashAlgorithm hashAlgorithm)
Generates a hexadecimal hash of a int and/or its wrapper class.String
createHash(long input)
Generates a hexadecimal hash of a long and/or its wrapper class.static String
createHash(long input, HashGenerator.HashAlgorithm hashAlgorithm)
Generates a hexadecimal hash of a long and/or its wrapper class.String
createHash(short input)
Generates a hexadecimal hash of a short and/or its wrapper class.static String
createHash(short input, HashGenerator.HashAlgorithm hashAlgorithm)
Generates a hexadecimal hash of a short and/or its wrapper class.String
createHash(String input, String encoding)
Generates a hexadecimal hash of a double and/or its wrapper class.static String
createHash(String input, String encoding, HashGenerator.HashAlgorithm hashAlgorithm)
Generates a hexadecimal hash of a double and/or its wrapper class.HashGenerator.HashAlgorithm
getHashAlgo()
Get the currently configured hash algorithm setting.void
setHashAlgo(HashGenerator.HashAlgorithm hashAlgo)
Sets the hash algorithm to be used for the next invocation the overloaded createHash methods.
-
-
-
Constructor Detail
-
HashGenerator
public HashGenerator(HashGenerator.HashAlgorithm hashAlgo)
Initializes a newHashGenerator
instance such that subsequent calls can be made passing in only the data to be hashed.In this context the instance is NOT thread safe and thread safety should be managed by the client. This constructor should be used in situations where client code is making large number of calls to this class and there are issues arising from garbage collection of objects that are instantiated on the stack.
In that case, the developer may opt to instantiate a new instance per thread, or simply synchronize calls to the class if that will not cause a performance bottleneck.
- Parameters:
hashAlgo
- Hash algorithm to be used to create hashes.
-
HashGenerator
public HashGenerator()
Default constructor. Instantiating in this manner leaves the instance without a configuredHashGenerator.HashAlgorithm
value, and subsequent calls will result in anIllegalStateException
.Making a subsequent, valid call to
setHashAlgo(HashAlgorithm)
will properly configure the instance.
-
-
Method Detail
-
getHashAlgo
public HashGenerator.HashAlgorithm getHashAlgo()
Get the currently configured hash algorithm setting.- Returns:
- the currently configured hash algorithm.
-
setHashAlgo
public void setHashAlgo(HashGenerator.HashAlgorithm hashAlgo)
Sets the hash algorithm to be used for the next invocation the overloaded createHash methods.- Parameters:
hashAlgo
- new hash algorithm to be set.
-
createHash
public static String createHash(byte input, HashGenerator.HashAlgorithm hashAlgorithm) throws NoSuchAlgorithmException, IllegalArgumentException
Generates a hexadecimal hash of a byte.- Parameters:
input
- byte to be hashed.hashAlgorithm
-HashGenerator.HashAlgorithm
to be used to generate the hash.- Returns:
- hexadecimal hash of the input data.
- Throws:
IllegalArgumentException
- if the hashAlgo argument is an invalidHashGenerator.HashAlgorithm
.NoSuchAlgorithmException
- if the hashAlgo argument is an invalidHashGenerator.HashAlgorithm
.
-
createHash
public String createHash(byte input) throws NoSuchAlgorithmException, IllegalStateException
Generates a hexadecimal hash of a byte and/or its wrapper class.- Parameters:
input
- byte to be hashed.- Returns:
- hexadecimal hash of the input data.
- Throws:
IllegalStateException
- if theHashGenerator
instance has not yet been configured with a validHashGenerator.HashAlgorithm
enum.NoSuchAlgorithmException
- if theHashGenerator
instance has not yet been configured with a validHashGenerator.HashAlgorithm
enum.
-
createHash
public static String createHash(char input, HashGenerator.HashAlgorithm hashAlgorithm) throws NoSuchAlgorithmException, IllegalArgumentException
Generates a hexadecimal hash of a char and/or its wrapper class.- Parameters:
input
- char to be hashed.hashAlgorithm
-HashGenerator.HashAlgorithm
to be used to generate the hash.- Returns:
- hexadecimal hash of the input data.
- Throws:
IllegalArgumentException
- if the hashAlgo argument is an invalidHashGenerator.HashAlgorithm
.NoSuchAlgorithmException
- if the hashAlgo argument is an invalidHashGenerator.HashAlgorithm
.
-
createHash
public String createHash(char input) throws NoSuchAlgorithmException, IllegalStateException
Generates a hexadecimal hash of a char and/or its wrapper class.- Parameters:
input
- char to be hashed.- Returns:
- hexadecimal hash of the input data.
- Throws:
IllegalStateException
- if theHashGenerator
instance has not yet been configured with a validHashGenerator.HashAlgorithm
enum.NoSuchAlgorithmException
- if theHashGenerator
instance has not yet been configured with a validHashGenerator.HashAlgorithm
enum.
-
createHash
public static String createHash(short input, HashGenerator.HashAlgorithm hashAlgorithm) throws NoSuchAlgorithmException, IllegalArgumentException
Generates a hexadecimal hash of a short and/or its wrapper class.- Parameters:
input
- short to be hashed.hashAlgorithm
-HashGenerator.HashAlgorithm
to be used to generate the hash.- Returns:
- hexadecimal hash of the input data.
- Throws:
IllegalArgumentException
- if the hashAlgo argument is an invalidHashGenerator.HashAlgorithm
.NoSuchAlgorithmException
- if the hashAlgo argument is an invalidHashGenerator.HashAlgorithm
.
-
createHash
public String createHash(short input) throws NoSuchAlgorithmException, IllegalStateException
Generates a hexadecimal hash of a short and/or its wrapper class.- Parameters:
input
- short to be hashed.- Returns:
- hexadecimal hash of the input data.
- Throws:
IllegalStateException
- if theHashGenerator
instance has not yet been configured with a validHashGenerator.HashAlgorithm
enum.NoSuchAlgorithmException
- if theHashGenerator
instance has not yet been configured with a validHashGenerator.HashAlgorithm
enum.
-
createHash
public static String createHash(int input, HashGenerator.HashAlgorithm hashAlgorithm) throws NoSuchAlgorithmException, IllegalArgumentException
Generates a hexadecimal hash of a int and/or its wrapper class.- Parameters:
input
- int to be hashed.hashAlgorithm
-HashGenerator.HashAlgorithm
to be used to generate the hash.- Returns:
- hexadecimal hash of the input data.
- Throws:
IllegalArgumentException
- if the hashAlgo argument is an invalidHashGenerator.HashAlgorithm
.NoSuchAlgorithmException
- if the hashAlgo argument is an invalidHashGenerator.HashAlgorithm
.
-
createHash
public String createHash(int input) throws NoSuchAlgorithmException, IllegalStateException
Generates a hexadecimal hash of a int and/or its wrapper class.- Parameters:
input
- int to be hashed.- Returns:
- hexadecimal hash of the input data.
- Throws:
IllegalStateException
- if theHashGenerator
instance has not yet been configured with a validHashGenerator.HashAlgorithm
enum.NoSuchAlgorithmException
- if theHashGenerator
instance has not yet been configured with a validHashGenerator.HashAlgorithm
enum.
-
createHash
public static String createHash(long input, HashGenerator.HashAlgorithm hashAlgorithm) throws NoSuchAlgorithmException, IllegalArgumentException
Generates a hexadecimal hash of a long and/or its wrapper class.- Parameters:
input
- int to be hashed.hashAlgorithm
-HashGenerator.HashAlgorithm
to be used to generate the hash.- Returns:
- hexadecimal hash of the input data.
- Throws:
IllegalArgumentException
- if the hashAlgo argument is an invalidHashGenerator.HashAlgorithm
.NoSuchAlgorithmException
- if the hashAlgo argument is an invalidHashGenerator.HashAlgorithm
.
-
createHash
public String createHash(long input) throws NoSuchAlgorithmException, IllegalStateException
Generates a hexadecimal hash of a long and/or its wrapper class.- Parameters:
input
- int to be hashed.- Returns:
- hexadecimal hash of the input data.
- Throws:
IllegalStateException
- if theHashGenerator
instance has not yet been configured with a validHashGenerator.HashAlgorithm
enum.NoSuchAlgorithmException
- if theHashGenerator
instance has not yet been configured with a validHashGenerator.HashAlgorithm
enum.
-
createHash
public static String createHash(float input, HashGenerator.HashAlgorithm hashAlgorithm) throws NoSuchAlgorithmException, IllegalArgumentException
Generates a hexadecimal hash of a long and/or its wrapper class.- Parameters:
input
- long to be hashed.hashAlgorithm
-HashGenerator.HashAlgorithm
to be used to generate the hash.- Returns:
- hexadecimal hash of the input data.
- Throws:
IllegalArgumentException
- if the hashAlgo argument is an invalidHashGenerator.HashAlgorithm
.NoSuchAlgorithmException
- if the hashAlgo argument is an invalidHashGenerator.HashAlgorithm
.
-
createHash
public String createHash(float input) throws NoSuchAlgorithmException, IllegalStateException
Generates a hexadecimal hash of a long and/or its wrapper class.- Parameters:
input
- long to be hashed.- Returns:
- hexadecimal hash of the input data.
- Throws:
IllegalStateException
- if theHashGenerator
instance has not yet been configured with a validHashGenerator.HashAlgorithm
enum.NoSuchAlgorithmException
- if theHashGenerator
instance has not yet been configured with a validHashGenerator.HashAlgorithm
enum.
-
createHash
public static String createHash(double input, HashGenerator.HashAlgorithm hashAlgorithm) throws NoSuchAlgorithmException, IllegalArgumentException
Generates a hexadecimal hash of a double and/or its wrapper class.- Parameters:
input
- double to be hashed.hashAlgorithm
-HashGenerator.HashAlgorithm
to be used to generate the hash.- Returns:
- hexadecimal hash of the input data.
- Throws:
IllegalArgumentException
- if the hashAlgo argument is an invalidHashGenerator.HashAlgorithm
.NoSuchAlgorithmException
- if the hashAlgo argument is an invalidHashGenerator.HashAlgorithm
.
-
createHash
public String createHash(double input) throws NoSuchAlgorithmException, IllegalStateException
Generates a hexadecimal hash of a double and/or its wrapper class.- Parameters:
input
- double to be hashed.- Returns:
- hexadecimal hash of the input data.
- Throws:
IllegalStateException
- if theHashGenerator
instance has not yet been configured with a validHashGenerator.HashAlgorithm
enum.NoSuchAlgorithmException
- if theHashGenerator
instance has not yet been configured with a validHashGenerator.HashAlgorithm
enum.
-
createHash
public static String createHash(String input, String encoding, HashGenerator.HashAlgorithm hashAlgorithm) throws UnsupportedEncodingException, IllegalArgumentException, NoSuchAlgorithmException
Generates a hexadecimal hash of a double and/or its wrapper class.- Parameters:
input
- String to be hashed.encoding
- validStandardCharsets
constant to be used when generating byte array from theString.getBytes(String)
method.hashAlgorithm
-HashGenerator.HashAlgorithm
to be used to generate the hash.- Returns:
- hexadecimal hash of the input data.
- Throws:
UnsupportedEncodingException
- if the encodingString
argument is not a valid encoding.IllegalArgumentException
- if the hashAlgo argument is an invalidHashGenerator.HashAlgorithm
.NoSuchAlgorithmException
- if the hashAlgo argument is an invalidHashGenerator.HashAlgorithm
.
-
createHash
public String createHash(String input, String encoding) throws UnsupportedEncodingException, IllegalStateException, NoSuchAlgorithmException
Generates a hexadecimal hash of a double and/or its wrapper class.- Parameters:
input
- String to be hashed.encoding
- validCharset
to be used when generating byte array from theString.getBytes(String)
method.- Returns:
- hexadecimal hash of the input data.
- Throws:
UnsupportedEncodingException
- if the encodingString
argument is not a valid encoding.IllegalStateException
- if theHashGenerator
instance has not yet been configured with a validHashGenerator.HashAlgorithm
enum.NoSuchAlgorithmException
- if theHashGenerator
instance has not yet been configured with a validHashGenerator.HashAlgorithm
enum.
-
createHash
public static String createHash(char[] input, HashGenerator.HashAlgorithm hashAlgorithm) throws IllegalArgumentException, NoSuchAlgorithmException
Generates a hexadecimal hash of a character array.- Parameters:
input
- char[] to be hashedhashAlgorithm
-HashGenerator.HashAlgorithm
to be used to generate the hash.- Returns:
- hexadecimal hash of the input data.
- Throws:
IllegalArgumentException
- if the hashAlgo argument is an invalidHashGenerator.HashAlgorithm
.NoSuchAlgorithmException
- if the hashAlgo argument is an invalidHashGenerator.HashAlgorithm
.
-
createHash
public String createHash(char[] input) throws IllegalStateException, NoSuchAlgorithmException
Generates a hexadecimal hash of a character array.- Parameters:
input
- char[] to be hashed- Returns:
- hexadecimal hash of the input data.
- Throws:
IllegalStateException
- if theHashGenerator
instance has not yet been configured with a validHashGenerator.HashAlgorithm
enum.NoSuchAlgorithmException
- if theHashGenerator
instance has not yet been configured with a validHashGenerator.HashAlgorithm
enum.
-
bytesToHex
public static String bytesToHex(byte[] hashBytes)
Generates a hexadecimal String representation of the hashed bytes.- Parameters:
hashBytes
- byte[] output from theMessageDigest.digest()
method.- Returns:
- hexadecimal representation of the hashed bytes.
-
-