Skip to content

Instantly share code, notes, and snippets.

@myndzi
Created January 21, 2014 17:30
Show Gist options
  • Save myndzi/8544346 to your computer and use it in GitHub Desktop.
Save myndzi/8544346 to your computer and use it in GitHub Desktop.
var Promise = require('bluebird'),
ie = require('int-encoder'),
crypto = require('crypto');
var alpha = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ`~!@#$%^&*()[{]}/?=+\\|-_\'",<.>;:';
function getRandomPass(len) {
var deferred = Promise.defer();
var bytes = Math.ceil(Math.log(alpha.length)/Math.log(256)*len);
crypto.randomBytes(bytes, function (ex, buf) {
ie.alphabet(alpha);
var hex = buf.toString('hex'),
num = ie.encode(hex, 16);
// cut off the MSB, it won't have the full range available
deferred.resolve(num.slice(1));
});
return deferred.promise;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment