-
Notifications
You must be signed in to change notification settings - Fork 2
/
genpass.js
61 lines (49 loc) · 1.51 KB
/
genpass.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
49
50
51
52
53
54
55
56
57
58
59
60
61
const crypto = require('crypto')
const readline = require('readline');
function pbkdf(keyStr, saltStr, iterations, keyLen, digestAlgo = "SHA-512") {
return new Promise( (resolve, reject) => {
crypto.pbkdf2(keyStr, saltStr, iterations, keyLen, digestAlgo.replace(/-/g, '').toLowerCase(), (err, key) => {
if (err) {
reject(err)
} else {
resolve(key)
}
})
})
}
async function generatePass(masterPassword, salt, version = 0, len = 40, alphabet, purpose) {
const buffer = await pbkdf(purpose + version + masterPassword, salt, 800000, len, "SHA-512")
return Array.from(new Uint8Array(buffer)).map((x) => alphabet.charAt(Math.floor((x / 256) * alphabet.length))).join("")
}
let [node, script, purpose = null, masterPass = null, salt = 'salt', version = 0, length = 40, alphabet = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789_-%$#.!@^&*()"] = process.argv
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout
});
let tasks = [ done ]
function nextTask() {
tasks.shift()()
}
function done() {
rl.close();
generatePass(masterPass, salt, version, length, alphabet, purpose).then((generatedPassword) => console.log(generatedPassword))
}
function getPurpose() {
rl.question('Purpose:', (answer) => {
purpose = answer
nextTask()
})
}
function getPassword() {
rl.question('Password:', (answer) => {
masterPass = answer
nextTask()
})
}
if (masterPass === null) {
tasks.unshift(getPassword)
}
if (purpose === null) {
tasks.unshift(getPurpose)
}
nextTask()