UNPKG

5.1 kBMarkdownView Raw
1# Hashy
2
3[![Build Status](https://img.shields.io/travis/JsCommunity/hashy/master.svg)](http://travis-ci.org/JsCommunity/hashy)
4[![Dependency Status](https://david-dm.org/JsCommunity/hashy/status.svg?theme=shields.io)](https://david-dm.org/JsCommunity/hashy)
5[![devDependency Status](https://david-dm.org/JsCommunity/hashy/dev-status.svg?theme=shields.io)](https://david-dm.org/JsCommunity/hashy#info=devDependencies)
6
7> Hash passwords the right way (Argon2 & bcrypt support)
8
9Hashy is small [node.js](http://nodejs.org/) library which aims to do
10passwords hashing *[the correct
11way](https://wiki.php.net/rfc/password_hash)*.
12
13It has been heavily inspired by the new [PHP password hashing
14API](http://www.php.net/manual/en/book.password.php) but, following
15the node.js philosophy, hashing is done asynchronously.
16
17Furthermore, to make the interfaces as easy to use as possible, async
18functions can either be used with callbacks or they return
19[promises](https://en.wikipedia.org/wiki/Promise_%28programming%29)
20which will make them super easy to work with [generators](https://github.com/petkaantonov/bluebird/blob/master/API.md#generators)!
21
22Supported algorithms:
23
24- [Argon2](https://en.wikipedia.org/wiki/Argon2)
25- [bcrypt](https://en.wikipedia.org/wiki/Bcrypt)
26
27## Why a new library?
28
29The other ones I found were too complicated and/or were missing
30important features.
31
32The main missing feature is the `needRehash()` function: cryptography
33is a fast-moving science and algorithms can quickly become obsolete or
34their parameters needs to be adjusted to compensate the performance
35increase of recent computers (e.g. [bcrypt cost
36factor](http://phpmaster.com/why-you-should-use-bcrypt-to-hash-stored-passwords/)).
37
38This is exactly what this function is for: checking whether a hash
39uses the correct algorithm (and options) to see if we need to compute
40a new hash for this password.
41
42## Install
43
44Installation of the npm package:
45
46```
47> npm install --save hashy
48```
49
50Hashy requires promises support, for Node versions prior to 0.12 [see
51this page](https://github.com/JsCommunity/promise-toolbox#usage) to
52enable them.
53
54## How to use it?
55
56First, you may take a look at examples: using [callbacks](https://github.com/JsCommunity/hashy/blob/master/examples/callbacks.js), [promises](https://github.com/JsCommunity/hashy/blob/master/examples/promises.js) or [generators](https://github.com/JsCommunity/hashy/blob/master/examples/generators.js) (requires Node >= 0.11).
57
58### Creating a hash
59
60```js
61hashy.hash(password, function (error, hash) {
62 if (error) {
63 return console.log(error)
64 }
65
66 console.log('generated hash: ', hash)
67})
68```
69
70`hash()` handles additionaly two parameters which may be passed before the callback:
71
721. `algo`: which algorithm to use, it defaults to `'bcrypt'`;
732. `options`: additional options for the current algorithm, for bcrypt
74it defaults to `{cost: 10}.`.
75
76
77### Checking a password against a hash
78
79```js
80hashy.verify(password, hash, function (error, success) {
81 if (error) {
82 return console.error(err)
83 }
84
85 if (success) {
86 console.log('you are now authenticated!')
87 } else {
88 console.warn('invalid password!')
89 }
90})
91```
92
93### Getting information about a hash
94
95```js
96var info = hashy.getInfo(hash)
97```
98
99### Checking whether a hash is up to date
100
101As I said [earlier](#why-a-new-library), we must be able to check
102whether the hash is up to date, i.e. if it has been generated by the
103last algorithm available with the last set of options.
104
105```js
106if (hashy.needsRehash(hash)) {
107 // Rehash.
108}
109```
110
111It handles the optional `algo` and `options` parameters like
112[`hash()`](#creating-a-hash).
113
114### Changing default options.
115
116The default options for a given algorithm is available at `hashy.options[>algo<]`.
117
118```js
119// Sets the default cost for bcrypt to 12.
120hashy.options.bcrypt.cost = 12
121```
122
123## Using promises
124
125Same interface as above but without the callbacks!
126
127```javascript
128// Hashing.
129hashy.hash(password).then(function (hash) {
130 console.log('generated hash:' hash)
131})
132
133// Checking.
134hashy.verify(password, hash).then(function (success) {
135 if (success) {
136 console.log('you are now authenticated!')
137 } else {
138 console.warn('invalid password!')
139 }
140})
141
142```
143
144As you can see, you don't even have to handle errors if you don't want
145to!
146
147## Using generators
148
149**Note:** only available since node.js 0.12.
150
151Same interface as promises but much more similar to a synchronous
152code!
153
154```javascript
155// Hashing.
156Bluebird.coroutine(function * () {
157 var hash = yield hashy.hash(password)
158 console.log('generated hash:', hash)
159})()
160
161// Checking.
162Bluebird.coroutine(function * () {
163 if (yield hashy.verify(password, hash)) {
164 console.log('you are now authenticated!')
165 } else {
166 console.warn('invalid password!')
167 }
168})()
169```
170
171## Contributing
172
173Contributions are *very* welcome, either on the documentation or on
174the code.
175
176You may:
177
178- report any [issue](https://github.com/JsCommunity/hashy/issues)
179 you've encountered;
180- fork and create a pull request.
181
182## License
183
184Hashy is released under the [MIT
185license](https://en.wikipedia.org/wiki/MIT_License).