forked from adonisjs/core
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
71 lines (66 loc) · 1.45 KB
/
index.js
File metadata and controls
71 lines (66 loc) · 1.45 KB
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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
'use strict'
/**
* adonis-framework
*
* (c) Harminder Virk <virk@adonisjs.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
const bcrypt = require('bcryptjs')
/**
* Create and verify hash values using Bcrypt as underlaying
* algorithm.
* @module Hash
*/
let Hash = exports = module.exports = {}
/**
* hash a value with given number of rounds
*
* @method make
* @param {String} value - value to hash
* @param {Number} [rounds=10] - number of rounds to be used for creating hash
*
* @return {Promise}
*
* @public
*
* @example
* yield Hash.make('somepassword')
* yield Hash.make('somepassword', 5)
*/
Hash.make = function (value, rounds) {
rounds = rounds || 10
return new Promise(function (resolve, reject) {
bcrypt.hash(value, rounds, function (error, hash) {
if (error) {
return reject(error)
}
resolve(hash)
})
})
}
/**
* verifies a given value against hash value
*
* @method verify
* @param {String} value - Plain value
* @param {String} hash - Previously hashed value
*
* @return {Promise}
*
* @public
*
* @example
* yield Hash.verify('plainpassword', 'hashpassword')
*/
Hash.verify = function (value, hash) {
return new Promise(function (resolve, reject) {
bcrypt.compare(value, hash, function (error, response) {
if (error) {
return reject(error)
}
resolve(response)
})
})
}