Generating Passwords with node.js

For a few years now, I've been using the same silly random password generator from a coworker of mine. Since I use 1Password for password management, I'm frequently creating new random passwords. While a feature exists within 1Password for generating new passwords, I usually find comfort in doing this from Terminal. When my coworker's server was down for a few days, I temporarily reverted to some bash-foo:

date +%s | sha256sum | base64 | head -c 13 ; echo
tr -cd '[:alnum:]' < /dev/urandom | fold -w13 | head -n1
< /dev/urandom tr -dc _A-Z-a-z-0-9 | head -c 13 ; echo

While these examples are super handy, I wanted to use this as an opportunity to play around with node.js. This simple utility can generate 13 character random passwords using 2 node modules, express and request. Here is the meat of it:

var express, http, make_passwd;

express = require('express');

http = express();
http.use(express.logger());

make_passwd = function(n, a) {
  var index = (Math.random() * (a.length - 1)).toFixed(0);
  return n > 0 ? a[index] + make_passwd(n - 1, a) : '';
};

http.get('/', function(request, response) {
  var password;

  password = make_passwd(13, 'qwertyuiopasdfghjklzxcvbnmQWERTYUIOPASDFGHJKLZXCVBNM1234567890');

  response.type('text/plain');
  response.send(password);
});

http.listen(process.env.PORT);

I've published a working version to Heroku. You can find the full repository on GitHub. Give it a shot:

curl –s http://passwd.tjstein.com 2>/dev/null | pbcopy