Skip to content

Instantly share code, notes, and snippets.

@oconnore oconnore/b32rand.js
Created Sep 28, 2014

Embed
What would you like to do?
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
You can’t perform that action at this time.