function randomStringWithLength( length ) { // random % characters.length produces too many characters at beginning of character range, because // beginning characters appear more times in random % characters.length than do characters at the end of the characters range. var endByteWithEqualProbability = 256 - (256 % characters.length); var numBytes = length * 2; var randomBytes = secureRandom.randomArray( numBytes ); // until we have a long enough result string... var s = ''; var byteIndex = -1; while ( s.length < length ) { ++ byteIndex; // ensure random byte is available if ( byteIndex >= randomBytes.length ) { randomBytes = secureRandom.randomArray( numBytes ); byteIndex = 0; } // select a random byte with flat probability distribution // (skip byte values that cause some characters to appear more than others) var randomByte = randomBytes[ byteIndex ]; if ( randomByte < endByteWithEqualProbability ) { var c = characters[ randomByte % characters.length ]; s += c; } } return s; }
function createTokenForSession() { return secureRandom.randomArray(100).map(code => code.toString(36)).join(''); }
function createSessionToken() { return secureRandom.randomArray(40).map(code => code.toString(16)).join(''); }
createSessionToken: function(userId, callback) { // new function declaration return secureRandom.randomArray(100).map(code => code.toString(36)).join('') // return secureRandom.randomArray(100).map(function(code) {return code.toString(36)}).join(''); },