From acd062c313712a3a54073b93ab475b41353e2505 Mon Sep 17 00:00:00 2001 From: Daniel Wirtz Date: Mon, 9 Jun 2014 23:17:26 +0200 Subject: [PATCH] Do not throw if hash.length != 60 --- README.md | 4 ++-- dist/bcrypt.js | 24 +++++++++++------------- dist/bcrypt.min.js | 24 ++++++++++++------------ package.json | 2 +- src/bcrypt.js | 24 +++++++++++------------- 5 files changed, 37 insertions(+), 41 deletions(-) diff --git a/README.md b/README.md index 1c4db13..521c997 100644 --- a/README.md +++ b/README.md @@ -1,8 +1,8 @@ ![bcrypt.js - bcrypt in plain JavaScript](https://raw.github.com/dcodeIO/bcrypt.js/master/bcrypt.png) =========== Optimized bcrypt in plain JavaScript with zero dependencies. Compiled through Closure Compiler using advanced -optimizations, 100% typed code. Fully compatible to [bcrypt](https://npmjs.org/package/bcrypt) and also working in the -browser. +optimizations, 100% typed code. Compatible to the C++ [bcrypt](https://npmjs.org/package/bcrypt) binding and also +working in the browser. Features ![Build Status](https://travis-ci.org/dcodeIO/bcrypt.js.png?branch=master) -------- diff --git a/dist/bcrypt.js b/dist/bcrypt.js index 46b96f3..b92cf1e 100644 --- a/dist/bcrypt.js +++ b/dist/bcrypt.js @@ -989,7 +989,7 @@ */ bcrypt.hashSync = function(s, salt) { if (!salt) salt = GENSALT_DEFAULT_LOG2_ROUNDS; - if (typeof salt == 'number') { + if (typeof salt === 'number') { salt = bcrypt.genSaltSync(salt); } return _hash(s, salt); @@ -1003,10 +1003,10 @@ * @expose */ bcrypt.hash = function(s, salt, callback) { - if (typeof callback != 'function') { + if (typeof callback !== 'function') { throw(new Error("Illegal 'callback': "+callback)); } - if (typeof salt == 'number') { + if (typeof salt === 'number') { bcrypt.genSalt(salt, function(err, salt) { _hash(s, salt, callback); }); @@ -1024,12 +1024,10 @@ * @expose */ bcrypt.compareSync = function(s, hash) { - if(typeof s != "string" || typeof hash != "string") { + if(typeof s !== "string" || typeof hash !== "string") { throw(new Error("Illegal argument types: "+(typeof s)+', '+(typeof hash))); } - if(hash.length != 60) { - throw(new Error("Illegal hash length: "+hash.length+" != 60")); - } + if (hash.length !== 60) return false; var comp = bcrypt.hashSync(s, hash.substr(0, hash.length-31)); var same = comp.length == hash.length; var max_length = (comp.length < hash.length) ? comp.length : hash.length; @@ -1053,7 +1051,7 @@ * @expose */ bcrypt.compare = function(s, hash, callback) { - if (typeof callback != 'function') { + if (typeof callback !== 'function') { throw(new Error("Illegal 'callback': "+callback)); } bcrypt.hash(s, hash.substr(0, 29), function(err, comp) { @@ -1069,7 +1067,7 @@ * @expose */ bcrypt.getRounds = function(hash) { - if(typeof hash != "string") { + if(typeof hash !== "string") { throw(new Error("Illegal type of 'hash': "+(typeof hash))); } return parseInt(hash.split("$")[2], 10); @@ -1083,19 +1081,19 @@ * @expose */ bcrypt.getSalt = function(hash) { - if (typeof hash != 'string') { + if (typeof hash !== 'string') { throw(new Error("Illegal type of 'hash': "+(typeof hash))); } - if(hash.length != 60) { + if (hash.length !== 60) { throw(new Error("Illegal hash length: "+hash.length+" != 60")); } return hash.substring(0, 29); }; // Enable module loading if available - if (typeof module != 'undefined' && module["exports"]) { // CommonJS + if (typeof module !== 'undefined' && module["exports"]) { // CommonJS module["exports"] = bcrypt; - } else if (typeof define != 'undefined' && define["amd"]) { // AMD + } else if (typeof define !== 'undefined' && define["amd"]) { // AMD define("bcrypt", function() { return bcrypt; }); } else { // Shim if (!global["dcodeIO"]) { diff --git a/dist/bcrypt.min.js b/dist/bcrypt.min.js index 998ad77..30bdd46 100644 --- a/dist/bcrypt.min.js +++ b/dist/bcrypt.min.js @@ -3,17 +3,17 @@ Released under the Apache License, Version 2.0 see: https://github.com/dcodeIO/bcrypt.js for details */ -function n(p){throw p;}var q=null; -(function(p){function v(b,a){var d=0,f=[],c,e;for((0>=a||a>b.length)&&n(Error("Invalid 'len': "+a));d>2&63]);c=(c&3)<<4;if(d>=a){f.push(s[c&63]);break}e=b[d++]&255;c|=e>>4&15;f.push(s[c&63]);c=(e&15)<<2;if(d>=a){f.push(s[c&63]);break}e=b[d++]&255;c|=e>>6&3;f.push(s[c&63]);f.push(s[e&63])}return f.join("")}function C(b){for(var a=0,d=b.length,f=0,c=[],e,k,g;af;){g=b.charCodeAt(a++);e=g=a||a>b.length)&&p(Error("Invalid 'len': "+a));d>2&63]);c=(c&3)<<4;if(d>=a){f.push(s[c&63]);break}e=b[d++]&255;c|=e>>4&15;f.push(s[c&63]);c=(e&15)<<2;if(d>=a){f.push(s[c&63]);break}e=b[d++]&255;c|=e>>6&3;f.push(s[c&63]);f.push(s[e&63])}return f.join("")}function C(b){for(var a=0,d=b.length,f=0,c=[],e,k,g;af;){g=b.charCodeAt(a++);e=g>>0;g|=(k&48)>>4;c.push(String.fromCharCode(g));if(16<=++f||a>=d)break;g=b.charCodeAt(a++);e=g>>0;g|=(e&60)>>2;c.push(String.fromCharCode(g));if(16<=++f||a>=d)break;g=b.charCodeAt(a++);k=g>>0;g|=k;c.push(String.fromCharCode(g));++f}b=[];for(a=0;a=g;)c=f[e>>24&255],c+=f[256|e>>16&255],c^=f[512|e>>8&255],c+=f[768|e& 255],k^=c^d[++g],c=f[k>>24&255],c+=f[256|k>>16&255],c^=f[512|k>>8&255],c+=f[768|k&255],e^=c^d[++g];b[a]=k^d[17];b[a+1]=e;return b}function t(b,a){var d,f=0;for(d=0;4>d;d++)f=f<<8|b[a]&255,a=(a+1)%b.length;return{key:f,a:a}}function y(b,a,d){for(var f=0,c=[0,0],e=a.length,k=d.length,g=0;gh;h++)for(m=0;m>1;m++)u(e,m<<1,g,l);p=[];for(h=0;h>24&255)>>>0),p.push((e[h]>>16&255)>>>0),p.push((e[h]>>8&255)>>>0),p.push((e[h]&255)>>>0);return f?(f(q,p),q):p}f&&z(c);return q}var e=B.slice(),k=e.length;(4>d||31=a&&(a=b.codePointAt(f),65535a&&n(RangeError("Illegal code point: "+a));128>a?c.push(a&127):2048>a?(c.push(a>>6&31|192),c.push(a&63|128)):65536>a?(c.push(a>>12&15|224),c.push(a>>6&63|128),c.push(a&63|128)):1114112>a?(c.push(a>>18&7|240),c.push(a>>12&63|128),c.push(a>>6&63|128),c.push(a&63|128)):n(RangeError("Illegal code point: "+a))}return d}function w(b,a,d){function f(a){var b=[];b.push("$2"); -"a"<=c&&b.push(c);b.push("$");10>k&&b.push("0");b.push(k.toString());b.push("$");b.push(v(g,g.length));b.push(v(a,4*B.length-1));return b.join("")}var c,e;("$"!=a.charAt(0)||"2"!=a.charAt(1))&&n(Error("Invalid salt version: "+a.substring(0,2)));"$"==a.charAt(2)?(c=String.fromCharCode(0),e=3):(c=a.charAt(2),("a"!=c||"$"!=a.charAt(3))&&n(Error("Invalid salt revision: "+a.substring(2,4))),e=4);"$"h;h++)for(m=0;m>1;m++)u(e,m<<1,g,l);n=[];for(h=0;h>24&255)>>>0),n.push((e[h]>>16&255)>>>0),n.push((e[h]>>8&255)>>>0),n.push((e[h]&255)>>>0);return f?(f(q,n),q):n}f&&z(c);return q}var e=B.slice(),k=e.length;(4>d||31=a&&(a=b.codePointAt(f),65535a&&p(RangeError("Illegal code point: "+a));128>a?c.push(a&127):2048>a?(c.push(a>>6&31|192),c.push(a&63|128)):65536>a?(c.push(a>>12&15|224),c.push(a>>6&63|128),c.push(a&63|128)):1114112>a?(c.push(a>>18&7|240),c.push(a>>12&63|128),c.push(a>>6&63|128),c.push(a&63|128)):p(RangeError("Illegal code point: "+a))}return d}function w(b,a,d){function f(a){var b=[];b.push("$2"); +"a"<=c&&b.push(c);b.push("$");10>k&&b.push("0");b.push(k.toString());b.push("$");b.push(v(g,g.length));b.push(v(a,4*B.length-1));return b.join("")}var c,e;("$"!=a.charAt(0)||"2"!=a.charAt(1))&&p(Error("Invalid salt version: "+a.substring(0,2)));"$"==a.charAt(2)?(c=String.fromCharCode(0),e=3):(c=a.charAt(2),("a"!=c||"$"!=a.charAt(3))&&p(Error("Invalid salt revision: "+a.substring(2,4))),e=4);"$"c||c>=f))return a=b.charCodeAt(c),55296<=a&&(56319>=a&&f>c+1)&&(b=b.charCodeAt(c+1),56320<=b&&57343>=b)?1024*(a-55296)+b-56320+65536:a}Object.defineProperty?Object.defineProperty(String.prototype,"codePointAt",{value:b,configurable:!0,writable:!0}):String.prototype.codePointAt=b}();var m={},E=[608135816,2242054355,320440878,57701188,2752067618,698298832,137296536,3964562569,1160258022, +function(){function b(a){this==q&&p(TypeError());var b=String(this),f=b.length,c=a?Number(a):0;c!=c&&(c=0);if(!(0>c||c>=f))return a=b.charCodeAt(c),55296<=a&&(56319>=a&&f>c+1)&&(b=b.charCodeAt(c+1),56320<=b&&57343>=b)?1024*(a-55296)+b-56320+65536:a}Object.defineProperty?Object.defineProperty(String.prototype,"codePointAt",{value:b,configurable:!0,writable:!0}):String.prototype.codePointAt=b}();var m={},E=[608135816,2242054355,320440878,57701188,2752067618,698298832,137296536,3964562569,1160258022, 953160567,3193202383,887688300,3232508343,3380367581,1065670069,3041331479,2450970073,2306472731],F=[3509652390,2564797868,805139163,3491422135,3101798381,1780907670,3128725573,4046225305,614570311,3012652279,134345442,2240740374,1667834072,1901547113,2757295779,4103290238,227898511,1921955416,1904987480,2182433518,2069144605,3260701109,2620446009,720527379,3318853667,677414384,3393288472,3101374703,2390351024,1614419982,1822297739,2954791486,3608508353,3174124327,2024746970,1432378464,3864339955, 2857741204,1464375394,1676153920,1439316330,715854006,3033291828,289532110,2706671279,2087905683,3018724369,1668267050,732546397,1947742710,3462151702,2609353502,2950085171,1814351708,2050118529,680887927,999245976,1800124847,3300911131,1713906067,1641548236,4213287313,1216130144,1575780402,4018429277,3917837745,3693486850,3949271944,596196993,3549867205,258830323,2213823033,772490370,2760122372,1774776394,2652871518,566650946,4142492826,1728879713,2882767088,1783734482,3629395816,2517608232,2874225571, 1861159788,326777828,3124490320,2130389656,2716951837,967770486,1724537150,2185432712,2364442137,1164943284,2105845187,998989502,3765401048,2244026483,1075463327,1455516326,1322494562,910128902,469688178,1117454909,936433444,3490320968,3675253459,1240580251,122909385,2157517691,634681816,4142456567,3825094682,3061402683,2540495037,79693498,3249098678,1084186820,1583128258,426386531,1761308591,1047286709,322548459,995290223,1845252383,2603652396,3431023940,2942221577,3202600964,3727903485,1712269319, @@ -36,7 +36,7 @@ function(){function b(a){this==q&&n(TypeError());var b=String(this),f=b.length,c 471910574,1539241949,458788160,3436315007,1807016891,3718408830,978976581,1043663428,3165965781,1927990952,4200891579,2372276910,3208408903,3533431907,1412390302,2931980059,4132332400,1947078029,3881505623,4168226417,2941484381,1077988104,1320477388,886195818,18198404,3786409E3,2509781533,112762804,3463356488,1866414978,891333506,18488651,661792760,1628790961,3885187036,3141171499,876946877,2693282273,1372485963,791857591,2686433993,3759982718,3167212022,3472953795,2716379847,445679433,3561995674, 3504004811,3574258232,54117162,3331405415,2381918588,3769707343,4154350007,1140177722,4074052095,668550556,3214352940,367459370,261225585,2610173221,4209349473,3468074219,3265815641,314222801,3066103646,3808782860,282218597,3406013506,3773591054,379116347,1285071038,846784868,2669647154,3771962079,3550491691,2305946142,453669953,1268987020,3317592352,3279303384,3744833421,2610507566,3859509063,266596637,3847019092,517658769,3462560207,3443424879,370717030,4247526661,2224018117,4143653529,4112773975, 2788324899,2477274417,1456262402,2901442914,1517677493,1846949527,2295493580,3734397586,2176403920,1280348187,1908823572,3871786941,846861322,1172426758,3287448474,3383383037,1655181056,3139813346,901632758,1897031941,2986607138,3066810236,3447102507,1393639104,373351379,950779232,625454576,3124240540,4148612726,2007998917,544563296,2244738638,2330496472,2058025392,1291430526,424198748,50039436,29584100,3605783033,2429876329,2791104160,1057563949,3255363231,3075367218,3463963227,1469046755,985887462], -B=[1332899944,1700884034,1701343084,1684370003,1668446532,1869963892],x=q;m.setRandomPolyfill=function(b){x=b};m.genSaltSync=function(b){b||(b=10);var a;b=b||10;(4>b||31b&&d.push("0");d.push(b.toString());d.push("$");try{d.push(v(H(),16)),a=d.join("")}catch(f){n(f)}return a};m.genSalt=function(b,a,d){"function"==typeof a&&(d=a,a=-1);var f;"function"==typeof b?(d=b,f=10):f=parseInt(b,10);"function"!=typeof d&&n(Error("Illegal or missing 'callback': "+ -d));z(function(){try{var a=m.genSaltSync(f);d(q,a)}catch(b){d(b,q)}})};m.hashSync=function(b,a){a||(a=10);"number"==typeof a&&(a=m.genSaltSync(a));return w(b,a)};m.hash=function(b,a,d){"function"!=typeof d&&n(Error("Illegal 'callback': "+d));"number"==typeof a?m.genSalt(a,function(a,c){w(b,c,d)}):w(b,a,d)};m.compareSync=function(b,a){("string"!=typeof b||"string"!=typeof a)&&n(Error("Illegal argument types: "+typeof b+", "+typeof a));60!=a.length&&n(Error("Illegal hash length: "+a.length+" != 60")); -for(var d=m.hashSync(b,a.substr(0,a.length-31)),f=d.length==a.length,c=d.length=e&&(a.length>=e&&d[e]!=a[e])&&(f=!1);return f};m.compare=function(b,a,d){"function"!=typeof d&&n(Error("Illegal 'callback': "+d));m.hash(b,a.substr(0,29),function(b,c){d(b,a===c)})};m.getRounds=function(b){"string"!=typeof b&&n(Error("Illegal type of 'hash': "+typeof b));return parseInt(b.split("$")[2],10)};m.getSalt=function(b){"string"!=typeof b&&n(Error("Illegal type of 'hash': "+ -typeof b));60!=b.length&&n(Error("Illegal hash length: "+b.length+" != 60"));return b.substring(0,29)};"undefined"!=typeof module&&module.exports?module.exports=m:"undefined"!=typeof define&&define.amd?define("bcrypt",function(){return m}):(p.dcodeIO||(p.dcodeIO={}),p.dcodeIO.bcrypt=m)})(this);})(); +B=[1332899944,1700884034,1701343084,1684370003,1668446532,1869963892],x=q;m.setRandomPolyfill=function(b){x=b};m.genSaltSync=function(b){b||(b=10);var a;b=b||10;(4>b||31b&&d.push("0");d.push(b.toString());d.push("$");try{d.push(v(H(),16)),a=d.join("")}catch(f){p(f)}return a};m.genSalt=function(b,a,d){"function"==typeof a&&(d=a,a=-1);var f;"function"==typeof b?(d=b,f=10):f=parseInt(b,10);"function"!=typeof d&&p(Error("Illegal or missing 'callback': "+ +d));z(function(){try{var a=m.genSaltSync(f);d(q,a)}catch(b){d(b,q)}})};m.hashSync=function(b,a){a||(a=10);"number"===typeof a&&(a=m.genSaltSync(a));return w(b,a)};m.hash=function(b,a,d){"function"!==typeof d&&p(Error("Illegal 'callback': "+d));"number"===typeof a?m.genSalt(a,function(a,c){w(b,c,d)}):w(b,a,d)};m.compareSync=function(b,a){("string"!==typeof b||"string"!==typeof a)&&p(Error("Illegal argument types: "+typeof b+", "+typeof a));if(60!==a.length)return!1;for(var d=m.hashSync(b,a.substr(0, +a.length-31)),f=d.length==a.length,c=d.length=e&&(a.length>=e&&d[e]!=a[e])&&(f=!1);return f};m.compare=function(b,a,d){"function"!==typeof d&&p(Error("Illegal 'callback': "+d));m.hash(b,a.substr(0,29),function(b,c){d(b,a===c)})};m.getRounds=function(b){"string"!==typeof b&&p(Error("Illegal type of 'hash': "+typeof b));return parseInt(b.split("$")[2],10)};m.getSalt=function(b){"string"!==typeof b&&p(Error("Illegal type of 'hash': "+typeof b));60!==b.length&& +p(Error("Illegal hash length: "+b.length+" != 60"));return b.substring(0,29)};"undefined"!==typeof module&&module.exports?module.exports=m:"undefined"!==typeof define&&define.amd?define("bcrypt",function(){return m}):(n.dcodeIO||(n.dcodeIO={}),n.dcodeIO.bcrypt=m)})(this);})(); diff --git a/package.json b/package.json index c4def17..b76f6d6 100644 --- a/package.json +++ b/package.json @@ -1,7 +1,7 @@ { "name": "bcryptjs", "description": "Optimized bcrypt in plain JavaScript with zero dependencies. 100% typed code. Fully compatible to 'bcrypt'.", - "version": "1.0.0", + "version": "1.0.1", "author": "Daniel Wirtz ", "contributors": [ "Shane Girish (https://github.com/shaneGirish)", diff --git a/src/bcrypt.js b/src/bcrypt.js index bd60c59..29d34ed 100644 --- a/src/bcrypt.js +++ b/src/bcrypt.js @@ -742,7 +742,7 @@ */ bcrypt.hashSync = function(s, salt) { if (!salt) salt = GENSALT_DEFAULT_LOG2_ROUNDS; - if (typeof salt == 'number') { + if (typeof salt === 'number') { salt = bcrypt.genSaltSync(salt); } return _hash(s, salt); @@ -756,10 +756,10 @@ * @expose */ bcrypt.hash = function(s, salt, callback) { - if (typeof callback != 'function') { + if (typeof callback !== 'function') { throw(new Error("Illegal 'callback': "+callback)); } - if (typeof salt == 'number') { + if (typeof salt === 'number') { bcrypt.genSalt(salt, function(err, salt) { _hash(s, salt, callback); }); @@ -777,12 +777,10 @@ * @expose */ bcrypt.compareSync = function(s, hash) { - if(typeof s != "string" || typeof hash != "string") { + if(typeof s !== "string" || typeof hash !== "string") { throw(new Error("Illegal argument types: "+(typeof s)+', '+(typeof hash))); } - if(hash.length != 60) { - throw(new Error("Illegal hash length: "+hash.length+" != 60")); - } + if (hash.length !== 60) return false; var comp = bcrypt.hashSync(s, hash.substr(0, hash.length-31)); var same = comp.length == hash.length; var max_length = (comp.length < hash.length) ? comp.length : hash.length; @@ -806,7 +804,7 @@ * @expose */ bcrypt.compare = function(s, hash, callback) { - if (typeof callback != 'function') { + if (typeof callback !== 'function') { throw(new Error("Illegal 'callback': "+callback)); } bcrypt.hash(s, hash.substr(0, 29), function(err, comp) { @@ -822,7 +820,7 @@ * @expose */ bcrypt.getRounds = function(hash) { - if(typeof hash != "string") { + if(typeof hash !== "string") { throw(new Error("Illegal type of 'hash': "+(typeof hash))); } return parseInt(hash.split("$")[2], 10); @@ -836,19 +834,19 @@ * @expose */ bcrypt.getSalt = function(hash) { - if (typeof hash != 'string') { + if (typeof hash !== 'string') { throw(new Error("Illegal type of 'hash': "+(typeof hash))); } - if(hash.length != 60) { + if (hash.length !== 60) { throw(new Error("Illegal hash length: "+hash.length+" != 60")); } return hash.substring(0, 29); }; // Enable module loading if available - if (typeof module != 'undefined' && module["exports"]) { // CommonJS + if (typeof module !== 'undefined' && module["exports"]) { // CommonJS module["exports"] = bcrypt; - } else if (typeof define != 'undefined' && define["amd"]) { // AMD + } else if (typeof define !== 'undefined' && define["amd"]) { // AMD define("bcrypt", function() { return bcrypt; }); } else { // Shim if (!global["dcodeIO"]) {