|
|
|
|
|
|
|
|
|
|
|
|
|
|
var bcrypt = {};
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
var randomFallback = null;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
function random(len) {
|
|
|
if (typeof module !== 'undefined' && module && module['exports'])
|
|
|
try {
|
|
|
return require("crypto")['randomBytes'](len);
|
|
|
} catch (e) {}
|
|
|
try {
|
|
|
var a; (self['crypto']||self['msCrypto'])['getRandomValues'](a = new Uint32Array(len));
|
|
|
return Array.prototype.slice.call(a);
|
|
|
} catch (e) {}
|
|
|
if (!randomFallback)
|
|
|
throw Error("Neither WebCryptoAPI nor a crypto module is available. Use bcrypt.setRandomFallback to set an alternative");
|
|
|
return randomFallback(len);
|
|
|
}
|
|
|
|
|
|
|
|
|
var randomAvailable = false;
|
|
|
try {
|
|
|
random(1);
|
|
|
randomAvailable = true;
|
|
|
} catch (e) {}
|
|
|
|
|
|
|
|
|
randomFallback = function(len) {
|
|
|
for (var a=[], i=0; i<len; ++i)
|
|
|
a[i] = ((0.5 + isaac() * 2.3283064365386963e-10) * 256) | 0;
|
|
|
return a;
|
|
|
};null;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
bcrypt.setRandomFallback = function(random) {
|
|
|
randomFallback = random;
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
bcrypt.genSaltSync = function(rounds, seed_length) {
|
|
|
rounds = rounds || GENSALT_DEFAULT_LOG2_ROUNDS;
|
|
|
if (typeof rounds !== 'number')
|
|
|
throw Error("Illegal arguments: "+(typeof rounds)+", "+(typeof seed_length));
|
|
|
if (rounds < 4)
|
|
|
rounds = 4;
|
|
|
else if (rounds > 31)
|
|
|
rounds = 31;
|
|
|
var salt = [];
|
|
|
salt.push("$2a$");
|
|
|
if (rounds < 10)
|
|
|
salt.push("0");
|
|
|
salt.push(rounds.toString());
|
|
|
salt.push('$');
|
|
|
salt.push(base64_encode(random(BCRYPT_SALT_LEN), BCRYPT_SALT_LEN));
|
|
|
return salt.join('');
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
bcrypt.genSalt = function(rounds, seed_length, callback) {
|
|
|
if (typeof seed_length === 'function')
|
|
|
callback = seed_length,
|
|
|
seed_length = undefined;
|
|
|
if (typeof rounds === 'function')
|
|
|
callback = rounds,
|
|
|
rounds = undefined;
|
|
|
if (typeof rounds === 'undefined')
|
|
|
rounds = GENSALT_DEFAULT_LOG2_ROUNDS;
|
|
|
else if (typeof rounds !== 'number')
|
|
|
throw Error("illegal arguments: "+(typeof rounds));
|
|
|
|
|
|
function _async(callback) {
|
|
|
nextTick(function() {
|
|
|
try {
|
|
|
callback(null, bcrypt.genSaltSync(rounds));
|
|
|
} catch (err) {
|
|
|
callback(err);
|
|
|
}
|
|
|
});
|
|
|
}
|
|
|
|
|
|
if (callback) {
|
|
|
if (typeof callback !== 'function')
|
|
|
throw Error("Illegal callback: "+typeof(callback));
|
|
|
_async(callback);
|
|
|
} else
|
|
|
return new Promise(function(resolve, reject) {
|
|
|
_async(function(err, res) {
|
|
|
if (err) {
|
|
|
reject(err);
|
|
|
return;
|
|
|
}
|
|
|
resolve(res);
|
|
|
});
|
|
|
});
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
bcrypt.hashSync = function(s, salt) {
|
|
|
if (typeof salt === 'undefined')
|
|
|
salt = GENSALT_DEFAULT_LOG2_ROUNDS;
|
|
|
if (typeof salt === 'number')
|
|
|
salt = bcrypt.genSaltSync(salt);
|
|
|
if (typeof s !== 'string' || typeof salt !== 'string')
|
|
|
throw Error("Illegal arguments: "+(typeof s)+', '+(typeof salt));
|
|
|
return _hash(s, salt);
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
bcrypt.hash = function(s, salt, callback, progressCallback) {
|
|
|
|
|
|
function _async(callback) {
|
|
|
if (typeof s === 'string' && typeof salt === 'number')
|
|
|
bcrypt.genSalt(salt, function(err, salt) {
|
|
|
_hash(s, salt, callback, progressCallback);
|
|
|
});
|
|
|
else if (typeof s === 'string' && typeof salt === 'string')
|
|
|
_hash(s, salt, callback, progressCallback);
|
|
|
else
|
|
|
nextTick(callback.bind(this, Error("Illegal arguments: "+(typeof s)+', '+(typeof salt))));
|
|
|
}
|
|
|
|
|
|
if (callback) {
|
|
|
if (typeof callback !== 'function')
|
|
|
throw Error("Illegal callback: "+typeof(callback));
|
|
|
_async(callback);
|
|
|
} else
|
|
|
return new Promise(function(resolve, reject) {
|
|
|
_async(function(err, res) {
|
|
|
if (err) {
|
|
|
reject(err);
|
|
|
return;
|
|
|
}
|
|
|
resolve(res);
|
|
|
});
|
|
|
});
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
function safeStringCompare(known, unknown) {
|
|
|
var right = 0,
|
|
|
wrong = 0;
|
|
|
for (var i=0, k=known.length; i<k; ++i) {
|
|
|
if (known.charCodeAt(i) === unknown.charCodeAt(i))
|
|
|
++right;
|
|
|
else
|
|
|
++wrong;
|
|
|
}
|
|
|
|
|
|
if (right < 0)
|
|
|
return false;
|
|
|
return wrong === 0;
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
bcrypt.compareSync = function(s, hash) {
|
|
|
if (typeof s !== "string" || typeof hash !== "string")
|
|
|
throw Error("Illegal arguments: "+(typeof s)+', '+(typeof hash));
|
|
|
if (hash.length !== 60)
|
|
|
return false;
|
|
|
return safeStringCompare(bcrypt.hashSync(s, hash.substr(0, hash.length-31)), hash);
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
bcrypt.compare = function(s, hash, callback, progressCallback) {
|
|
|
|
|
|
function _async(callback) {
|
|
|
if (typeof s !== "string" || typeof hash !== "string") {
|
|
|
nextTick(callback.bind(this, Error("Illegal arguments: "+(typeof s)+', '+(typeof hash))));
|
|
|
return;
|
|
|
}
|
|
|
if (hash.length !== 60) {
|
|
|
nextTick(callback.bind(this, null, false));
|
|
|
return;
|
|
|
}
|
|
|
bcrypt.hash(s, hash.substr(0, 29), function(err, comp) {
|
|
|
if (err)
|
|
|
callback(err);
|
|
|
else
|
|
|
callback(null, safeStringCompare(comp, hash));
|
|
|
}, progressCallback);
|
|
|
}
|
|
|
|
|
|
if (callback) {
|
|
|
if (typeof callback !== 'function')
|
|
|
throw Error("Illegal callback: "+typeof(callback));
|
|
|
_async(callback);
|
|
|
} else
|
|
|
return new Promise(function(resolve, reject) {
|
|
|
_async(function(err, res) {
|
|
|
if (err) {
|
|
|
reject(err);
|
|
|
return;
|
|
|
}
|
|
|
resolve(res);
|
|
|
});
|
|
|
});
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
bcrypt.getRounds = function(hash) {
|
|
|
if (typeof hash !== "string")
|
|
|
throw Error("Illegal arguments: "+(typeof hash));
|
|
|
return parseInt(hash.split("$")[2], 10);
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
bcrypt.getSalt = function(hash) {
|
|
|
if (typeof hash !== 'string')
|
|
|
throw Error("Illegal arguments: "+(typeof hash));
|
|
|
if (hash.length !== 60)
|
|
|
throw Error("Illegal hash length: "+hash.length+" != 60");
|
|
|
return hash.substring(0, 29);
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
bcrypt.encodeBase64 = base64_encode;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
bcrypt.decodeBase64 = base64_decode;
|
|
|
|