Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

The hash functions can now take a round value as the second parameter…

…. This allows someone to not have to handle the or deal with generating the salt since it really is boilerplate.

Signed-off-by: Nick Campbell <nicholas.j.campbell@gmail.com>
  • Loading branch information...
commit 9f9bb506329eee77d47f9d98ee84395a982e8dcc 1 parent f096371
@ncb000gt authored
Showing with 27 additions and 5 deletions.
  1. +14 −4 bcrypt.js
  2. +7 −0 test/async.js
  3. +6 −1 test/sync.js
View
18 bcrypt.js
@@ -91,8 +91,12 @@ module.exports.encrypt_sync = function(data, salt) {
module.exports.hashSync = function(data, salt) {
if (data == null || data == undefined || salt == null || salt == undefined) {
throw new Error('data and salt arguments required');
- } else if (typeof data !== 'string' || typeof salt !== 'string') {
- throw new Error('data and salt must be strings');
+ } else if (typeof data !== 'string' && (typeof salt !== 'string' || typeof salt !== 'number')) {
+ throw new Error('data must be a string and salt must either be a salt string or a number of rounds');
+ }
+
+ if (typeof salt === 'number') {
+ salt = module.exports.genSaltSync(salt);
}
return bindings.encrypt_sync(data, salt);
@@ -111,8 +115,8 @@ module.exports.encrypt = function(data, salt, cb) {
module.exports.hash = function(data, salt, cb) {
if (data == null || data == undefined || salt == null || salt == undefined) {
throw new Error('data and salt arguments required');
- } else if (typeof data !== 'string' || typeof salt !== 'string') {
- throw new Error('data and salt must be strings');
+ } else if (typeof data !== 'string' && (typeof salt !== 'string' || typeof salt !== 'number')) {
+ throw new Error('data must be a string and salt must either be a salt string or a number of rounds');
}
if (!cb) {
@@ -121,6 +125,12 @@ module.exports.hash = function(data, salt, cb) {
throw new Error('callback must be a function');
}
+ if (typeof salt === 'number') {
+ return module.exports.genSalt(salt, function(err, salt) {
+ return bindings.encrypt(data, salt, cb);
+ });
+ }
+
return bindings.encrypt(data, salt, cb);
};
View
7 test/async.js
@@ -33,6 +33,13 @@ module.exports = {
});
});
},
+ test_hash_rounds: function(assert) {
+ assert.expect(1);
+ bcrypt.hash('bacon', 8, function(err, hash) {
+ assert.equals(bcrypt.getRounds(hash), 8, "Number of rounds should be that specified in the function call.");
+ assert.done();
+ });
+ },
test_hash_empty_strings: function(assert) {
assert.expect(2);
bcrypt.genSalt(10, function(err, salt) {
View
7 test/sync.js
@@ -29,6 +29,11 @@ module.exports = {
assert.ok(bcrypt.hashSync('password', bcrypt.genSaltSync(10)), "Shouldn't throw an Error.");
assert.done();
},
+ test_hash_rounds: function(assert) {
+ var hash = bcrypt.hashSync('password', 8);
+ assert.equals(bcrypt.getRounds(hash), 8, "Number of rounds should equal 8.");
+ assert.done();
+ },
test_hash_empty_string: function(assert) {
assert.ok(bcrypt.hashSync('', bcrypt.genSaltSync(10)), "Shouldn't throw an Error.");
assert.throws(function() {bcrypt.hashSync('password', '')}, "Should have thrown an Error related to the salt.");
@@ -44,7 +49,7 @@ module.exports = {
assert.done();
},
test_hash_pw_not_hash_str: function(assert) {
- assert.throws(function() {bcrypt.hashSync('password', 1);}, "Should throw an Error. hash should be a string.");
+ assert.throws(function() {bcrypt.hashSync('password', {});}, "Should throw an Error. hash should be a string or number.");
assert.done();
},
test_hash_salt_validity: function(assert) {
Please sign in to comment.
Something went wrong with that request. Please try again.