Created
March 19, 2014 00:28
-
-
Save joaquimserafim/9633125 to your computer and use it in GitHub Desktop.
save pwd in terminal
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env node | |
var stdin = process.openStdin(); | |
// get a password from the console & print stars while the user types | |
function setPassword () { | |
process.stdin.resume(); | |
process.stdin.setEncoding('utf8'); | |
process.stdin.setRawMode(true); | |
var password = ''; | |
var keepFirstpwd = ''; | |
var round = 0; | |
process.stdout.write('Set new password: '); | |
process.stdin.on('data', function (char) { | |
switch (char) { | |
case '\n': | |
case '\r': | |
case '\u0004': | |
if (++round === 2) { | |
process.stdin.setRawMode(false); | |
if (password === keepFirstpwd ) | |
console.log('\npasswd: OK'); | |
else | |
console.log('\npasswd: passwords do not match, check again'); | |
stdin.pause(); | |
return; | |
} | |
keepFirstpwd = password; | |
password = ''; | |
process.stdout.write('\nConfirm new password: '); | |
break; | |
case '\u0003': | |
console.log('\npasswd: Cancelled'); | |
process.exit(); | |
break; | |
default: | |
// hide passsword characters | |
process.stdout.write('*'); | |
password += char; | |
break; | |
} | |
}); | |
} | |
setPassword(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment