-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsalt.js
42 lines (28 loc) · 1.09 KB
/
salt.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
const { scryptSync, randomBytes, timingSafeEqual } = require('crypto');
//Salting is use as a proof of work algorithm used in crypto
function signup(email, password) {
const salt = randomBytes(16).toString('hex');
const hashedPassword = scryptSync(password, salt, 64).toString('hex');
const user = { email, password: `${salt}:${hashedPassword}` }
users.push(user);
return user
}
function login(email, password) {
const user = users.find(v => v.email === email);
const [salt, key] = user.password.split(':');
const hashedBuffer = scryptSync(password, salt, 64);
console.log(hashedBuffer, "hashedBuffer")
const keyBuffer = Buffer.from(key, 'hex');
console.log(keyBuffer, "Key");
const match = timingSafeEqual(hashedBuffer, keyBuffer); //This prevents a timing attack
if (match) {
return 'login success!'
} else {
return 'login fail!'
}
}
const users = [];
const user = signup('foo@bar.com', 'pa$$word');
console.log(user, "This is user")
const result = login('foo@bar.com', 'pa$$word')
console.log(result, "Result")