-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcs142password.js
34 lines (31 loc) · 1010 Bytes
/
cs142password.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
var crypto = require('crypto');
/*
* Return a salted and hashed password entry from a
* clear text password.
* @param {string} clearTextPassword
* @return {object} passwordEntry
* where passwordEntry is an object with two string
* properties:
* salt - The salt used for the password.
* hash - The sha1 hash of the password and salt
*/
function makePasswordEntry(clearTextPassword) {
let salt = crypto.randomBytes(8);
let hash = crypto.createHash("sha1").update(clearTextPassword + salt).digest("hex");
return {
salt: salt,
hash: hash
}
}
/*
* Return true if the specified clear text password
* and salt generates the specified hash.
* @param {string} hash
* @param {string} salt
* @param {string} clearTextPassword
* @return {boolean}
*/
function doesPasswordMatch(hash, salt, clearTextPassword) {
return crypto.createHash("sha1").update(clearTextPassword + salt).digest("hex") === hash;
}
module.exports = {makePasswordEntry, doesPasswordMatch}