Skip to content

Commit 5be2eb6

Browse files
committed
Post-merge; Fixed genSalt not defaulting to default rounds with omitted callback (promise), see #58
1 parent bfba82c commit 5be2eb6

File tree

10 files changed

+178
-60
lines changed

10 files changed

+178
-60
lines changed

.vscode/settings.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"vsicons.presets.angular": false
3+
}

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.4.0",
4+
"version": "2.4.1",
55
"main": "dist/bcrypt.min.js",
66
"license": "New-BSD",
77
"homepage": "http://dcode.io/",

debug.log

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
[0207/082007:ERROR:tcp_listen_socket.cc(76)] Could not bind socket to 127.0.0.1:6004
2+
[0207/082007:ERROR:node_debugger.cc(86)] Cannot start debugger server
3+
[0207/082011:ERROR:tcp_listen_socket.cc(76)] Could not bind socket to 127.0.0.1:6004
4+
[0207/082011:ERROR:node_debugger.cc(86)] Cannot start debugger server

dist/bcrypt.js

Lines changed: 112 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -141,14 +141,14 @@
141141
seed_length = undefined; // Not supported.
142142
if (typeof rounds === 'function')
143143
callback = rounds,
144+
rounds = undefined;
145+
if (typeof rounds === 'undefined')
144146
rounds = GENSALT_DEFAULT_LOG2_ROUNDS;
147+
else if (typeof rounds !== 'number')
148+
throw Error("illegal arguments: "+(typeof rounds));
145149

