Class HashGenerator
- java.lang.Object
- 
- com.ryanchapin.util.HashGenerator
 
- 
 public class HashGenerator extends Object TheHashGeneratoris 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 MD2digest 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 createHashmethods are called on aHashGeneratorinstance, synchronization must be handled by the calling code or their must only be a single thread making calls into theHashGeneratorinstance.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 HashGeneratorstatic 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 theHashGeneratorin synchronized methods.The HashGeneratorcan 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 with0x0after creating a hash.DO NOT USE Stringas input data for hashing passwords asStringobjects 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 SummaryNested Classes Modifier and Type Class Description static classHashGenerator.DataTypeData types supported by the HashGenerator.static classHashGenerator.HashAlgorithmSupported hashing algorithms.
 - 
Constructor SummaryConstructors Constructor Description HashGenerator()Default constructor.HashGenerator(HashGenerator.HashAlgorithm hashAlgo)Initializes a newHashGeneratorinstance such that subsequent calls can be made passing in only the data to be hashed.
 - 
Method SummaryAll Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description static StringbytesToHex(byte[] hashBytes)Generates a hexadecimal String representation of the hashed bytes.StringcreateHash(byte input)Generates a hexadecimal hash of a byte and/or its wrapper class.static StringcreateHash(byte input, HashGenerator.HashAlgorithm hashAlgorithm)Generates a hexadecimal hash of a byte.StringcreateHash(char input)Generates a hexadecimal hash of a char and/or its wrapper class.StringcreateHash(char[] input)Generates a hexadecimal hash of a character array.static StringcreateHash(char[] input, HashGenerator.HashAlgorithm hashAlgorithm)Generates a hexadecimal hash of a character array.static StringcreateHash(char input, HashGenerator.HashAlgorithm hashAlgorithm)Generates a hexadecimal hash of a char and/or its wrapper class.StringcreateHash(double input)Generates a hexadecimal hash of a double and/or its wrapper class.static StringcreateHash(double input, HashGenerator.HashAlgorithm hashAlgorithm)Generates a hexadecimal hash of a double and/or its wrapper class.StringcreateHash(float input)Generates a hexadecimal hash of a long and/or its wrapper class.static StringcreateHash(float input, HashGenerator.HashAlgorithm hashAlgorithm)Generates a hexadecimal hash of a long and/or its wrapper class.StringcreateHash(int input)Generates a hexadecimal hash of a int and/or its wrapper class.static StringcreateHash(int input, HashGenerator.HashAlgorithm hashAlgorithm)Generates a hexadecimal hash of a int and/or its wrapper class.StringcreateHash(long input)Generates a hexadecimal hash of a long and/or its wrapper class.static StringcreateHash(long input, HashGenerator.HashAlgorithm hashAlgorithm)Generates a hexadecimal hash of a long and/or its wrapper class.StringcreateHash(short input)Generates a hexadecimal hash of a short and/or its wrapper class.static StringcreateHash(short input, HashGenerator.HashAlgorithm hashAlgorithm)Generates a hexadecimal hash of a short and/or its wrapper class.StringcreateHash(String input, String encoding)Generates a hexadecimal hash of a double and/or its wrapper class.static StringcreateHash(String input, String encoding, HashGenerator.HashAlgorithm hashAlgorithm)Generates a hexadecimal hash of a double and/or its wrapper class.HashGenerator.HashAlgorithmgetHashAlgo()Get the currently configured hash algorithm setting.voidsetHashAlgo(HashGenerator.HashAlgorithm hashAlgo)Sets the hash algorithm to be used for the next invocation the overloaded createHash methods.
 
- 
- 
- 
Constructor Detail- 
HashGeneratorpublic HashGenerator(HashGenerator.HashAlgorithm hashAlgo) Initializes a newHashGeneratorinstance 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.
 
 - 
HashGeneratorpublic HashGenerator() Default constructor. Instantiating in this manner leaves the instance without a configuredHashGenerator.HashAlgorithmvalue, and subsequent calls will result in anIllegalStateException.Making a subsequent, valid call to setHashAlgo(HashAlgorithm)will properly configure the instance.
 
- 
 - 
Method Detail- 
getHashAlgopublic HashGenerator.HashAlgorithm getHashAlgo() Get the currently configured hash algorithm setting.- Returns:
- the currently configured hash algorithm.
 
 - 
setHashAlgopublic 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.
 
 - 
createHashpublic 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.HashAlgorithmto be used to generate the hash.
- Returns:
- hexadecimal hash of the input data.
- Throws:
- IllegalArgumentException- if the hashAlgo argument is an invalid- HashGenerator.HashAlgorithm.
- NoSuchAlgorithmException- if the hashAlgo argument is an invalid- HashGenerator.HashAlgorithm.
 
 - 
createHashpublic 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 the- HashGeneratorinstance has not yet been configured with a valid- HashGenerator.HashAlgorithmenum.
- NoSuchAlgorithmException- if the- HashGeneratorinstance has not yet been configured with a valid- HashGenerator.HashAlgorithmenum.
 
 - 
createHashpublic 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.HashAlgorithmto be used to generate the hash.
- Returns:
- hexadecimal hash of the input data.
- Throws:
- IllegalArgumentException- if the hashAlgo argument is an invalid- HashGenerator.HashAlgorithm.
- NoSuchAlgorithmException- if the hashAlgo argument is an invalid- HashGenerator.HashAlgorithm.
 
 - 
createHashpublic 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 the- HashGeneratorinstance has not yet been configured with a valid- HashGenerator.HashAlgorithmenum.
- NoSuchAlgorithmException- if the- HashGeneratorinstance has not yet been configured with a valid- HashGenerator.HashAlgorithmenum.
 
 - 
