Skip to content

Commit

Permalink
API changes done. Now the normal methods will be async, *_sync will b…
Browse files Browse the repository at this point in the history
…e blocking operations.

Signed-off-by: Nick Campbell <nicholas.j.campbell@gmail.com>
  • Loading branch information
ncb000gt committed Mar 27, 2011
1 parent a805cd6 commit 150255d
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 47 deletions.
12 changes: 1 addition & 11 deletions bcrypt.js
@@ -1,13 +1,3 @@
var bcrypt_lib = require('./bcrypt_lib'),
emitter = require('events').Emitter,
exports = module.exports;
var bcrypt_lib = require('./bcrypt_lib');

//function BCrypt() {
//emitter.call(this);
//this.lib = new bcrypt_lib.BCrypt();

//}
//sys.inherits(BCrypt, emitter);

//module.exports = BCrypt;
module.exports = new bcrypt_lib.BCrypt();
50 changes: 31 additions & 19 deletions src/bcrypt_node.cc
Expand Up @@ -49,9 +49,12 @@ void BCrypt::Initialize (Handle<Object> target) {

t->InstanceTemplate()->SetInternalFieldCount(1);

NODE_SET_PROTOTYPE_METHOD(t, "gen_salt", BCrypt::GenerateSalt);
NODE_SET_PROTOTYPE_METHOD(t, "hashpw", BCrypt::HashPW);
NODE_SET_PROTOTYPE_METHOD(t, "compare", BCrypt::Compare);
NODE_SET_PROTOTYPE_METHOD(t, "gen_salt_sync", BCrypt::GenerateSaltSync);
NODE_SET_PROTOTYPE_METHOD(t, "hashpw_sync", BCrypt::HashPWSync);
NODE_SET_PROTOTYPE_METHOD(t, "compare_sync", BCrypt::CompareSync);
//NODE_SET_PROTOTYPE_METHOD(t, "gen_salt", BCrypt::GenerateSalt);
//NODE_SET_PROTOTYPE_METHOD(t, "hashpw", BCrypt::HashPW);
//NODE_SET_PROTOTYPE_METHOD(t, "compare", BCrypt::Compare);

target->Set(String::NewSymbol("BCrypt"), t->GetFunction());
}
Expand All @@ -65,9 +68,26 @@ Handle<Value> BCrypt::New(const Arguments& args) {
return args.This();
}

