Skip to content

Instantly share code, notes, and snippets.

@droidScriptKiddy
Forked from ValeriiVasin/password.js
Last active September 15, 2019 01:54
Show Gist options
  • Save droidScriptKiddy/95b8174f55228f9d3f1941965c023b1f to your computer and use it in GitHub Desktop.
Save droidScriptKiddy/95b8174f55228f9d3f1941965c023b1f to your computer and use it in GitHub Desktop.
Reading password node.js
// Get a password from the console
function getPassword(done) {
process.stdin.resume();
process.stdin.setEncoding('utf8');
process.stdin.setRawMode(true);
var password = '';
process.stdout.write('Password: ');
process.stdin.on('data', function(ch) {
ch = ch + '';
// backspace
if (ch.charCodeAt(0) === 127) {
password = password.slice(0, -1);
return;
}
switch (ch) {
case '\n':
case '\r':
case '\u0004':
// They've finished typing their password
process.stdin.setRawMode(false);
process.stdin.pause();
process.stdout.write('\n');
done(password);
break;
case '\u0003':
// Ctrl C
throw new Error('Cancelled');
default:
// More passsword characters
password += ch;
break;
}
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment