Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Changes to make using the library a little easier and less typing. Al…
…so, getting ready to push to npm. Moved to nodeunit testing. Added a few tests...nothing special. Updated the readme and changed the licening to MIT.

Signed-off-by: Nick Campbell <nicholas.j.campbell@gmail.com>
  • Loading branch information
ncb000gt committed Nov 5, 2010
1 parent f90d46f commit 183f816
Show file tree
Hide file tree
Showing 8 changed files with 125 additions and 81 deletions.
48 changes: 19 additions & 29 deletions LICENSE
@@ -1,29 +1,19 @@
/*
* Copyright (c) 2101, Nicholas Campbell <nicholas.j.campbell@gmail.com>
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. Neither the name of the <ORGANIZATION> nor the names of its
* contributors may be used to endorse or promote products derived from
* this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
* HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
Copyright (c) 2010 Nicholas Campbell

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
36 changes: 20 additions & 16 deletions README.md
Expand Up @@ -10,9 +10,15 @@ Dependencies

* NodeJS
* BSD Libs
* [mjsunit.runner][mjsunit.runner]

To Build

From NPM
============

npm install bcrypt


From Source
============

I run K/Ubuntu. I was able to pull in the bsd libs using:
Expand All @@ -22,29 +28,30 @@ _Eventually I'd like to get it so that the libs are all built in to the same pac
What is required is the file `random.h` within `/usr/includes/bsd/` and the compiled bsd libs in `/usr/lib/`. These have to match the conf.check.

Assuming you've already built node, you can run the waf script:
node-waf configure
node-waf configure
node-waf build

npm link

Usage
============

To hash a password:
var bc = new bcrypt.BCrypt();
var salt = bc.gen_salt(20);
var hash = bc.hashpw("B4c0/\/", salt);
var bcrypt = require('bcrypt');
var salt = bcrypt.gen_salt(20);
var hash = bcrypt.hashpw("B4c0/\/", salt);

To check a password:
bc.compare("B4c0/\/", hash); // true
bc.compare("not_bacon", hash); // false
var bcrypt = require('bcrypt');
bcrypt.compare("B4c0/\/", hash); // true
bcrypt.compare("not_bacon", hash); // false


Testing
============

To test this module from the JS level, make sure to build the module. Then, assuming node is on your $PATH:
node test/runner.js

I am using nodeunit. I like the way you write tests with it and I like the default output. As such you'll need it to run the tests. I suspect my tests would run on an older version, but these were written and worked against 0.3.1
npm install nodeunit@0.3.1
nodeunit test/

Credits
============
Expand All @@ -64,8 +71,5 @@ Unless stated elsewhere, file headers or otherwise, the license as stated in the





[bcryptwiki]: http://en.wikipedia.org/wiki/Crypt_(Unix)#Blowfish-based_scheme
[bcryptgs]: http://mail-index.netbsd.org/tech-crypto/2002/05/24/msg000204.html
[mjsunit.runner]: http://github.com/tmpvar/mjsunit.runner
[bcryptgs]: http://mail-index.netbsd.org/tech-crypto/2002/05/24/msg000204.html
3 changes: 3 additions & 0 deletions bcrypt.js
@@ -0,0 +1,3 @@
var bcrypt_lib = require('./bcrypt_lib');

module.exports = new bcrypt_lib.BCrypt();
28 changes: 28 additions & 0 deletions package.json
@@ -0,0 +1,28 @@
{
"name": "bcrypt",
"description": "A bcrypt library for NodeJS.",
"main": "./bcrypt",
"version": "0.1.0",
"author": "Nick Campbell (http://github.com/ncb000gt)",
"engines": { "node": ">= 0.1.100" },
"repository": {
"type": "git",
"url": "http://github.com/ncb000gt/node.bcrypt.js.git"
},
"licenses": [
{
"type": "MIT"
}
],
"bugs": {
"web" : "http://github.com/ncb000gt/node.bcrypt.js/issues"
},
"dependencies": {
"nodeunit": ">=0.3.1",
},
"scripts": {
"build" : "node-waf configure build",
"test" : "node-waf test"
"clean" : "node-waf clean"
}
}
17 changes: 0 additions & 17 deletions test/core.js

This file was deleted.

46 changes: 46 additions & 0 deletions test/functional.js
@@ -0,0 +1,46 @@
var testCase = require('nodeunit').testCase,
bcrypt = require('../bcrypt');

module.exports = testCase({
test_salt_length: function(assert) {
var salt = bcrypt.gen_salt(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.done();
},
test_salt_rounds_is_string_number: function(assert) {
assert.throws(function() {bcrypt.gen_salt('55');}, "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.done();
},
test_hashpw: function(assert) {
assert.ok(bcrypt.hashpw('password', bcrypt.gen_salt(100)), "Should throw an Error. hash should not be null/undefined.");
assert.done();
},
test_hash_pw_no_params: function(assert) {
assert.throws(function() {bcrypt.hashpw();}, "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 hash.");
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.done();
},
test_hash_compare: function(assert) {
var salt = bcrypt.gen_salt(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.");
assert.done();
}
});
11 changes: 0 additions & 11 deletions test/runner.js

This file was deleted.

17 changes: 9 additions & 8 deletions wscript
@@ -1,6 +1,6 @@
import Options
import Options, Utils
from os import unlink, symlink, popen
from os.path import exists
from os.path import exists, islink

srcdir = "."
blddir = "build"
Expand All @@ -20,7 +20,7 @@ def configure(conf):

def build(bld):
bcryptnode = bld.new_task_gen("cxx", "shlib", "node_addon")
bcryptnode.target = "bcrypt_node"
bcryptnode.target = "bcrypt_lib"
bcryptnode.source = """
src/blowfish.cc
src/bcrypt.cc
Expand All @@ -30,12 +30,13 @@ def build(bld):
/usr/includes/bsd/
"""
bcryptnode.uselib = 'LIBBSD'
def test(t):
Utils.exec_command('nodeunit test')

def shutdown():
# HACK to get bcrypt.node out of build directory.
# better way to do this?
t = 'bcrypt_lib.node'
if Options.commands['clean']:
if exists('bcrypt_node.node'): unlink('bcrypt_node.node')
if exists(t): unlink(t)
else:
if exists('build/default/bcrypt_node.node') and not exists('bcrypt_node.node'):
symlink('build/default/bcrypt_node.node', 'bcrypt_node.node')
if exists('build/default/' + t) and not exists(t):
symlink('build/default/' + t, t)

0 comments on commit 183f816

Please sign in to comment.