Skip to content

Commit d36bfb4

Browse files
committedFeb 11, 2025·
Generate 2b hashes by default
BREAKING CHANGE: This library was not affected by the bug that led to incrementing the bcrypt version from 2a to 2b, but nowadays most implementations use 2b, including the native bcrypt binding, so this change aligns with them. Existing hashes will continue to work, but test logic that generates hashes and compares them literally might need to be updated to account for the new default.
1 parent c8c9c01 commit d36bfb4

File tree

2 files changed

+22
-22
lines changed

2 files changed

+22
-22
lines changed
 

‎index.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ export function genSaltSync(rounds, seed_length) {
9191
else if (rounds > 31)
9292
rounds = 31;
9393
var salt = [];
94-
salt.push("$2a$");
94+
salt.push("$2b$");
9595
if (rounds < 10)
9696
salt.push("0");
9797
salt.push(rounds.toString());
@@ -228,7 +228,7 @@ export function compareSync(s, hash) {
228228
throw Error("Illegal arguments: "+(typeof s)+', '+(typeof hash));
229229
if (hash.length !== 60)
230230
return false;
231-
return safeStringCompare(hashSync(s, hash.substr(0, hash.length-31)), hash);
231+
return safeStringCompare(hashSync(s, hash.substring(0, hash.length-31)), hash);
232232
}
233233

234234
/**

‎tests/index.js

+20-20
Original file line numberDiff line numberDiff line change
@@ -51,13 +51,13 @@ const tests = [
5151
},
5252
function compareSync(done) {
5353
var salt1 = bcrypt.genSaltSync(),
54-
hash1 = bcrypt.hashSync("hello", salt1); // $2a$
55-
var salt2 = bcrypt.genSaltSync().replace(/\$2a\$/, "$2y$"),
54+
hash1 = bcrypt.hashSync("hello", salt1); // $2b$
55+
var salt2 = bcrypt.genSaltSync().replace(/\$2b\$/, "$2y$"),
5656
hash2 = bcrypt.hashSync("world", salt2);
57-
var salt3 = bcrypt.genSaltSync().replace(/\$2a\$/, "$2b$"),
57+
var salt3 = bcrypt.genSaltSync().replace(/\$2b\$/, "$2a$"),
5858
hash3 = bcrypt.hashSync("hello world", salt3);
5959

60-
assert.strictEqual(hash1.substring(0,4), "$2a$");
60+
assert.strictEqual(hash1.substring(0,4), "$2b$");
6161
assert(bcrypt.compareSync("hello", hash1));
6262
assert(!bcrypt.compareSync("hello", hash2));
6363
assert(!bcrypt.compareSync("hello", hash3));
@@ -67,7 +67,7 @@ const tests = [
6767
assert(!bcrypt.compareSync("world", hash1));
6868
assert(!bcrypt.compareSync("world", hash3));
6969

70-
assert.strictEqual(hash3.substring(0,4), "$2b$");
70+
assert.strictEqual(hash3.substring(0,4), "$2a$");
7171
assert(bcrypt.compareSync("hello world", hash3));
7272
assert(!bcrypt.compareSync("hello world", hash1));
7373
assert(!bcrypt.compareSync("hello world", hash2));
@@ -160,24 +160,24 @@ const tests = [
160160
assert.equal(hash1, hash2);
161161
done();
162162
},
163-
// function compat_roundsOOB(done) {
164-
// var salt1 = bcrypt.genSaltSync(0), // $10$ like not set
165-
// salt2 = binding.genSaltSync(0);
166-
// assert.strictEqual(salt1.substring(0, 7), "$2a$10$");
167-
// assert.strictEqual(salt2.substring(0, 7), "$2a$10$");
163+
function compat_roundsOOB(done) {
164+
var salt1 = bcrypt.genSaltSync(0), // $10$ like not set
165+
salt2 = binding.genSaltSync(0);
166+
assert.strictEqual(salt1.substring(0, 7), "$2b$10$");
167+
assert.strictEqual(salt2.substring(0, 7), "$2b$10$");
168168

169-
// salt1 = bcrypt.genSaltSync(3); // $04$ is lower cap
170-
// salt2 = bcrypt.genSaltSync(3);
171-
// assert.strictEqual(salt1.substring(0, 7), "$2a$04$");
172-
// assert.strictEqual(salt2.substring(0, 7), "$2a$04$");
169+
salt1 = bcrypt.genSaltSync(3); // $04$ is lower cap
170+
salt2 = bcrypt.genSaltSync(3);
171+
assert.strictEqual(salt1.substring(0, 7), "$2b$04$");
172+
assert.strictEqual(salt2.substring(0, 7), "$2b$04$");
173173

174-
// salt1 = bcrypt.genSaltSync(32); // $31$ is upper cap
175-
// salt2 = bcrypt.genSaltSync(32);
176-
// assert.strictEqual(salt1.substring(0, 7), "$2a$31$");
177-
// assert.strictEqual(salt2.substring(0, 7), "$2a$31$");
174+
salt1 = bcrypt.genSaltSync(32); // $31$ is upper cap
175+
salt2 = bcrypt.genSaltSync(32);
176+
assert.strictEqual(salt1.substring(0, 7), "$2b$31$");
177+
assert.strictEqual(salt2.substring(0, 7), "$2b$31$");
178178

179-
// done();
180-
// }
179+
done();
180+
}
181181
]
182182

183183
function next() {

0 commit comments

Comments
 (0)
Please sign in to comment.