Skip to content

Instantly share code, notes, and snippets.

@sammyd
Forked from isaacs/enter-password.js
Created April 11, 2012 11:50
Show Gist options
  • Save sammyd/2358848 to your computer and use it in GitHub Desktop.
Save sammyd/2358848 to your computer and use it in GitHub Desktop.
Password collection from the console for node.js
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
}
})
}
@sammyd
Copy link
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