Skip to content

Commit b09f7f2

Browse files
committedMar 5, 2018
Post-merge; Clean up a bit
1 parent 648482a commit b09f7f2

File tree

8 files changed

+176
-305
lines changed

8 files changed

+176
-305
lines changed
 

Diff for: ‎dist/bcrypt.js

+59-229
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,6 @@
106106
* @param {number=} seed_length Not supported.
107107
* @returns {string} Resulting salt
108108
* @throws {Error} If a random fallback is required but not set
109-
* @expose
110109
*/
111110
bcrypt.genSaltSync = function(rounds, seed_length) {
112111
rounds = rounds || GENSALT_DEFAULT_LOG2_ROUNDS;
@@ -133,7 +132,6 @@
133132
* @param {function(Error, string=)=} callback Callback receiving the error, if any, and the resulting salt
134133
* @returns {!Promise} If `callback` has been omitted
135134
* @throws {Error} If `callback` is present but not a function
136-
* @expose
137135
*/
138136
bcrypt.genSalt = function(rounds, seed_length, callback) {
139137
if (typeof seed_length === 'function')
@@ -178,7 +176,6 @@
178176
* @param {string} s String to hash
179177
* @param {(number|string)=} salt Salt length to generate or salt to use, default to 10
180178
* @returns {string} Resulting hash
181-
* @expose
182179
*/
183180
bcrypt.hashSync = function(s, salt) {
184181
if (typeof salt === 'undefined')
@@ -199,7 +196,6 @@
199196
* (0.0 - 1.0), maximally once per `MAX_EXECUTION_TIME = 100` ms.
200197
* @returns {!Promise} If `callback` has been omitted
201198
* @throws {Error} If `callback` is present but not a function
202-
* @expose
203199
*/
204200
bcrypt.hash = function(s, salt, callback, progressCallback) {
205201

@@ -238,18 +234,11 @@
238234
* @inner
239235
*/
240236
function safeStringCompare(known, unknown) {
241-
var right = 0,
242-
wrong = 0;
243-
for (var i=0, k=known.length; i<k; ++i) {
244-
if (known.charCodeAt(i) === unknown.charCodeAt(i))
245-
++right;
246-
else
247-
++wrong;
237+
var diff = known.length ^ unknown.length;
238+
for (var i = 0; i < known.length; ++i) {
239+
diff |= known.charCodeAt(i) ^ unknown.charCodeAt(i);
248240
}
249-
// Prevent removal of unused variables (never true, actually)
250-
if (right < 0)
251-
return false;
252-
return wrong === 0;
241+
return diff === 0;
253242
}
254243

255244
/**
@@ -258,7 +247,6 @@
258247
* @param {string} hash Hash to test against
259248
* @returns {boolean} true if matching, otherwise false
260249
* @throws {Error} If an argument is illegal
261-
* @expose
262250
*/
263251
bcrypt.compareSync = function(s, hash) {
264252
if (typeof s !== "string" || typeof hash !== "string")
@@ -277,7 +265,6 @@
277265
* (0.0 - 1.0), maximally once per `MAX_EXECUTION_TIME = 100` ms.
278266
* @returns {!Promise} If `callback` has been omitted
279267
* @throws {Error} If `callback` is present but not a function
280-
* @expose
281268
*/
282269
bcrypt.compare = function(s, hash, callback, progressCallback) {
283270

@@ -319,7 +306,6 @@
319306
* @param {string} hash Hash to extract the used number of rounds from
320307
* @returns {number} Number of rounds used
321308
* @throws {Error} If `hash` is not a string
322-
* @expose
323309
*/
324310
bcrypt.getRounds = function(hash) {
325311
if (typeof hash !== "string")
@@ -332,7 +318,6 @@
332318
* @param {string} hash Hash to extract the salt from
333319
* @returns {string} Extracted salt part
334320
* @throws {Error} If `hash` is not a string or otherwise invalid
335-
* @expose
336321
*/
337322
bcrypt.getSalt = function(hash) {
338323
if (typeof hash !== 'string')
@@ -352,23 +337,67 @@
352337
? (typeof setImmediate === 'function' ? setImmediate : process.nextTick)
353338
: setTimeout;
354339

340+
/** Calculates the byte length of a string encoded as UTF8. */
341+
function utf8Length(string) {
342+
var len = 0,
343+
c = 0;
344+
for (var i = 0; i < string.length; ++i) {
345+
c = string.charCodeAt(i);
346+
if (c < 128)
347+
len += 1;
348+
else if (c < 2048)
349+
len += 2;
350+
else if (
351+
(c & 0xFC00) === 0xD800 &&
352+
(string.charCodeAt(i + 1) & 0xFC00) === 0xDC00
353+
) {
354+
++i;
355+
len += 4;
356+
} else
357+
len += 3;
358+
}
359+
return len;
360+
}
361+
362+
/** Converts a string to an array of UTF8 bytes. */
363+
function utf8Array(string) {
364+
var offset = 0,
365+
c1, c2;
366+
var buffer = new Array(utf8Length(string));
367+
for (var i = 0, k = string.length; i < k; ++i) {
368+
c1 = string.charCodeAt(i);
369+
if (c1 < 128) {
370+
buffer[offset++] = c1;
371+
} else if (c1 < 2048) {
372+
buffer[offset++] = c1 >> 6 | 192;
373+
buffer[offset++] = c1 & 63 | 128;
374+
} else if (
375+
( c1 & 0xFC00) === 0xD800 &&
376+
((c2 = string.charCodeAt(i + 1)) & 0xFC00) === 0xDC00
377+
) {
378+
c1 = 0x10000 + ((c1 & 0x03FF) << 10) + (c2 & 0x03FF);
379+
++i;
380+
buffer[offset++] = c1 >> 18 | 240;
381+
buffer[offset++] = c1 >> 12 & 63 | 128;
382+
buffer[offset++] = c1 >> 6 & 63 | 128;
383+
buffer[offset++] = c1 & 63 | 128;
384+
} else {
385+
buffer[offset++] = c1 >> 12 | 224;
386+
buffer[offset++] = c1 >> 6 & 63 | 128;
387+
buffer[offset++] = c1 & 63 | 128;
388+
}
389+
}
390+
return buffer;
391+
}
392+
355393
/**
356394
* Converts a JavaScript string to UTF8 bytes.
395+
* @function
357396
* @param {string} str String
358397
* @returns {!Array.<number>} UTF8 bytes
359398
* @inner
360399
*/
361-
function stringToBytes(str) {
362-
var out = [],
363-
i = 0;
364-
utfx.encodeUTF16toUTF8(function() {
365-
if (i >= str.length) return null;
366-
return str.charCodeAt(i++);
367-
}, function(b) {
368-
out.push(b);
369-
});
370-
return out;
371-
}
400+
var stringToBytes = utf8Array;
372401

373402
// A base64 implementation for the bcrypt algorithm. This is partly non-standard.
374403

@@ -486,203 +515,6 @@
486515
return res;
487516
}
488517

489-
/**
490-
* utfx-embeddable (c) 2014 Daniel Wirtz <dcode@dcode.io>
491-
* Released under the Apache License, Version 2.0
492-
* see: https://github.com/dcodeIO/utfx for details
493-
*/
494-
var utfx = function() {
495-
"use strict";
496-
497-
/**
498-
* utfx namespace.
499-
* @inner
500-
* @type {!Object.<string,*>}
501-
*/
502-
var utfx = {};
503-
504-
/**
505-
* Maximum valid code point.
506-
* @type {number}
507-
* @const
508-
*/
509-
utfx.MAX_CODEPOINT = 0x10FFFF;
510-
511-
/**
512-
* Encodes UTF8 code points to UTF8 bytes.
513-
* @param {(!function():number|null) | number} src Code points source, either as a function returning the next code point
514-
* respectively `null` if there are no more code points left or a single numeric code point.
515-
* @param {!function(number)} dst Bytes destination as a function successively called with the next byte
516-
*/
517-
utfx.encodeUTF8 = function(src, dst) {
518-
var cp = null;
519-
if (typeof src === 'number')
520-
cp = src,
521-
src = function() { return null; };
522-
while (cp !== null || (cp = src()) !== null) {
523-
if (cp < 0x80)
524-
dst(cp&0x7F);
525-
else if (cp < 0x800)
526-
dst(((cp>>6)&0x1F)|0xC0),
527-
dst((cp&0x3F)|0x80);
528-
else if (cp < 0x10000)
529-
dst(((cp>>12)&0x0F)|0xE0),
530-
dst(((cp>>6)&0x3F)|0x80),
531-
dst((cp&0x3F)|0x80);
532-
else
533-
dst(((cp>>18)&0x07)|0xF0),
534-
dst(((cp>>12)&0x3F)|0x80),
535-
dst(((cp>>6)&0x3F)|0x80),
536-
dst((cp&0x3F)|0x80);
537-
cp = null;
538-
}
539-
};
540-
541-
/**
542-
* Decodes UTF8 bytes to UTF8 code points.
543-
* @param {!function():number|null} src Bytes source as a function returning the next byte respectively `null` if there
544-
* are no more bytes left.
545-
* @param {!function(number)} dst Code points destination as a function successively called with each decoded code point.
546-
* @throws {RangeError} If a starting byte is invalid in UTF8
547-
* @throws {Error} If the last sequence is truncated. Has an array property `bytes` holding the
548-
* remaining bytes.
549-
*/
550-
utfx.decodeUTF8 = function(src, dst) {
551-
var a, b, c, d, fail = function(b) {
552-
b = b.slice(0, b.indexOf(null));
553-
var err = Error(b.toString());
554-
err.name = "TruncatedError";
555-
err['bytes'] = b;
556-
throw err;
557-
};
558-
while ((a = src()) !== null) {
559-
if ((a&0x80) === 0)
560-
dst(a);
561-
else if ((a&0xE0) === 0xC0)
562-
((b = src()) === null) && fail([a, b]),
563-
dst(((a&0x1F)<<6) | (b&0x3F));
564-
else if ((a&0xF0) === 0xE0)
565-
((b=src()) === null || (c=src()) === null) && fail([a, b, c]),
566-
dst(((a&0x0F)<<12) | ((b&0x3F)<<6) | (c&0x3F));
567-
else if ((a&0xF8) === 0xF0)
568-
((b=src()) === null || (c=src()) === null || (d=src()) === null) && fail([a, b, c ,d]),
569-
dst(((a&0x07)<<18) | ((b&0x3F)<<12) | ((c&0x3F)<<6) | (d&0x3F));
570-
else throw RangeError("Illegal starting byte: "+a);
571-
}
572-
};
573-
574-
/**
575-
* Converts UTF16 characters to UTF8 code points.
576-
* @param {!function():number|null} src Characters source as a function returning the next char code respectively
577-
* `null` if there are no more characters left.
578-
* @param {!function(number)} dst Code points destination as a function successively called with each converted code
579-
* point.
580-
*/
581-
utfx.UTF16toUTF8 = function(src, dst) {
582-
var c1, c2 = null;
583-
while (true) {
584-
if ((c1 = c2 !== null ? c2 : src()) === null)
585-
break;
586-
if (c1 >= 0xD800 && c1 <= 0xDFFF) {
587-
if ((c2 = src()) !== null) {
588-
if (c2 >= 0xDC00 && c2 <= 0xDFFF) {
589-
dst((c1-0xD800)*0x400+c2-0xDC00+0x10000);
590-
c2 = null; continue;
591-
}
592-
}
593-
}
594-
dst(c1);
595-
}
596-
if (c2 !== null) dst(c2);
597-
};
598-
599-
/**
600-
* Converts UTF8 code points to UTF16 characters.
601-
* @param {(!function():number|null) | number} src Code points source, either as a function returning the next code point
602-
* respectively `null` if there are no more code points left or a single numeric code point.
603-
* @param {!function(number)} dst Characters destination as a function successively called with each converted char code.
604-
* @throws {RangeError} If a code point is out of range
605-
*/
606-
utfx.UTF8toUTF16 = function(src, dst) {
607-
var cp = null;
608-
if (typeof src === 'number')
609-
cp = src, src = function() { return null; };
610-
while (cp !== null || (cp = src()) !== null) {
611-
if (cp <= 0xFFFF)
612-
dst(cp);
613-
else
614-
cp -= 0x10000,
615-
dst((cp>>10)+0xD800),
616-
dst((cp%0x400)+0xDC00);
617-
cp = null;
618-
}
619-
};
620-
621-
/**
622-
* Converts and encodes UTF16 characters to UTF8 bytes.
623-
* @param {!function():number|null} src Characters source as a function returning the next char code respectively `null`
624-
* if there are no more characters left.
625-
* @param {!function(number)} dst Bytes destination as a function successively called with the next byte.
626-
*/
627-
utfx.encodeUTF16toUTF8 = function(src, dst) {
628-
utfx.UTF16toUTF8(src, function(cp) {
629-
utfx.encodeUTF8(cp, dst);
630-
});
631-
};
632-
633-
/**
634-
* Decodes and converts UTF8 bytes to UTF16 characters.
635-
* @param {!function():number|null} src Bytes source as a function returning the next byte respectively `null` if there
636-
* are no more bytes left.
637-
* @param {!function(number)} dst Characters destination as a function successively called with each converted char code.
638-
* @throws {RangeError} If a starting byte is invalid in UTF8
639-
* @throws {Error} If the last sequence is truncated. Has an array property `bytes` holding the remaining bytes.
640-
*/
641-
utfx.decodeUTF8toUTF16 = function(src, dst) {
642-
utfx.decodeUTF8(src, function(cp) {
643-
utfx.UTF8toUTF16(cp, dst);
644-
});
645-
};
646-
647-
/**
648-
* Calculates the byte length of an UTF8 code point.
649-
* @param {number} cp UTF8 code point
650-
* @returns {number} Byte length
651-
*/
652-
utfx.calculateCodePoint = function(cp) {
653-
return (cp < 0x80) ? 1 : (cp < 0x800) ? 2 : (cp < 0x10000) ? 3 : 4;
654-
};
655-
656-
/**
657-
* Calculates the number of UTF8 bytes required to store UTF8 code points.
658-
* @param {(!function():number|null)} src Code points source as a function returning the next code point respectively
659-
* `null` if there are no more code points left.
660-
* @returns {number} The number of UTF8 bytes required
661-
*/
662-
utfx.calculateUTF8 = function(src) {
663-
var cp, l=0;
664-
while ((cp = src()) !== null)
665-
l += utfx.calculateCodePoint(cp);
666-
return l;
667-
};
668-
669-
/**
670-
* Calculates the number of UTF8 code points respectively UTF8 bytes required to store UTF16 char codes.
671-
* @param {(!function():number|null)} src Characters source as a function returning the next char code respectively
672-
* `null` if there are no more characters left.
673-
* @returns {!Array.<number>} The number of UTF8 code points at index 0 and the number of UTF8 bytes required at index 1.
674-
*/
675-
utfx.calculateUTF16asUTF8 = function(src) {
676-
var n=0, l=0;
677-
utfx.UTF16toUTF8(src, function(cp) {
678-
++n; l += utfx.calculateCodePoint(cp);
679-
});
680-
return [n,l];
681-
};
682-
683-
return utfx;
684-
}();
685-
686518
Date.now = Date.now || function() { return +new Date; };
687519

688520
/**
@@ -1361,7 +1193,6 @@
13611193
* @param {!Array.<number>} b Byte array
13621194
* @param {number} len Maximum input length
13631195
* @returns {string}
1364-
* @expose
13651196
*/
13661197
bcrypt.encodeBase64 = base64_encode;
13671198

@@ -1371,7 +1202,6 @@
13711202
* @param {string} s String to decode
13721203
* @param {number} len Maximum output length
13731204
* @returns {!Array.<number>}
1374-
* @expose
13751205
*/
13761206
bcrypt.decodeBase64 = base64_decode;
13771207

Diff for: ‎dist/bcrypt.min.js

+52-43
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Diff for: ‎dist/bcrypt.min.js.gz

1.15 KB
Binary file not shown.

Diff for: ‎dist/bcrypt.min.map

+4-4
Large diffs are not rendered by default.

Diff for: ‎package.json

+3-4
Original file line numberDiff line numberDiff line change
@@ -33,11 +33,10 @@
3333
"browser": "dist/bcrypt.js",
3434
"dependencies": {},
3535
"devDependencies": {
36-
"testjs": "~1",
37-
"closurecompiler": "~1",
38-
"metascript": "~0.18",
3936
"bcrypt": "latest",
40-
"utfx": "~1"
37+
"closurecompiler": "~1",
38+
"metascript": "~1.0.0",
39+
"testjs": "~1"
4140
},
4241
"license": "MIT",
4342
"scripts": {

Diff for: ‎src/bcrypt.js

+1-11
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,6 @@ bcrypt.setRandomFallback = function(random) {
6666
* @param {number=} seed_length Not supported.
6767
* @returns {string} Resulting salt
6868
* @throws {Error} If a random fallback is required but not set
69-
* @expose
7069
*/
7170
bcrypt.genSaltSync = function(rounds, seed_length) {
7271
rounds = rounds || GENSALT_DEFAULT_LOG2_ROUNDS;
@@ -93,7 +92,6 @@ bcrypt.genSaltSync = function(rounds, seed_length) {
9392
* @param {function(Error, string=)=} callback Callback receiving the error, if any, and the resulting salt
9493
* @returns {!Promise} If `callback` has been omitted
9594
* @throws {Error} If `callback` is present but not a function
96-
* @expose
9795
*/
9896
bcrypt.genSalt = function(rounds, seed_length, callback) {
9997
if (typeof seed_length === 'function')
@@ -138,7 +136,6 @@ bcrypt.genSalt = function(rounds, seed_length, callback) {
138136
* @param {string} s String to hash
139137
* @param {(number|string)=} salt Salt length to generate or salt to use, default to 10
140138
* @returns {string} Resulting hash
141-
* @expose
142139
*/
143140
bcrypt.hashSync = function(s, salt) {
144141
if (typeof salt === 'undefined')
@@ -159,7 +156,6 @@ bcrypt.hashSync = function(s, salt) {
159156
* (0.0 - 1.0), maximally once per `MAX_EXECUTION_TIME = 100` ms.
160157
* @returns {!Promise} If `callback` has been omitted
161158
* @throws {Error} If `callback` is present but not a function
162-
* @expose
163159
*/
164160
bcrypt.hash = function(s, salt, callback, progressCallback) {
165161

@@ -199,7 +195,7 @@ bcrypt.hash = function(s, salt, callback, progressCallback) {
199195
*/
200196
function safeStringCompare(known, unknown) {
201197
var diff = known.length ^ unknown.length;
202-
for (var i=0, i<known.length; ++i) {
198+
for (var i = 0; i < known.length; ++i) {
203199
diff |= known.charCodeAt(i) ^ unknown.charCodeAt(i);
204200
}
205201
return diff === 0;
@@ -211,7 +207,6 @@ function safeStringCompare(known, unknown) {
211207
* @param {string} hash Hash to test against
212208
* @returns {boolean} true if matching, otherwise false
213209
* @throws {Error} If an argument is illegal
214-
* @expose
215210
*/
216211
bcrypt.compareSync = function(s, hash) {
217212
if (typeof s !== "string" || typeof hash !== "string")
@@ -230,7 +225,6 @@ bcrypt.compareSync = function(s, hash) {
230225
* (0.0 - 1.0), maximally once per `MAX_EXECUTION_TIME = 100` ms.
231226
* @returns {!Promise} If `callback` has been omitted
232227
* @throws {Error} If `callback` is present but not a function
233-
* @expose
234228
*/
235229
bcrypt.compare = function(s, hash, callback, progressCallback) {
236230

@@ -272,7 +266,6 @@ bcrypt.compare = function(s, hash, callback, progressCallback) {
272266
* @param {string} hash Hash to extract the used number of rounds from
273267
* @returns {number} Number of rounds used
274268
* @throws {Error} If `hash` is not a string
275-
* @expose
276269
*/
277270
bcrypt.getRounds = function(hash) {
278271
if (typeof hash !== "string")
@@ -285,7 +278,6 @@ bcrypt.getRounds = function(hash) {
285278
* @param {string} hash Hash to extract the salt from
286279
* @returns {string} Extracted salt part
287280
* @throws {Error} If `hash` is not a string or otherwise invalid
288-
* @expose
289281
*/
290282
bcrypt.getSalt = function(hash) {
291283
if (typeof hash !== 'string')
@@ -305,7 +297,6 @@ bcrypt.getSalt = function(hash) {
305297
* @param {!Array.<number>} b Byte array
306298
* @param {number} len Maximum input length
307299
* @returns {string}
308-
* @expose
309300
*/
310301
bcrypt.encodeBase64 = base64_encode;
311302

@@ -315,6 +306,5 @@ bcrypt.encodeBase64 = base64_encode;
315306
* @param {string} s String to decode
316307
* @param {number} len Maximum output length
317308
* @returns {!Array.<number>}
318-
* @expose
319309
*/
320310
bcrypt.decodeBase64 = base64_decode;

Diff for: ‎src/bcrypt/util.js

+5-14
Original file line numberDiff line numberDiff line change
@@ -8,26 +8,17 @@ var nextTick = typeof process !== 'undefined' && process && typeof process.nextT
88
? (typeof setImmediate === 'function' ? setImmediate : process.nextTick)
99
: setTimeout;
1010

11+
//? include("util/utf8.js");
12+
1113
/**
1214
* Converts a JavaScript string to UTF8 bytes.
15+
* @function
1316
* @param {string} str String
1417
* @returns {!Array.<number>} UTF8 bytes
1518
* @inner
1619
*/
17-
function stringToBytes(str) {
18-
var out = [],
19-
i = 0;
20-
utfx.encodeUTF16toUTF8(function() {
21-
if (i >= str.length) return null;
22-
return str.charCodeAt(i++);
23-
}, function(b) {
24-
out.push(b);
25-
});
26-
return out;
27-
}
20+
var stringToBytes = utf8Array;
2821

2922
//? include("util/base64.js");
3023

31-
//? include("../../node_modules/utfx/dist/utfx-embeddable.js");
32-
33-
Date.now = Date.now || function() { return +new Date; };
24+
Date.now = Date.now || function() { return +new Date; };

Diff for: ‎src/bcrypt/util/utf8.js

+52
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
/** Calculates the byte length of a string encoded as UTF8. */
2+
function utf8Length(string) {
3+
var len = 0,
4+
c = 0;
5+
for (var i = 0; i < string.length; ++i) {
6+
c = string.charCodeAt(i);
7+
if (c < 128)
8+
len += 1;
9+
else if (c < 2048)
10+
len += 2;
11+
else if (
12+
(c & 0xFC00) === 0xD800 &&
13+
(string.charCodeAt(i + 1) & 0xFC00) === 0xDC00
14+
) {
15+
++i;
16+
len += 4;
17+
} else
18+
len += 3;
19+
}
20+
return len;
21+
}
22+
23+
/** Converts a string to an array of UTF8 bytes. */
24+
function utf8Array(string) {
25+
var offset = 0,
26+
c1, c2;
27+
var buffer = new Array(utf8Length(string));
28+
for (var i = 0, k = string.length; i < k; ++i) {
29+
c1 = string.charCodeAt(i);
30+
if (c1 < 128) {
31+
buffer[offset++] = c1;
32+
} else if (c1 < 2048) {
33+
buffer[offset++] = c1 >> 6 | 192;
34+
buffer[offset++] = c1 & 63 | 128;
35+
} else if (
36+
( c1 & 0xFC00) === 0xD800 &&
37+
((c2 = string.charCodeAt(i + 1)) & 0xFC00) === 0xDC00
38+
) {
39+
c1 = 0x10000 + ((c1 & 0x03FF) << 10) + (c2 & 0x03FF);
40+
++i;
41+
buffer[offset++] = c1 >> 18 | 240;
42+
buffer[offset++] = c1 >> 12 & 63 | 128;
43+
buffer[offset++] = c1 >> 6 & 63 | 128;
44+
buffer[offset++] = c1 & 63 | 128;
45+
} else {
46+
buffer[offset++] = c1 >> 12 | 224;
47+
buffer[offset++] = c1 >> 6 & 63 | 128;
48+
buffer[offset++] = c1 & 63 | 128;
49+
}
50+
}
51+
return buffer;
52+
}

0 commit comments

Comments
 (0)
Please sign in to comment.