Skip to content

Commit fcebd97

Browse files
Password Hash Examples
1 parent 866b6d8 commit fcebd97

19 files changed

Lines changed: 1986 additions & 0 deletions
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package com.howtodoinjava.core.basic;
2+
3+
public class StringPoolExample
4+
{
5+
public static void main(String[] args)
6+
{
7+
String a = "howtodoinjava";
8+
String b = "howtodoinjava";
9+
10+
String c = new String("howtodoinjava");
11+
String d = c.intern();
12+
13+
System.out.println(a == b); //true
14+
System.out.println(b == c); //false
15+
16+
System.out.println(a == c); //false
17+
System.out.println(a == d); //true
18+
}
19+
}
Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
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

Comments
 (0)