Created
September 28, 2014 01:56
-
-
Save oconnore/bd933328edb83cb27866 to your computer and use it in GitHub Desktop.
Random Base32 encoded strings
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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