|
| 1 | +package com.howtodoinjava.hashing.password.demo.advanced; |
| 2 | + |
| 3 | +import java.math.BigInteger; |
| 4 | +import java.security.NoSuchAlgorithmException; |
| 5 | +import java.security.SecureRandom; |
| 6 | +import java.security.spec.InvalidKeySpecException; |
| 7 | + |
| 8 | +import javax.crypto.SecretKeyFactory; |
| 9 | +import javax.crypto.spec.PBEKeySpec; |
| 10 | + |
| 11 | +public class ReallyStrongSecuredPassword { |
| 12 | + |
| 13 | + public static void main(String[] args) throws NoSuchAlgorithmException, InvalidKeySpecException { |
| 14 | + |
| 15 | + String originalPassword = "password"; |
| 16 | + String generatedSecuredPasswordHash = generateStorngPasswordHash(originalPassword); |
| 17 | + System.out.println(generatedSecuredPasswordHash); |
| 18 | + |
| 19 | + boolean matched = validatePassword("password", generatedSecuredPasswordHash); |
| 20 | + System.out.println(matched); |
| 21 | + |
| 22 | + matched = validatePassword("password1", generatedSecuredPasswordHash); |
| 23 | + System.out.println(matched); |
| 24 | + } |
| 25 | + |
| 26 | + private static boolean validatePassword(String originalPassword, String storedPassword) |
| 27 | + throws NoSuchAlgorithmException, InvalidKeySpecException { |
| 28 | + String[] parts = storedPassword.split(":"); |
| 29 | + int iterations = Integer.parseInt(parts[0]); |
| 30 | + byte[] salt = fromHex(parts[1]); |
| 31 | + byte[] hash = fromHex(parts[2]); |
| 32 | + |
| 33 | + PBEKeySpec spec = |
| 34 | + new PBEKeySpec(originalPassword.toCharArray(), salt, iterations, hash.length * 8); |
| 35 | + |
| 36 | + SecretKeyFactory skf = SecretKeyFactory.getInstance("PBKDF2WithHmacSHA1"); |
| 37 | + |
| 38 | + byte[] testHash = skf.generateSecret(spec).getEncoded(); |
| 39 | + |
| 40 | + int diff = hash.length ^ testHash.length; |
| 41 | + |
| 42 | + for (int i = 0; i < hash.length && i < testHash.length; i++) { |
| 43 | + diff |= hash[i] ^ testHash[i]; |
| 44 | + } |
| 45 | + |
| 46 | + return diff == 0; |
| 47 | + } |
| 48 | + |
| 49 | + private static String generateStorngPasswordHash(String password) |
| 50 | + throws NoSuchAlgorithmException, InvalidKeySpecException { |
| 51 | + int iterations = 1000; |
| 52 | + char[] chars = password.toCharArray(); |
| 53 | + byte[] salt = getSalt().getBytes(); |
| 54 | + |
| 55 | + PBEKeySpec spec = new PBEKeySpec(chars, salt, iterations, 64 * 8); |
| 56 | + SecretKeyFactory skf = SecretKeyFactory.getInstance("PBKDF2WithHmacSHA1"); |
| 57 | + byte[] hash = skf.generateSecret(spec).getEncoded(); |
| 58 | + return iterations + ":" + toHex(salt) + ":" + toHex(hash); |
| 59 | + |
| 60 | + } |
| 61 | + |
| 62 | + private static String getSalt() throws NoSuchAlgorithmException { |
| 63 | + SecureRandom sr = SecureRandom.getInstance("SHA1PRNG"); |
| 64 | + byte[] salt = new byte[16]; |
| 65 | + sr.nextBytes(salt); |
| 66 | + return salt.toString(); |
| 67 | + } |
| 68 | + |
| 69 | + private static String toHex(byte[] array) throws NoSuchAlgorithmException { |
| 70 | + BigInteger bi = new BigInteger(1, array); |
| 71 | + String hex = bi.toString(16); |
| 72 | + int paddingLength = (array.length * 2) - hex.length(); |
| 73 | + if (paddingLength > 0) { |
| 74 | + return String.format("%0" + paddingLength + "d", 0) + hex; |
| 75 | + } else { |
| 76 | + return hex; |
| 77 | + } |
| 78 | + } |
| 79 | + |
| 80 | + private static byte[] fromHex(String hex) throws NoSuchAlgorithmException { |
| 81 | + byte[] bytes = new byte[hex.length() / 2]; |
| 82 | + for (int i = 0; i < bytes.length; i++) { |
| 83 | + bytes[i] = (byte) Integer.parseInt(hex.substring(2 * i, 2 * i + 2), 16); |
| 84 | + } |
| 85 | + return bytes; |
| 86 | + } |
| 87 | +} |
0 commit comments