146150
function _async(callback) {
147151
nextTick(function() { // Pretty thin, but salting is fast enough
148-
if (typeof rounds !== 'number') {
149-
callback(Error("Illegal arguments: "+(typeof rounds)));
150-
return;
151-
}
152152
try {
153153
callback(null, bcrypt.genSaltSync(rounds));
154154
} catch (err) {
@@ -962,19 +962,113 @@
962962
r = lr[off + 1];
963963

964964
l ^= P[0];
965+
966+
/*
965967
for (var i=0, k=BLOWFISH_NUM_ROUNDS-2; i<=k;)
966968
// Feistel substitution on left word
967-
n = S[(l >> 24) & 0xff],
969+
n = S[l >>> 24],
968970
n += S[0x100 | ((l >> 16) & 0xff)],
969971
n ^= S[0x200 | ((l >> 8) & 0xff)],
970972
n += S[0x300 | (l & 0xff)],
971973
r ^= n ^ P[++i],
972974
// Feistel substitution on right word
973-
n = S[(r >> 24) & 0xff],
975+
n = S[r >>> 24],
974976
n += S[0x100 | ((r >> 16) & 0xff)],
975977
n ^= S[0x200 | ((r >> 8) & 0xff)],
976978
n += S[0x300 | (r & 0xff)],
977979
l ^= n ^ P[++i];
980+
*/
981+
982+
//The following is an unrolled version of the above loop.
983+
//Iteration 0
984+
n = S[l >>> 24];
985+
n += S[0x100 | ((l >> 16) & 0xff)];
986+
n ^= S[0x200 | ((l >> 8) & 0xff)];
987+
n += S[0x300 | (l & 0xff)];
988+
r ^= n ^ P[1];
989+
n = S[r >>> 24];
990+
n += S[0x100 | ((r >> 16) & 0xff)];
991+
n ^= S[0x200 | ((r >> 8) & 0xff)];
992+
n += S[0x300 | (r & 0xff)];
993+
l ^= n ^ P[2];
994+
//Iteration 1
995+
n = S[l >>> 24];
996+
n += S[0x100 | ((l >> 16) & 0xff)];
997+
n ^= S[0x200 | ((l >> 8) & 0xff)];
998+
n += S[0x300 | (l & 0xff)];
999+
r ^= n ^ P[3];
1000+
n = S[r >>> 24];
1001+
n += S[0x100 | ((r >> 16) & 0xff)];
1002+
n ^= S[0x200 | ((r >> 8) & 0xff)];
1003+
n += S[0x300 | (r & 0xff)];
1004+
l ^= n ^ P[4];
1005+
//Iteration 2
1006+
n = S[l >>> 24];
1007+
n += S[0x100 | ((l >> 16) & 0xff)];
1008+
n ^= S[0x200 | ((l >> 8) & 0xff)];
1009+
n += S[0x300 | (l & 0xff)];
1010+
r ^= n ^ P[5];
1011+
n = S[r >>> 24];
1012+
n += S[0x100 | ((r >> 16) & 0xff)];
1013+
n ^= S[0x200 | ((r >> 8) & 0xff)];
1014+
n += S[0x300 | (r & 0xff)];
1015+
l ^= n ^ P[6];
1016+
//Iteration 3
1017+
n = S[l >>> 24];
1018+
n += S[0x100 | ((l >> 16) & 0xff)];
1019+
n ^= S[0x200 | ((l >> 8) & 0xff)];
1020+
n += S[0x300 | (l & 0xff)];
1021+
r ^= n ^ P[7];
1022+
n = S[r >>> 24];
1023+
n += S[0x100 | ((r >> 16) & 0xff)];
1024+
n ^= S[0x200 | ((r >> 8) & 0xff)];
1025+
n += S[0x300 | (r & 0xff)];
1026+
l ^= n ^ P[8];
1027+
//Iteration 4
1028+
n = S[l >>> 24];
1029+
n += S[0x100 | ((l >> 16) & 0xff)];
1030+
n ^= S[0x200 | ((l >> 8) & 0xff)];
1031+
n += S[0x300 | (l & 0xff)];
1032+
r ^= n ^ P[9];
1033+
n = S[r >>> 24];
1034+
n += S[0x100 | ((r >> 16) & 0xff)];
1035+
n ^= S[0x200 | ((r >> 8) & 0xff)];
1036+
n += S[0x300 | (r & 0xff)];
1037+
l ^= n ^ P[10];
1038+
//Iteration 5
1039+
n = S[l >>> 24];
1040+
n += S[0x100 | ((l >> 16) & 0xff)];
1041+
n ^= S[0x200 | ((l >> 8) & 0xff)];
1042+
n += S[0x300 | (l & 0xff)];
1043+
r ^= n ^ P[11];
1044+
n = S[r >>> 24];
1045+
n += S[0x100 | ((r >> 16) & 0xff)];
1046+
n ^= S[0x200 | ((r >> 8) & 0xff)];
1047+
n += S[0x300 | (r & 0xff)];
1048+
l ^= n ^ P[12];
1049+
//Iteration 6
1050+
n = S[l >>> 24];
1051+
n += S[0x100 | ((l >> 16) & 0xff)];
1052+
n ^= S[0x200 | ((l >> 8) & 0xff)];
1053+
n += S[0x300 | (l & 0xff)];
1054+
r ^= n ^ P[13];
1055+
n = S[r >>> 24];
1056+
n += S[0x100 | ((r >> 16) & 0xff)];
1057+
n ^= S[0x200 | ((r >> 8) & 0xff)];
1058+
n += S[0x300 | (r & 0xff)];
1059+
l ^= n ^ P[14];
1060+
//Iteration 7
1061+
n = S[l >>> 24];
1062+
n += S[0x100 | ((l >> 16) & 0xff)];
1063+
n ^= S[0x200 | ((l >> 8) & 0xff)];
1064+
n += S[0x300 | (l & 0xff)];
1065+
r ^= n ^ P[15];
1066+
n = S[r >>> 24];
1067+
n += S[0x100 | ((r >> 16) & 0xff)];
1068+
n ^= S[0x200 | ((r >> 8) & 0xff)];
1069+
n += S[0x300 | (r & 0xff)];
1070+
l ^= n ^ P[16];
1071+
9781072
lr[off] = r ^ P[BLOWFISH_NUM_ROUNDS + 1];
9791073
lr[off + 1] = l;
9801074
return lr;
@@ -1094,9 +1188,18 @@
10941188
throw err;
10951189
}
10961190
rounds = (1 << rounds) >>> 0;
1097-
var P = P_ORIG.slice(),
1098-
S = S_ORIG.slice(),
1099-
i = 0, j;
1191+
1192+
var P, S, i = 0, j;
1193+
1194+
//Use typed arrays when available - huge speedup!
1195+
if (Int32Array) {
1196+
P = new Int32Array(P_ORIG);
1197+
S = new Int32Array(S_ORIG);
1198+
} else {
1199+
P = P_ORIG.slice();
1200+
S = S_ORIG.slice();
1201+
}
1202+
11001203
_ekskey(salt, b, P, S);
11011204

11021205
/**

0 commit comments

Comments
 (0)