-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
39 lines (36 loc) · 1.38 KB
/
index.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
var sha256crypt = require('./src/sha256crypt').cwrap('sha256_crypt', 'string', ['string', 'string']);
function verify(password, rounds, salt, derived) {
var rounds_n = parseInt(rounds, 10);
if (rounds_n < 1000) {
throw new Error('Rounds must be at least 1000.');
}
if (rounds_n > 999999999) {
throw new Error('Rounds must be at most 999999999.');
}
if (!(/^[\.\/0-9A-Za-z]{0,16}$/.test(salt))) {
throw new Error('Salt must contain characters only from ./0-9A-Za-z and has a length between 0 and 16 characters (inclusive)');
}
const padded_salt = '$5$rounds=' + rounds_n.toFixed(0) + '$' + salt;
const hash = sha256crypt(password, padded_salt);
const verify = `${padded_salt}$${derived}`;
return (hash === verify);
}
function hash(password, rounds, salt) {
var rounds_n = parseInt(rounds, 10);
if (rounds_n < 1000) {
throw new Error('Rounds must be at least 1000.');
}
if (rounds_n > 999999999) {
throw new Error('Rounds must be at most 999999999.');
}
if (!(/^[\.\/0-9A-Za-z]{0,16}$/.test(salt))) {
throw new Error('Salt must contain characters only from ./0-9A-Za-z');
}
const padded_salt = '$5$rounds=' + rounds_n.toFixed(0) + '$' + salt;
const hash = sha256crypt(password, padded_salt);
return hash.split('$').pop();
}
module.exports = {
verify: verify,
hash: hash
}