Skip to content

Instantly share code, notes, and snippets.

@oconnore
Created September 28, 2014 01:56
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save oconnore/bd933328edb83cb27866 to your computer and use it in GitHub Desktop.
Save oconnore/bd933328edb83cb27866 to your computer and use it in GitHub Desktop.
Random Base32 encoded strings
var b32 = require('thirty-two');
var crypto = require('crypto');
module.exports.randomStr = function(bits) {
bits = (typeof bits === 'number' && bits >= 80) ? bits : 128;
var randb32 = b32.encode(
crypto.randomBytes(Math.ceil(bits / 8))).toString('utf8');
var idx = randb32.indexOf('=');
return randb32.substr(0, idx > 0 ? idx : randb32.length);
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment