forked from sen-su/couch-pwd
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
49 lines (42 loc) · 1.41 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
var crypto = require('crypto');
class CouchPwd {
constructor(iterations = 10, keylen = 20, size = 16, encoding = 'hex', digest = 'SHA1') {
this.iterations = iterations;
this.keylen = keylen;
this.size = size;
this.encoding = encoding;
this.digest = digest;
}
/**
* If `pwd` and `salt` are provided:
* - creates a hash from both
* - returns `hash`
* If just `pwd` is provided:
* - generates `salt` and hashes
* - returns `salt` and `hash`
*/
hash(pwd, salt, cb = undefined) {
if (arguments.length === 3) {
// hash('secret', 'salt', function(err, hash) {})
if (!pwd) return cb(new Error('password missing'));
if (!salt) return cb(new Error('salt missing'));
crypto.pbkdf2(pwd, salt, this.iterations, this.keylen, this.digest, (err, hash) => {
if (err) return cb(err);
cb(null, hash.toString(this.encoding));
});
} else {
// hash('secret', function(err, salt, hash) {})
cb = salt;
if (!pwd) return cb(new Error('password missing'));
crypto.randomBytes(this.size, (err, salt) => {
if (err) return cb(err);
const saltStr = salt.toString('hex');
crypto.pbkdf2(pwd, saltStr, this.iterations, this.keylen, this.digest, (err, hash) => {
if (err) return cb(err);
cb(null, saltStr, hash.toString(this.encoding));
});
});
}
}
}
module.exports = CouchPwd;