Skip to content

Instantly share code, notes, and snippets.

@joaquimserafim
Created March 19, 2014 00:28
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save joaquimserafim/9633125 to your computer and use it in GitHub Desktop.
Save joaquimserafim/9633125 to your computer and use it in GitHub Desktop.
save pwd in terminal
#!/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