-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathgenerateSaltAndHash
executable file
·80 lines (70 loc) · 2.16 KB
/
generateSaltAndHash
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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
#!/usr/bin/env node
const crypto = require('crypto')
function parsePBKDF2Iterations (Value) {
Value = Value.trim()
if (Value === '') { throw 'no PBKDF2 iteration <count> given' }
let Count = parseInt(Value,10)
if (isFinite(Count) && (Count > 0)) {
return Count
} else {
throw 'invalid PBKDF2 iteration <count> given'
}
}
const Arguments = require('commander')
Arguments
.usage('[options]')
.option('--pbkdf2-iterations <count>', 'PBKDF2 iteration count', parsePBKDF2Iterations)
const PBKDF2Iterations = Arguments.opts().pbkdf2Iterations || 100000
const stdin = process.stdin
const stdout = process.stdout
function readPassword (Prompt, CallBack) {
if (Prompt != null) {
stdout.write(Prompt)
}
stdin.resume()
stdin.setRawMode(true)
stdin.resume()
stdin.setEncoding('utf8')
let Password = ''
stdin.on('data', function (readChar) {
readChar = readChar.toString('utf8')
switch (readChar) {
case '\n':
case '\r':
case '\u0004':
stdout.write('\n')
stdin.setRawMode(false)
stdin.pause()
return CallBack(null,Password)
case '\u0003': // ctrl-c
return CallBack(new Error('aborted'))
case '\u007f': // backspace
Password = Password.slice(0, Password.length-1)
stdout.clearLine()
stdout.cursorTo(0)
if (Prompt != null) { stdout.write(Prompt) }
stdout.write(Password.split('').map(() => '*').join(''))
break
default:
stdout.write('*')
Password += readChar
}
})
}
readPassword('enter your password: ', (Error,Password) => {
if (Error == null) {
let PasswordSalt = crypto.randomBytes(16)
let PasswordHash = crypto.pbkdf2Sync(
Password, PasswordSalt, PBKDF2Iterations, 64, 'sha512'
)
stdout.write('"Salt":"' + PasswordSalt.toString('hex') + '"\n')
stdout.write('"Hash":"' + PasswordHash.toString('hex') + '"\n')
} else {
if (Error.message === 'aborted') {
stdout.write('\n')
process.exit()
} else {
throw Error
}
}
})