Skip to content

Commit 53aa22a

Browse files
authored
Added BCrypt hashing (StubbornJava#45)
1 parent b5312ef commit 53aa22a

5 files changed

Lines changed: 220 additions & 2 deletions

File tree

‎gradle/dependencies.gradle‎

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,8 @@ ext {
2323
jsoup : '1.10.3', // DOM parsing library
2424
lombok : '1.16.16', // Code gen
2525
sitemapgen4j : '1.0.6', // Sitemap generator for SEO
26-
26+
jbcrypt : '0.4', // BCrypt salted hashing library
27+
2728
junit : '4.12', // Unit Testing
2829
]
2930
libs = [
@@ -66,7 +67,8 @@ ext {
6667
jsoup : "org.jsoup:jsoup:$versions.jsoup",
6768
lombok : "org.projectlombok:lombok:$versions.lombok",
6869
sitemapgen4j : "com.github.dfabulich:sitemapgen4j:$versions.sitemapgen4j",
69-
70+
jbcrypt : "org.mindrot:jbcrypt:$versions.jbcrypt",
71+
7072
junit : "junit:junit:$versions.junit",
7173
]
7274
}

‎stubbornjava-common/build.gradle‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ dependencies {
3333
compile libs.failsafe
3434
compile libs.jsoup
3535
compile libs.sitemapgen4j
36+
compile libs.jbcrypt
3637

3738
testCompile libs.junit
3839
}
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
package com.stubbornjava.common;
2+
3+
import java.util.function.Function;
4+
5+
import org.slf4j.Logger;
6+
import org.slf4j.LoggerFactory;
7+
8+
public class Hashing {
9+
private static final Logger log = LoggerFactory.getLogger(Hashing.class);
10+
11+
// {{start:bcrypt}}
12+
// This should be updated every year or two.
13+
private static final UpdatableBCrypt bcrypt = new UpdatableBCrypt(11);
14+
15+
public static String hash(String password) {
16+
return bcrypt.hash(password);
17+
}
18+
19+
public static boolean verifyAndUpdateHash(String password, String hash, Function<String, Boolean> updateFunc) {
20+
return bcrypt.verifyAndUpdateHash(password, hash, updateFunc);
21+
}
22+
// {{end:bcrypt}}
23+
24+
public static void main(String[] args) {
25+
// {{start:bcryptMain}}
26+
// Mini function to test updates.
27+
String[] mutableHash = new String[1];
28+
Function<String, Boolean> update = hash -> { mutableHash[0] = hash; return true; };
29+
30+
String hashPw1 = Hashing.hash("password");
31+
log.debug("hash of pw1: {}", hashPw1);
32+
log.debug("verifying pw1: {}", Hashing.verifyAndUpdateHash("password", hashPw1, update));
33+
log.debug("verifying pw1 fails: {}", Hashing.verifyAndUpdateHash("password1", hashPw1, update));
34+
String hashPw2 = Hashing.hash("password");
35+
log.debug("hash of pw2: {}", hashPw2);
36+
log.debug("verifying pw2: {}", Hashing.verifyAndUpdateHash("password", hashPw2, update));
37+
log.debug("verifying pw2 fails: {}", Hashing.verifyAndUpdateHash("password2", hashPw2, update));
38+
39+
UpdatableBCrypt oldHasher = new UpdatableBCrypt(7);
40+
String oldHash = oldHasher.hash("password");
41+
log.debug("hash of oldHash: {}", oldHash);
42+
log.debug("verifying oldHash: {}, hash upgraded to: {}",
43+
Hashing.verifyAndUpdateHash("password", oldHash, update),
44+
mutableHash[0]);
45+
// {{end:bcryptMain}}
46+
47+
}
48+
}
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
package com.stubbornjava.common;
2+
3+
import java.util.function.Function;
4+
5+
import org.mindrot.jbcrypt.BCrypt;
6+
import org.slf4j.Logger;
7+
import org.slf4j.LoggerFactory;
8+
9+
// {{start:UpdatableBCrypt}}
10+
public class UpdatableBCrypt {
11+
private static final Logger log = LoggerFactory.getLogger(UpdatableBCrypt.class);
12+
13+
private final int logRounds;
14+
15+
public UpdatableBCrypt(int logRounds) {
16+
this.logRounds = logRounds;
17+
}
18+
19+
public String hash(String password) {
20+
return BCrypt.hashpw(password, BCrypt.gensalt(logRounds));
21+
}
22+
23+
public boolean verifyHash(String password, String hash) {
24+
return BCrypt.checkpw(password, hash);
25+
}
26+
27+
public boolean verifyAndUpdateHash(String password, String hash, Function<String, Boolean> updateFunc) {
28+
if (BCrypt.checkpw(password, hash)) {
29+
int rounds = getRounds(hash);
30+
// It might be smart to only allow increasing the rounds.
31+
// If someone makes a mistake the ability to undo it would be nice though.
32+
if (rounds != logRounds) {
33+
log.debug("Updating password from {} rounds to {}", rounds, logRounds);
34+
String newHash = hash(password);
35+
return updateFunc.apply(newHash);
36+
}
37+
return true;
38+
}
39+
return false;
40+
}
41+
42+
/*
43+
* Copy pasted from BCrypt internals :(. Ideally a method
44+
* to exports parts would be public. We only care about rounds
45+
* currently.
46+
*/
47+
private int getRounds(String salt) {
48+
char minor = (char)0;
49+
int off = 0;
50+
51+
if (salt.charAt(0) != '$' || salt.charAt(1) != '2')
52+
throw new IllegalArgumentException ("Invalid salt version");
53+
if (salt.charAt(2) == '$')
54+
off = 3;
55+
else {
56+
minor = salt.charAt(2);
57+
if (minor != 'a' || salt.charAt(3) != '$')
58+
throw new IllegalArgumentException ("Invalid salt revision");
59+
off = 4;
60+
}
61+
62+
// Extract number of rounds
63+
if (salt.charAt(off + 2) > '$')
64+
throw new IllegalArgumentException ("Missing salt rounds");
65+
return Integer.parseInt(salt.substring(off, off + 2));
66+
}
67+
}
68+
// {{end:UpdatableBCrypt}}
69+
Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
package com.stubbornjava.common;
2+
3+
import static org.junit.Assert.assertEquals;
4+
import static org.junit.Assert.assertFalse;
5+
import static org.junit.Assert.assertTrue;
6+
7+
import org.junit.Test;
8+
9+
public class UpdatableBCryptTest {
10+
11+
@Test
12+
public void testHash() {
13+
UpdatableBCrypt bcrypt = new UpdatableBCrypt(11);
14+
String hash = bcrypt.hash("password");
15+
16+
assertTrue(bcrypt.verifyHash("password", hash));
17+
}
18+
19+
@Test
20+
public void testVerifyAndUpdateDoesNotUpdate() {
21+
UpdatableBCrypt bcrypt11 = new UpdatableBCrypt(10);
22+
String hash11 = bcrypt11.hash("password");
23+
assertTrue(bcrypt11.verifyHash("password", hash11));
24+
25+
UpdatableBCrypt bcrypt12 = new UpdatableBCrypt(10);
26+
27+
String[] hash12 = new String[1];
28+
bcrypt12.verifyAndUpdateHash("password", hash11, newHash -> {
29+
hash12[0] = newHash;
30+
return true;
31+
});
32+
assertTrue(null == hash12[0]);
33+
}
34+
35+
@Test
36+
public void testVerifyAndUpdateChangesHash() {
37+
UpdatableBCrypt bcrypt11 = new UpdatableBCrypt(10);
38+
String hash11 = bcrypt11.hash("password");
39+
assertTrue(bcrypt11.verifyHash("password", hash11));
40+
41+
UpdatableBCrypt bcrypt12 = new UpdatableBCrypt(11);
42+
43+
String[] hash12 = new String[1];
44+
boolean result = bcrypt12.verifyAndUpdateHash("password", hash11, newHash -> {
45+
hash12[0] = newHash;
46+
return true;
47+
});
48+
assertTrue(result);
49+
assertTrue(null != hash12[0]);
50+
assertTrue(bcrypt12.verifyHash("password", hash12[0]));
51+
52+
// Hash shouldn't change again
53+
String finalHash = hash12[0];
54+
result = bcrypt12.verifyAndUpdateHash("password", finalHash, newHash -> {
55+
hash12[0] = newHash;
56+
return true;
57+
});
58+
assertTrue(result);
59+
assertTrue(null != finalHash);
60+
assertEquals(finalHash, hash12[0]);
61+
assertTrue(bcrypt12.verifyHash("password", finalHash));
62+
}
63+
64+
@Test
65+
public void testVerifyAndUpdateChangesHashSkippingRounds() {
66+
UpdatableBCrypt bcrypt11 = new UpdatableBCrypt(5);
67+
String hash11 = bcrypt11.hash("password");
68+
assertTrue(bcrypt11.verifyHash("password", hash11));
69+
70+
UpdatableBCrypt bcrypt12 = new UpdatableBCrypt(13);
71+
72+
String[] hash12 = new String[1];
73+
boolean result = bcrypt12.verifyAndUpdateHash("password", hash11, newHash -> {
74+
hash12[0] = newHash;
75+
return true;
76+
});
77+
assertTrue(result);
78+
assertTrue(null != hash12[0]);
79+
assertTrue(bcrypt12.verifyHash("password", hash12[0]));
80+
}
81+
82+
@Test
83+
public void testVerifyAndUpdateFailsOnBadPW() {
84+
UpdatableBCrypt bcrypt11 = new UpdatableBCrypt(10);
85+
String hash11 = bcrypt11.hash("password");
86+
assertTrue(bcrypt11.verifyHash("password", hash11));
87+
88+
UpdatableBCrypt bcrypt12 = new UpdatableBCrypt(11);
89+
90+
String[] hash12 = new String[1];
91+
boolean result = bcrypt12.verifyAndUpdateHash("password1", hash11, newHash -> {
92+
hash12[0] = newHash;
93+
return true;
94+
});
95+
assertFalse(result);
96+
assertTrue(null == hash12[0]);
97+
}
98+
}

0 commit comments

Comments
 (0)