Get password from input using node.js

前端 未结 4 1396
梦谈多话
梦谈多话 2021-02-02 09:29

How to get password from input using node.js? Which means you should not output password entered in console.

相关标签:
4条回答
  • 2021-02-02 09:45

    Update 2015 Dec 13: readline has replaced process.stdin and node_stdio was removed from Node 0.5.10.

    var BACKSPACE = String.fromCharCode(127);
    
    
    // Probably should use readline
    // https://nodejs.org/api/readline.html
    function getPassword(prompt, callback) {
        if (prompt) {
          process.stdout.write(prompt);
        }
    
        var stdin = process.stdin;
        stdin.resume();
        stdin.setRawMode(true);
        stdin.resume();
        stdin.setEncoding('utf8');
    
        var password = '';
        stdin.on('data', function (ch) {
            ch = ch.toString('utf8');
    
            switch (ch) {
            case "\n":
            case "\r":
            case "\u0004":
                // They've finished typing their password
                process.stdout.write('\n');
                stdin.setRawMode(false);
                stdin.pause();
                callback(false, password);
                break;
            case "\u0003":
                // Ctrl-C
                callback(true);
                break;
            case BACKSPACE:
                password = password.slice(0, password.length - 1);
                process.stdout.clearLine();
                process.stdout.cursorTo(0);
                process.stdout.write(prompt);
                process.stdout.write(password.split('').map(function () {
                  return '*';
                }).join(''));
                break;
            default:
                // More passsword characters
                process.stdout.write('*');
                password += ch;
                break;
            }
        });
    }
    
    getPassword('Password: ');
    
    0 讨论(0)
  • 2021-02-02 09:47

    You can use the read module (disclosure: written by me) for this:

    In your shell:

    npm install read
    

    Then in your JS:

    var read = require('read')
    read({ prompt: 'Password: ', silent: true }, function(er, password) {
      console.log('Your password is: %s', password)
    })
    
    0 讨论(0)
  • 2021-02-02 09:48

    To do this I found this excellent Google Group post

    Which contains the following snippet:

    var stdin = process.openStdin()
      , stdio = process.binding("stdio")
    stdio.setRawMode()
    
    var password = ""
    stdin.on("data", function (c) {
      c = c + ""
      switch (c) {
        case "\n": case "\r": case "\u0004":
          stdio.setRawMode(false)
          console.log("you entered: "+password)
          stdin.pause()
          break
        case "\u0003":
          process.exit()
          break
        default:
          password += c
          break
      }
    })
    
    0 讨论(0)
  • 2021-02-02 09:55

    Here is my tweaked version of nailer's from above, updated to get a callback and for node 0.8 usage:

    /**
     * Get a password from stdin.
     *
     * Adapted from <http://stackoverflow.com/a/10357818/122384>.
     *
     * @param prompt {String} Optional prompt. Default 'Password: '.
     * @param callback {Function} `function (cancelled, password)` where
     *      `cancelled` is true if the user aborted (Ctrl+C).
     *
     * Limitations: Not sure if backspace is handled properly.
     */
    function getPassword(prompt, callback) {
        if (callback === undefined) {
            callback = prompt;
            prompt = undefined;
        }
        if (prompt === undefined) {
            prompt = 'Password: ';
        }
        if (prompt) {
            process.stdout.write(prompt);
        }
    
        var stdin = process.stdin;
        stdin.resume();
        stdin.setRawMode(true);
        stdin.resume();
        stdin.setEncoding('utf8');
    
        var password = '';
        stdin.on('data', function (ch) {
            ch = ch + "";
    
            switch (ch) {
            case "\n":
            case "\r":
            case "\u0004":
                // They've finished typing their password
                process.stdout.write('\n');
                stdin.setRawMode(false);
                stdin.pause();
                callback(false, password);
                break;
            case "\u0003":
                // Ctrl-C
                callback(true);
                break;
            default:
                // More passsword characters
                process.stdout.write('*');
                password += ch;
                break;
            }
        });
    }
    
    0 讨论(0)
提交回复
热议问题