createHashpublic 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.HashAlgorithmto be used to generate the hash.
- Returns:
- hexadecimal hash of the input data.
- Throws:
- IllegalArgumentException- if the hashAlgo argument is an invalid- HashGenerator.HashAlgorithm.
- NoSuchAlgorithmException- if the hashAlgo argument is an invalid- HashGenerator.HashAlgorithm.
 
 - 
createHashpublic 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 the- HashGeneratorinstance has not yet been configured with a valid- HashGenerator.HashAlgorithmenum.
- NoSuchAlgorithmException- if the- HashGeneratorinstance has not yet been configured with a valid- HashGenerator.HashAlgorithmenum.
 
 - 
createHashpublic 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.HashAlgorithmto be used to generate the hash.
- Returns:
- hexadecimal hash of the input data.
- Throws:
- IllegalArgumentException- if the hashAlgo argument is an invalid- HashGenerator.HashAlgorithm.
- NoSuchAlgorithmException- if the hashAlgo argument is an invalid- HashGenerator.HashAlgorithm.
 
 - 
createHashpublic 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 the- HashGeneratorinstance has not yet been configured with a valid- HashGenerator.HashAlgorithmenum.
- NoSuchAlgorithmException- if the- HashGeneratorinstance has not yet been configured with a valid- HashGenerator.HashAlgorithmenum.
 
 - 
createHashpublic 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.HashAlgorithmto be used to generate the hash.
- Returns:
- hexadecimal hash of the input data.
- Throws:
- IllegalArgumentException- if the hashAlgo argument is an invalid- HashGenerator.HashAlgorithm.
- NoSuchAlgorithmException- if the hashAlgo argument is an invalid- HashGenerator.HashAlgorithm.
 
 - 
createHashpublic 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 the- HashGeneratorinstance has not yet been configured with a valid- HashGenerator.HashAlgorithmenum.
- NoSuchAlgorithmException- if the- HashGeneratorinstance has not yet been configured with a valid- HashGenerator.HashAlgorithmenum.
 
 - 
createHashpublic 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.HashAlgorithmto be used to generate the hash.
- Returns:
- hexadecimal hash of the input data.
- Throws:
- IllegalArgumentException- if the hashAlgo argument is an invalid- HashGenerator.HashAlgorithm.
- NoSuchAlgorithmException- if the hashAlgo argument is an invalid- HashGenerator.HashAlgorithm.
 
 - 
createHashpublic 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 the- HashGeneratorinstance has not yet been configured with a valid- HashGenerator.HashAlgorithmenum.
- NoSuchAlgorithmException- if the- HashGeneratorinstance has not yet been configured with a valid- HashGenerator.HashAlgorithmenum.
 
 - 
createHashpublic 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.HashAlgorithmto be used to generate the hash.
- Returns:
- hexadecimal hash of the input data.
- Throws:
- IllegalArgumentException- if the hashAlgo argument is an invalid- HashGenerator.HashAlgorithm.
- NoSuchAlgorithmException- if the hashAlgo argument is an invalid- HashGenerator.HashAlgorithm.
 
 - 
createHashpublic 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 the- HashGeneratorinstance has not yet been configured with a valid- HashGenerator.HashAlgorithmenum.
- NoSuchAlgorithmException- if the- HashGeneratorinstance has not yet been configured with a valid- HashGenerator.HashAlgorithmenum.
 
 - 
createHashpublic 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- valid- StandardCharsetsconstant to be used when generating byte array from the- String.getBytes(String)method.
- hashAlgorithm-- HashGenerator.HashAlgorithmto be used to generate the hash.
- Returns:
- hexadecimal hash of the input data.
- Throws:
- UnsupportedEncodingException- if the encoding- Stringargument is not a valid encoding.
- IllegalArgumentException- if the hashAlgo argument is an invalid- HashGenerator.HashAlgorithm.
- NoSuchAlgorithmException- if the hashAlgo argument is an invalid- HashGenerator.HashAlgorithm.
 
 - 
createHashpublic 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- valid- Charsetto be used when generating byte array from the- String.getBytes(String)method.
- Returns:
- hexadecimal hash of the input data.
- Throws:
- UnsupportedEncodingException- if the encoding- Stringargument is not a valid encoding.
- IllegalStateException- if the- HashGeneratorinstance has not yet been configured with a valid- HashGenerator.HashAlgorithmenum.
- NoSuchAlgorithmException- if the- HashGeneratorinstance has not yet been configured with a valid- HashGenerator.HashAlgorithmenum.
 
 - 
createHashpublic static String createHash(char[] input, HashGenerator.HashAlgorithm hashAlgorithm) throws IllegalArgumentException, NoSuchAlgorithmException Generates a hexadecimal hash of a character array.- Parameters:
- input- char[] to be hashed
- hashAlgorithm-- HashGenerator.HashAlgorithmto be used to generate the hash.
- Returns:
- hexadecimal hash of the input data.
- Throws:
- IllegalArgumentException- if the hashAlgo argument is an invalid- HashGenerator.HashAlgorithm.
- NoSuchAlgorithmException- if the hashAlgo argument is an invalid- HashGenerator.HashAlgorithm.
 
 - 
createHashpublic 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 the- HashGeneratorinstance has not yet been configured with a valid- HashGenerator.HashAlgorithmenum.
- NoSuchAlgorithmException- if the- HashGeneratorinstance has not yet been configured with a valid- HashGenerator.HashAlgorithmenum.
 
 - 
bytesToHexpublic static String bytesToHex(byte[] hashBytes) Generates a hexadecimal String representation of the hashed bytes.- Parameters:
- hashBytes- byte[] output from the- MessageDigest.digest()method.
- Returns:
- hexadecimal representation of the hashed bytes.
 
 
- 
 
-