Skip to content

Commit d04b48d

Browse files
committed
Add post on bcrypt
1 parent 8a3832b commit d04b48d

3 files changed

Lines changed: 66 additions & 0 deletions

File tree

‎stubbornjava-webapp/src/main/java/com/stubbornjava/webapp/post/JavaLib.java‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,12 @@ public enum JavaLib {
112112
"https://github.com/dfabulich/sitemapgen4j",
113113
"SitemapGen4j is a library to generate XML sitemaps in Java."
114114
)
115+
, jBCrypt(
116+
"jBCrypt",
117+
"http://www.mindrot.org/projects/jBCrypt/",
118+
null,
119+
"jBCrypt is a Java™ implementation of OpenBSD's Blowfish password hashing code, as described in \"A Future-Adaptable Password Scheme\" by Niels Provos and David Mazières."
120+
)
115121
;
116122

117123
private final String name;

‎stubbornjava-webapp/src/main/java/com/stubbornjava/webapp/post/PostData.java‎

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -577,6 +577,24 @@ public class PostData {
577577
))
578578
.build()
579579
);
580+
posts.add(PostRaw.builder()
581+
.postId(892208155060669739L)
582+
.title("Hashing Passwords in Java with BCrypt")
583+
.metaDesc("Java BCrypt example using jbcrypt with auto updating salted hashes. Secure password salted hashing in Java with BCrypt.")
584+
.dateCreated(LocalDateTime.parse("2017-08-01T01:15:30"))
585+
.dateUpdated(LocalDateTime.parse("2017-08-01T01:15:30"))
586+
.javaLibs(Lists.newArrayList(JavaLib.jBCrypt, JavaLib.Logback))
587+
.tags(Lists.newArrayList())
588+
.gitFileReferences(Lists.newArrayList(
589+
FileReference.stubbornJava(
590+
"hashing",
591+
"stubbornjava-common/src/main/java/com/stubbornjava/common/Hashing.java")
592+
, FileReference.stubbornJava(
593+
"bcrypt",
594+
"stubbornjava-common/src/main/java/com/stubbornjava/common/UpdatableBCrypt.java")
595+
))
596+
.build()
597+
);
580598
}
581599

582600
public static List<PostRaw> getPosts() {
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
{{#assign "s1"}}
2+
BCrypt is a one way salted hash function based on the [Blowfish cipher](https://en.wikipedia.org/wiki/Blowfish_(cipher)). It provides several enhancements over plain text passwords (unfortunately this still happens quite often) and traditional hashing algorithms (md5). It wouldn't be accurate to say BCrypt is the best way to store passwords but it should be good enough. Algorithms such as [PBKDF2](https://en.wikipedia.org/wiki/PBKDF2) could be used as a more thoroughly tested algorithm but BCrypt is commonly used as well. `jBCrypt` is a Java implementation of BCrypt.
3+
4+
## BCrypt features
5+
* **Not plain text** - Not only do plain text passwords compromise your website if the database is breached but they can also compromise other websites for the users. Unfortunately a lot of users share passwords across websites.
6+
* **One way hashing** - BCrypt is a one way hash function to obfuscate the password such that it is not stored in plain text.
7+
* **Salted hashing** - Generating random bytes (the salt) and combining it with the password before hashing creates unique hashes across each users password. If two users have the same password they will not have the same password hash. This is to prevent [rainbow table](https://en.wikipedia.org/wiki/Rainbow_table) attacks which can reverse hashed passwords using common hashing functions that do not utilize a salt.
8+
* **Logarithmic iterations** - The hashing function is executed many times sequentially which can be increased exponentially known as [key stretching](https://en.wikipedia.org/wiki/Key_stretching). This is to make the function CPU intensive which makes it more secure against brute force attacks.
9+
* **Updatable iterations** - As CPUs become faster so do brute force attacks. Since BCrypt stores the number of iterations as part of the hash its possible to verify a password and then increase its strength by generating a new hash with a higher number of iterations.
10+
11+
## jBCrypt Example Wrapper.
12+
We will be wrapping the standard `jBCrypt` with our own methods to allow auto updating the iterations on the fly.
13+
{{/assign}}
14+
{{md s1}}
15+
{{> templates/src/widgets/code/code-snippet file=bcrypt section=bcrypt.sections.UpdatableBCrypt}}
16+
17+
{{#assign "s2"}}
18+
## Static Hashing Utility Class
19+
Let's make a static utility class to handle everything for us. Ideally we will periodically update the number of iterations in this class based on the recommended intervals.
20+
{{/assign}}
21+
{{md s2}}
22+
{{> templates/src/widgets/code/code-snippet file=hashing section=hashing.sections.bcrypt}}
23+
24+
{{#assign "s3"}}
25+
## BCrypt Java Example with Updatable Iterations
26+
Putting it all together.
27+
{{/assign}}
28+
{{md s3}}
29+
{{> templates/src/widgets/code/code-snippet file=hashing section=hashing.sections.bcryptMain}}
30+
{{#assign "s4"}}
31+
Notice how the first and second password are the same but the hashes are different. We are also able to upgrade an older version of a BCrypt password using a low number of iterations to a higher number on the fly. In the real world the passed in `Function` would be updating the database.
32+
{{/assign}}
33+
{{md s4}}
34+
<pre class="line-numbers"><code class="language-bash">2017-08-02 01:07:38.757 [main] DEBUG com.stubbornjava.common.Hashing - hash of pw1: $2a$11$MXOOO1JYngri2arcL6Cic.KuBujhqgz.B2ri6szqN2/cfsdiQa7se
35+
2017-08-02 01:07:38.936 [main] DEBUG com.stubbornjava.common.Hashing - verifying pw1: true
36+
2017-08-02 01:07:39.108 [main] DEBUG com.stubbornjava.common.Hashing - verifying pw1 fails: false
37+
2017-08-02 01:07:39.284 [main] DEBUG com.stubbornjava.common.Hashing - hash of pw2: $2a$11$TckEYxY/0DPf6OfpQhXKP./hl45UlgXYs0jQFsZZCwBEjUCo7bUKy
38+
2017-08-02 01:07:39.466 [main] DEBUG com.stubbornjava.common.Hashing - verifying pw2: true
39+
2017-08-02 01:07:39.647 [main] DEBUG com.stubbornjava.common.Hashing - verifying pw2 fails: false
40+
2017-08-02 01:07:39.660 [main] DEBUG com.stubbornjava.common.Hashing - hash of oldHash: $2a$07$LMVTcMYGZZ6ORi4oPUGPAe3v8Kqpl.UCzKO2s8yfFN6c9vfM4szKW
41+
2017-08-02 01:07:39.671 [main] DEBUG c.s.common.UpdatableBCrypt - Updating password from 7 rounds to 11
42+
2017-08-02 01:07:39.848 [main] DEBUG com.stubbornjava.common.Hashing - verifying oldHash: true, hash upgraded to: $2a$11$1MvimUN3YO4G9DGY8G7rqOyPIjyye7jMYdIu8/BXL7t.e9CECj5Oa</code></pre>

0 commit comments

Comments
 (0)