|
| 1 | +package io.pointgenesis.utilities.security.hash; |
| 2 | + |
| 3 | +import java.security.NoSuchAlgorithmException; |
| 4 | +import java.security.spec.InvalidKeySpecException; |
| 5 | + |
| 6 | +import javax.crypto.SecretKey; |
| 7 | +import javax.crypto.SecretKeyFactory; |
| 8 | +import javax.crypto.spec.PBEKeySpec; |
| 9 | +import javax.xml.bind.DatatypeConverter; |
| 10 | + |
| 11 | +import org.apache.logging.log4j.LogManager; |
| 12 | +import org.apache.logging.log4j.Logger; |
| 13 | + |
| 14 | +/** |
| 15 | + * Generates a hash following the OWASP recommended practices for password hashing. |
| 16 | + * |
| 17 | + * References: |
| 18 | + * |
| 19 | + * [1] https://www.owasp.org/index.php/Hashing_Java |
| 20 | + * |
| 21 | + * @author Travis L. Steinmetz |
| 22 | + * |
| 23 | + * Copyright 2019 Point Genesis Solutions, LLC |
| 24 | + * |
| 25 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 26 | + * you may not use this file except in compliance with the License. |
| 27 | + * You may obtain a copy of the License at |
| 28 | + * |
| 29 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 30 | + * |
| 31 | + * Unless required by applicable law or agreed to in writing, software |
| 32 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 33 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 34 | + * See the License for the specific language governing permissions and |
| 35 | + * limitations under the License. |
| 36 | + */ |
| 37 | + |
| 38 | +public class SecureHash { |
| 39 | + private static final Logger log = LogManager.getLogger(); |
| 40 | + |
| 41 | + /** Marked private to prevent outside instantiation. **/ |
| 42 | + private SecureHash() { |
| 43 | + } |
| 44 | + |
| 45 | + /** |
| 46 | + * Generates a one-way hash from the given inputs. |
| 47 | + * |
| 48 | + * @param value the clear text value to transform. |
| 49 | + * @param salt the salt value to apply while hashing {@code}value. |
| 50 | + * |
| 51 | + * @return the hashed representation of {@code}value. |
| 52 | + */ |
| 53 | + public static String getHash(final String value, final String salt) { |
| 54 | + int DEFAULT_ITERATIONS = 65536; |
| 55 | + return getHash(value, salt, DEFAULT_ITERATIONS); |
| 56 | + } |
| 57 | + |
| 58 | + /** |
| 59 | + * Generates a one-way hash from the given inputs. |
| 60 | + * |
| 61 | + * @param value the clear text value to transform. |
| 62 | + * @param salt the salt value to apply while hashing {@code}value. |
| 63 | + * @param iterations the number of hash iterations to execute against {@code}value. |
| 64 | + * |
| 65 | + * @return the hashed representation of {@code}value. |
| 66 | + */ |
| 67 | + public static String getHash(final String value, final String salt, final int iterations) { |
| 68 | + int DEFAULT_LENGTH = 256; |
| 69 | + return getHash(value, salt, iterations, DEFAULT_LENGTH); |
| 70 | + } |
| 71 | + |
| 72 | + /** |
| 73 | + * Generates a one-way hash from the given inputs. |
| 74 | + * |
| 75 | + * @param value the clear text value to transform. |
| 76 | + * @param salt the salt value to apply while hashing {@code}value. |
| 77 | + * @param iterations the number of hash iterations to execute against {@code}value. |
| 78 | + * @param length the key length. |
| 79 | + * |
| 80 | + * @return the hashed representation of {@code}value. |
| 81 | + */ |
| 82 | + public static String getHash( |
| 83 | + final String value, final String salt, |
| 84 | + final int iterations, final int length) { |
| 85 | + String DEFAULT_ALGORITHM = "PBKDF2WithHmacSHA512"; |
| 86 | + return getHash(value, salt, iterations, length, DEFAULT_ALGORITHM); |
| 87 | + } |
| 88 | + |
| 89 | + /** |
| 90 | + * Generates a one-way hash from the given inputs. |
| 91 | + * |
| 92 | + * @param value the clear text value to transform. |
| 93 | + * @param salt the salt value to apply while hashing {@code}value. |
| 94 | + * @param iterations the number of hash iterations to execute against {@code}value. |
| 95 | + * @param length the key length. |
| 96 | + * @param algorithm the algorithm used in the hash generation. |
| 97 | + * |
| 98 | + * @return the hashed representation of {@code}value. |
| 99 | + */ |
| 100 | + public static String getHash( |
| 101 | + final String value, final String salt, |
| 102 | + final int iterations, final int length, final String algorithm) { |
| 103 | + boolean isValidInput = true; |
| 104 | + |
| 105 | + if (value == null || value.isEmpty()) { |
| 106 | + isValidInput = false; |
| 107 | + log.error("\"value\" cannot be null or empty."); |
| 108 | + } |
| 109 | + |
| 110 | + if (salt == null || salt.isEmpty()) { |
| 111 | + isValidInput = false; |
| 112 | + log.error("\"salt\" cannot be null or empty."); |
| 113 | + } |
| 114 | + |
| 115 | + if (algorithm == null || algorithm.isEmpty()) { |
| 116 | + isValidInput = false; |
| 117 | + log.error("\"algorithm\" cannot be null or empty."); |
| 118 | + } |
| 119 | + |
| 120 | + try { |
| 121 | + if (isValidInput == false) { |
| 122 | + throw new IllegalArgumentException("One or more inputs do not conform to the expected format."); |
| 123 | + } |
| 124 | + |
| 125 | + SecretKeyFactory skf = SecretKeyFactory.getInstance(algorithm); |
| 126 | + PBEKeySpec spec = new PBEKeySpec(value.toCharArray(), DatatypeConverter.parseHexBinary(salt), iterations, length); |
| 127 | + SecretKey key = skf.generateSecret(spec); |
| 128 | + |
| 129 | + return DatatypeConverter.printHexBinary(key.getEncoded()); |
| 130 | + } catch (NoSuchAlgorithmException | InvalidKeySpecException e) { |
| 131 | + String valueAsString = null; |
| 132 | + if (value != null) { |
| 133 | + valueAsString = new String(value); |
| 134 | + try { |
| 135 | + /*Obfuscate the value passed into the method for security purposes.*/ |
| 136 | + valueAsString = DatatypeConverter.printHexBinary(valueAsString.getBytes()); |
| 137 | + } catch (Exception ex) { |
| 138 | + log.warn("Unable to convert given \"value\" to hex binary format."); |
| 139 | + } |
| 140 | + } |
| 141 | + log.error( |
| 142 | + "Unable to compute hash of the following inputs -> value: {} | salt: {} | iterations: {} | length: {} | algorithm: {}", |
| 143 | + valueAsString, salt, iterations, length, algorithm); |
| 144 | + throw new IllegalArgumentException("Unable to generate a hash using the provided inputs.", e); |
| 145 | + } |
| 146 | + } |
| 147 | + |
| 148 | +} |
0 commit comments