Handle<Value> BCrypt::GenerateSalt(const Arguments& args) {
BCrypt *bcrypt_obj = ObjectWrap::Unwrap<BCrypt>(args.This());
int BCrypt::GetSeed(u_int8_t *seed, int size) {
switch (RAND_bytes((unsigned char *)seed, size)) {
case -1:
case 0:
switch (RAND_pseudo_bytes(seed, size)) {
case -1:
return -1;
case 0:
return 0;
default:
return 1;
}
default:
return 1;

}
}

Handle<Value> BCrypt::GenerateSaltSync(const Arguments& args) {
BCrypt *bcrypt_obj = ObjectWrap::Unwrap<BCrypt>(args.This());
HandleScope scope;

int size = 20;
Expand All @@ -82,18 +102,12 @@ Handle<Value> BCrypt::GenerateSalt(const Arguments& args) {
size = args[1]->Int32Value();
}

u_int8_t *_seed;
_seed = (u_int8_t *)malloc(size * sizeof(u_int8_t));
switch (RAND_bytes((unsigned char *)_seed, size)) {
u_int8_t *_seed = (u_int8_t *)malloc(size * sizeof(u_int8_t));
switch(BCrypt::GetSeed(_seed, size)) {
case -1:
return ThrowException(Exception::Error(String::New("RAND does not support bytes operation")));
return ThrowException(Exception::Error(String::New("Rand operation not supported.")));
case 0:
switch (RAND_pseudo_bytes(_seed, size)) {
case -1:
return ThrowException(Exception::Error(String::New("RAND does not support pseudo_bytes operation")));
case 0:
return ThrowException(Exception::Error(String::New("RAND generated bytes were not cryptographically strong")));
}
return ThrowException(Exception::Error(String::New("Rand operation did not generate a cryptographically sound seed.")));
}
char* salt = bcrypt_gensalt(rounds, _seed);
int salt_len = strlen(salt);
Expand All @@ -103,9 +117,8 @@ Handle<Value> BCrypt::GenerateSalt(const Arguments& args) {
return scope.Close(outString);
}

Handle<Value> BCrypt::HashPW(const Arguments& args) {
Handle<Value> BCrypt::HashPWSync(const Arguments& args) {
BCrypt *bcrypt_obj = ObjectWrap::Unwrap<BCrypt>(args.This());

HandleScope scope;

if (args.Length() < 2) {
Expand All @@ -124,9 +137,8 @@ Handle<Value> BCrypt::HashPW(const Arguments& args) {
return scope.Close(outString);
}

Handle<Value> BCrypt::Compare(const Arguments& args) {
Handle<Value> BCrypt::CompareSync(const Arguments& args) {
BCrypt *bcrypt_obj = ObjectWrap::Unwrap<BCrypt>(args.This());

HandleScope scope;

if (args.Length() < 2) {
Expand Down
8 changes: 6 additions & 2 deletions src/bcrypt_node.h
Expand Up @@ -3,10 +3,14 @@
class BCrypt : public node::ObjectWrap {
static int EIO_GenSalt(eio_req *req);
static int EIO_GenSaltAfter(eio_req *req);
static int GetSeed(u_int8_t *seed, int size);
public:
static void Initialize(v8::Handle<v8::Object> target);
static v8::Handle<v8::Value> New(const v8::Arguments& args);
static v8::Handle<v8::Value> GenerateSalt(const v8::Arguments& args);
static v8::Handle<v8::Value> GenerateSaltSync(const v8::Arguments& args);
static v8::Handle<v8::Value> HashPWSync(const v8::Arguments& args);
static v8::Handle<v8::Value> CompareSync(const v8::Arguments& args);
/*static v8::Handle<v8::Value> GenerateSalt(const v8::Arguments& args);
static v8::Handle<v8::Value> HashPW(const v8::Arguments& args);
static v8::Handle<v8::Value> Compare(const v8::Arguments& args);
static v8::Handle<v8::Value> Compare(const v8::Arguments& args);*/
};
30 changes: 15 additions & 15 deletions test/functional.js
Expand Up @@ -3,65 +3,65 @@ var testCase = require('nodeunit').testCase,

module.exports = testCase({
test_salt_length: function(assert) {
var salt = bcrypt.gen_salt(10);
var salt = bcrypt.gen_salt_sync(10);
assert.equals(29, salt.length, "Salt isn't the correct length.");
assert.done();
},
test_salt_no_params: function(assert) {
assert.throws(function() {bcrypt.gen_salt();}, "Should throw an Error. gen_salt requires # of rounds.");
assert.throws(function() {bcrypt.gen_salt_sync();}, "Should throw an Error. gen_salt requires # of rounds.");
assert.done();
},
test_salt_rounds_is_string_number: function(assert) {
assert.throws(function() {bcrypt.gen_salt('10');}, "Should throw an Error. No params.");
assert.throws(function() {bcrypt.gen_salt_sync('10');}, "Should throw an Error. No params.");
assert.done();
},
test_salt_rounds_is_NaN: function(assert) {
assert.throws(function() {bcrypt.gen_salt('b');}, "Should throw an Error. gen_salt requires rounds to be a number.");
assert.throws(function() {bcrypt.gen_salt_sync('b');}, "Should throw an Error. gen_salt requires rounds to be a number.");
assert.done();
},
test_hashpw: function(assert) {
assert.ok(bcrypt.hashpw('password', bcrypt.gen_salt(10)), "Shouldn't throw an Error.");
assert.ok(bcrypt.hashpw_sync('password', bcrypt.gen_salt_sync(10)), "Shouldn't throw an Error.");
assert.done();
},
test_hash_pw_no_params: function(assert) {
assert.throws(function() {bcrypt.hashpw();}, "Should throw an Error. No Params.");
assert.throws(function() {bcrypt.hashpw_sync();}, "Should throw an Error. No Params.");
assert.done();
},
test_hash_pw_one_param: function(assert) {
assert.throws(function() {bcrypt.hashpw('password');}, "Should throw an Error. No salt.");
assert.throws(function() {bcrypt.hashpw_sync('password');}, "Should throw an Error. No salt.");
assert.done();
},
test_hash_pw_not_hash_str: function(assert) {
assert.throws(function() {bcrypt.hashpw('password', 1);}, "Should throw an Error. hash should be a string.");
assert.throws(function() {bcrypt.hashpw_sync('password', 1);}, "Should throw an Error. hash should be a string.");
assert.done();
},
test_verify_salt: function(assert) {
var salt = bcrypt.gen_salt(10);
var salt = bcrypt.gen_salt_sync(10);
var split_salt = salt.split('$');
assert.ok(split_salt[1], '2a');
assert.ok(split_salt[2], '10');
assert.done();
},
test_verify_salt_min_rounds: function(assert) {
var salt = bcrypt.gen_salt(1);
var salt = bcrypt.gen_salt_sync(1);
var split_salt = salt.split('$');
assert.ok(split_salt[1], '2a');
assert.ok(split_salt[2], '4');
assert.done();
},
test_verify_salt_max_rounds: function(assert) {
var salt = bcrypt.gen_salt(100);
var salt = bcrypt.gen_salt_sync(100);
var split_salt = salt.split('$');
assert.ok(split_salt[1], '2a');
assert.ok(split_salt[2], '31');
assert.done();
},
test_hash_compare: function(assert) {
var salt = bcrypt.gen_salt(10);
var salt = bcrypt.gen_salt_sync(10);
assert.equals(29, salt.length, "Salt isn't the correct length.");
var hash = bcrypt.hashpw("test", salt);
assert.ok(bcrypt.compare("test", hash), "These hashes should be equal.");
assert.ok(!(bcrypt.compare("blah", hash)), "These hashes should not be equal.");
var hash = bcrypt.hashpw_sync("test", salt);
assert.ok(bcrypt.compare_sync("test", hash), "These hashes should be equal.");
assert.ok(!(bcrypt.compare_sync("blah", hash)), "These hashes should not be equal.");
assert.done();
}
});

0 comments on commit 150255d

Please sign in to comment.