Permalink
Browse files

fix annoying cryptic node errors when nulls are passed

  • Loading branch information...
1 parent f25a0a9 commit bb8b30a3ce4b1cee79ade5247074376e07aef225 @tj committed Jun 28, 2012
Showing with 30 additions and 0 deletions.
  1. +3 −0 index.js
  2. +27 −0 test/pwd.js
View
@@ -53,9 +53,12 @@ exports.iterations = function(n){
exports.hash = function(pwd, salt, fn){
if (3 == arguments.length) {
+ if (!pwd) return fn(new Error('password missing'));
+ if (!salt) return fn(new Error('salt missing'));
crypto.pbkdf2(pwd, salt, iterations, len, fn);
} else {
fn = salt;
+ if (!pwd) return fn(new Error('password missing'));
crypto.randomBytes(len, function(err, salt){
if (err) return fn(err);
salt = salt.toString('base64');
View
@@ -10,6 +10,15 @@ describe('.hash(pass, fn)', function(){
done();
})
})
+
+ describe('when pass is missing', function(){
+ it('should pass an error', function(done){
+ pass.hash(null, function(err, salt, hash){
+ err.message.should.include('password missing');
+ done();
+ })
+ })
+ })
})
describe('.hash(pass, salt, fn)', function(){
@@ -21,6 +30,24 @@ describe('.hash(pass, salt, fn)', function(){
})
})
})
+
+ describe('when pass is missing', function(){
+ it('should pass an error', function(done){
+ pass.hash(null, 'asdf', function(err, salt, hash){
+ err.message.should.include('password missing');
+ done();
+ })
+ })
+ })
+
+ describe('when salt is missing', function(){
+ it('should pass an error', function(done){
+ pass.hash('asdf', null, function(err, salt, hash){
+ err.message.should.include('salt missing');
+ done();
+ })
+ })
+ })
})
describe('.iterations(n)', function(){

0 comments on commit bb8b30a

Please sign in to comment.