Skip to content

Commit

Permalink
init
Browse files Browse the repository at this point in the history
  • Loading branch information
jonathanong committed Oct 20, 2014
0 parents commit 1dcc944
Show file tree
Hide file tree
Showing 7 changed files with 178 additions and 0 deletions.
8 changes: 8 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@

.DS_Store*
*.log
*.gz

node_modules
coverage
cache
6 changes: 6 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
node_js:
- "0.10"
- "0.11"
language: node_js
script: "npm run-script test-travis"
after_script: "npm install coveralls@2 && cat ./coverage/lcov.info | coveralls"
22 changes: 22 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@

The MIT License (MIT)

Copyright (c) 2014 Jonathan Ong [email protected]

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.
54 changes: 54 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@

# bcrypt-then

[![NPM version][npm-image]][npm-url]
[![Build status][travis-image]][travis-url]
[![Test coverage][coveralls-image]][coveralls-url]
[![Dependency Status][david-image]][david-url]
[![License][license-image]][license-url]
[![Downloads][downloads-image]][downloads-url]
[![Gittip][gittip-image]][gittip-url]

[bcrypt](https://github.com/ncb000gt/node.bcrypt.js/) as promised.

## API

Only implements two methods:

### bcrypt.hash(password, [iterations]).then( hash => )

Hash a password with a # of `iterations`, defaulting to `10`.

```js
bcrypt.hash('password', 15).then(function (hash) {

})
```

### bcrypt.compare(password, hash).then( valid => )

Compare a password with a bcrypt hash.
Returns a boolean.

```js
bcrypt.compare('password', user.password).then(function (valid) {
assert(valid)
})
```

[npm-image]: https://img.shields.io/npm/v/bcrypt-then.svg?style=flat-square
[npm-url]: https://npmjs.org/package/bcrypt-then
[github-tag]: http://img.shields.io/github/tag/thenables/bcrypt-then.svg?style=flat-square
[github-url]: https://github.com/thenables/bcrypt-then/tags
[travis-image]: https://img.shields.io/travis/thenables/bcrypt-then.svg?style=flat-square
[travis-url]: https://travis-ci.org/thenables/bcrypt-then
[coveralls-image]: https://img.shields.io/coveralls/thenables/bcrypt-then.svg?style=flat-square
[coveralls-url]: https://coveralls.io/r/thenables/bcrypt-then
[david-image]: http://img.shields.io/david/thenables/bcrypt-then.svg?style=flat-square
[david-url]: https://david-dm.org/thenables/bcrypt-then
[license-image]: http://img.shields.io/npm/l/bcrypt-then.svg?style=flat-square
[license-url]: LICENSE
[downloads-image]: http://img.shields.io/npm/dm/bcrypt-then.svg?style=flat-square
[downloads-url]: https://npmjs.org/package/bcrypt-then
[gittip-image]: https://img.shields.io/gratipay/jonathanong.svg?style=flat-square
[gittip-url]: https://gratipay.com/jonathanong/
22 changes: 22 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@

var bcrypt = require('bcrypt')
var Promise = require('native-or-bluebird')

exports.hash = function (password, salt) {
salt = salt || 10
return new Promise(function (resolve, reject) {
bcrypt.hash(password, salt, function (err, hash) {
if (err) return reject(err)
resolve(hash)
})
})
}

exports.compare = function (expected, hash) {
return new Promise(function (resolve, reject) {
bcrypt.compare(expected, hash, function (err, res) {
if (err) return reject(err)
resolve(res)
})
})
}
29 changes: 29 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
{
"name": "bcrypt-then",
"description": "bcrypt as promised",
"version": "1.0.0",
"author": "Jonathan Ong <[email protected]> (http://jongleberry.com)",
"license": "MIT",
"repository": "thenables/bcrypt-then",
"dependencies": {
"bcrypt": "^0.8.0",
"native-or-bluebird": "^1.1.1"
},
"devDependencies": {
"bluebird": "^2.3.6",
"istanbul": "0",
"mocha": "1"
},
"scripts": {
"test": "mocha --reporter spec",
"test-cov": "istanbul cover node_modules/mocha/bin/_mocha -- --reporter dot",
"test-travis": "istanbul cover node_modules/mocha/bin/_mocha --report lcovonly -- --reporter dot"
},
"keywords": [
"bcrypt",
"then",
"promise",
"password"
],
"files": ["index.js"]
}
37 changes: 37 additions & 0 deletions test/test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@

var assert = require('assert')
var bcrypt = require('..')

describe('bcrypt', function () {
describe('.hash', function () {
it('(password)', function () {
return bcrypt.hash('password').then(function (hash) {
assert(typeof hash === 'string')
})
})

it('(password, salt)', function () {
return bcrypt.hash('password', 10, function (hash) {
assert(typeof hash === 'string')
})
})
})

describe('.compare', function () {
it('should return `true` for valid passwords', function () {
return bcrypt.hash('password').then(function (hash) {
return bcrypt.compare('password', hash)
}).then(function (valid) {
assert(valid === true)
})
})

it('should return `false` for iinvalid passwords', function () {
return bcrypt.hash('password').then(function (hash) {
return bcrypt.compare('kljasdjfhaskdf', hash)
}).then(function (valid) {
assert(valid === false)
})
})
})
})

0 comments on commit 1dcc944

Please sign in to comment.