Skip to content

Commit fe03385

Browse files
committed
Compatibility with $2b$ hashes, fixes #30
1 parent be83807 commit fe03385

7 files changed

Lines changed: 25 additions & 14 deletions

File tree

bower.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"name": "bcryptjs",
33
"description": "Optimized bcrypt in plain JavaScript with zero dependencies.",
4-
"version": "2.2.2",
4+
"version": "2.3.0",
55
"main": "dist/bcrypt-isaac.js",
66
"license": "New-BSD",
77
"homepage": "http://dcode.io/",

dist/bcrypt.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1131,10 +1131,10 @@
11311131
}
11321132
if (salt.charAt(2) === '$')
11331133
minor = String.fromCharCode(0),
1134-
offset = 3;
1134+
offset = 3;
11351135
else {
11361136
minor = salt.charAt(2);
1137-
if ((minor !== 'a' && minor !== 'y') || salt.charAt(3) !== '$') {
1137+
if ((minor !== 'a' && minor !== 'b' && minor !== 'y') || salt.charAt(3) !== '$') {
11381138
err = Error("Invalid salt revision: "+salt.substring(2,4));
11391139
if (callback) {
11401140
nextTick(callback.bind(this, err));

dist/bcrypt.min.js

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/bcrypt.min.map

Lines changed: 1 addition & 1 deletion
Large diffs are not rendered by default.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"name": "bcryptjs",
33
"description": "Optimized bcrypt in plain JavaScript with zero dependencies. Compatible to 'bcrypt'.",
4-
"version": "2.2.2",
4+
"version": "2.3.0",
55
"author": "Daniel Wirtz <dcode@dcode.io>",
66
"contributors": [
77
"Shane Girish <shaneGirish@gmail.com> (https://github.com/shaneGirish)",

src/bcrypt/impl.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -497,10 +497,10 @@ function _hash(s, salt, callback, progressCallback) {
497497
}
498498
if (salt.charAt(2) === '$')
499499
minor = String.fromCharCode(0),
500-
offset = 3;
500+
offset = 3;
501501
else {
502502
minor = salt.charAt(2);
503-
if ((minor !== 'a' && minor !== 'y') || salt.charAt(3) !== '$') {
503+
if ((minor !== 'a' && minor !== 'b' && minor !== 'y') || salt.charAt(3) !== '$') {
504504
err = Error("Invalid salt revision: "+salt.substring(2,4));
505505
if (callback) {
506506
nextTick(callback.bind(this, err));

tests/suite.js

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -47,13 +47,26 @@ module.exports = {
4747
"compareSync": function(test) {
4848
var salt1 = bcrypt.genSaltSync(),
4949
hash1 = bcrypt.hashSync("hello", salt1); // $2a$
50-
var salt2 = bcrypt.genSaltSync();
51-
salt2 = salt2.substring(0,2)+'y'+salt2.substring(3); // $2y$
52-
var hash2 = bcrypt.hashSync("world", salt2);
50+
var salt2 = bcrypt.genSaltSync().replace(/\$2a\$/, "$2y$"),
51+
hash2 = bcrypt.hashSync("world", salt2);
52+
var salt3 = bcrypt.genSaltSync().replace(/\$2a\$/, "$2b$"),
53+
hash3 = bcrypt.hashSync("hello world", salt3);
54+
55+
test.strictEqual(hash1.substring(0,4), "$2a$");
5356
test.ok(bcrypt.compareSync("hello", hash1));
5457
test.notOk(bcrypt.compareSync("hello", hash2));
58+
test.notOk(bcrypt.compareSync("hello", hash3));
59+
60+
test.strictEqual(hash2.substring(0,4), "$2y$");
5561
test.ok(bcrypt.compareSync("world", hash2));
5662
test.notOk(bcrypt.compareSync("world", hash1));
63+
test.notOk(bcrypt.compareSync("world", hash3));
64+
65+
test.strictEqual(hash3.substring(0,4), "$2b$");
66+
test.ok(bcrypt.compareSync("hello world", hash3));
67+
test.notOk(bcrypt.compareSync("hello world", hash1));
68+
test.notOk(bcrypt.compareSync("hello world", hash2));
69+
5770
test.done();
5871
},
5972

@@ -84,9 +97,7 @@ module.exports = {
8497

8598
"getSalt": function(test) {
8699
var hash1 = bcrypt.hashSync("hello", bcrypt.genSaltSync());
87-
test.log("Hash: "+hash1);
88100
var salt = bcrypt.getSalt(hash1);
89-
test.log("Salt: "+salt);
90101
var hash2 = bcrypt.hashSync("hello", salt);
91102
test.equal(hash1, hash2);
92103
test.done();

0 commit comments

Comments
 (0)