-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcryptPractice.js
More file actions
28 lines (22 loc) · 922 Bytes
/
cryptPractice.js
File metadata and controls
28 lines (22 loc) · 922 Bytes
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
const crypt = require("bcrypt")
const {timingSafeEqual,scryptSync,randomBytes} = require("crypto")
const userDB={}
const signup = (username,passwd)=> {
const salt = randomBytes(16).toString("hex")
const hashedPasswd = scryptSync(passwd,salt,64).toString("hex")
userDB.username=username
userDB.passwd = `${salt}:${hashedPasswd}`
}
const login= (username,passwd)=>{
const usernameDB= userDB.username
if(username===usernameDB){
const [salt, passwdDB] = userDB.passwd.split(":")
const apparentPasswd = scryptSync(passwd,salt,64).toString("hex")
const match = timingSafeEqual(Buffer.from(apparentPasswd,'hex'),Buffer.from(passwdDB,'hex'))
if(match){
console.log("Log in successful")
}else{console.log('log in was unsuccsseful')}
}else{console.log('invalid username')}
}
signup('willow','banana')
login('willow','banan')