-
-
Save sammyd/2358848 to your computer and use it in GitHub Desktop.
Password collection from the console for node.js
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
var get_pass = function(callback) { | |
console.info("Enter password:") | |
var stdin = process.openStdin() | |
stdin.setEncoding('utf8') | |
var tty = require('tty') | |
tty.setRawMode(true) | |
var password = "" | |
stdin.on("data", function (c) { | |
switch (c) { | |
case "\n": case "\r": case "\u0004": | |
tty.setRawMode(false) | |
console.log("Password received") | |
stdin.pause() | |
callback(password) | |
break | |
case "\u0003": | |
process.exit() | |
break | |
default: | |
password += c | |
break | |
} | |
}) | |
} |
Author
sammyd
commented
Apr 11, 2012
- Added the setEncoding to ensure characters instead of a buffer (replaces c = "" + c)
- Wrapped in a function expecting a callback with the collected password
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment