-
Notifications
You must be signed in to change notification settings - Fork 0
/
09-secure-password-async.js
48 lines (39 loc) · 1.1 KB
/
09-secure-password-async.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
var db = require('./db')
var pwd = require('secure-password')({
memlimit: 1024 * 1024 * 128,
opslimit: 8
})
function register (username, password, cb) {
var ping = setInterval(function () {
console.log(' hash')
}, 100)
pwd.hash(password, function (err, hash) {
clearInterval(ping)
if (err) return cb(err)
db.set(username, hash)
return cb()
})
}
function login (username, password, cb) {
var savedHash = db.get(username)
if (savedHash == null) return false
var ping = setInterval(function () {
console.log(' verify')
}, 100)
return pwd.verify(password, savedHash, function (err, valid) {
clearInterval(ping)
if (err) return cb(err)
return cb(null, valid)
})
}
register('emilbayes', Buffer.from('secret'), function (err) {
if (err) return console.error(err)
login('emilbayes', Buffer.from('secret'), function (err, valid) {
if (err) return console.error(err)
console.log(valid)
login('emilbayes', Buffer.from('secret2'), function (err, valid) {
if (err) return console.error(err)
console.log(valid)
})
})
})