Share randomString method.

This simply shares a common implementation and makes no judgements on the validity of its use. The string created is not a secure random number, so some uses of it may not be applicable.
This commit is contained in:
Chad Weider 2012-01-28 17:38:23 -08:00
parent 34edba3adf
commit 363e168561
12 changed files with 37 additions and 61 deletions

View file

@ -48,19 +48,7 @@ var padutils = require('/pad_utils').padutils;
var createCookie = require('/pad_utils').createCookie;
var readCookie = require('/pad_utils').readCookie;
function randomString()
{
var chars = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
var string_length = 20;
var randomstring = '';
for (var i = 0; i < string_length; i++)
{
var rnum = Math.floor(Math.random() * chars.length);
randomstring += chars.substring(rnum, rnum + 1);
}
return "t." + randomstring;
}
var randomString = require('/pad_utils').randomString;
function getParams()
{
@ -184,7 +172,7 @@ function handshake()
var token = readCookie("token");
if (token == null)
{
token = randomString();
token = "t." + randomString();
createCookie("token", token, 60);
}

View file

@ -20,6 +20,23 @@
* limitations under the License.
*/
/**
* Generates a random String with the given length. Is needed to generate the Author, Group, readonly, session Ids
*/
function randomString(len)
{
var chars = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
var randomstring = '';
len = len || 20
for (var i = 0; i < len; i++)
{
var rnum = Math.floor(Math.random() * chars.length);
randomstring += chars.substring(rnum, rnum + 1);
}
return randomstring;
}
function createCookie(name, value, days, path)
{
if (days)
@ -510,6 +527,7 @@ padutils.setupGlobalExceptionHandler = setupGlobalExceptionHandler;
padutils.binarySearch = require('/ace2_common').binarySearch;
exports.randomString = randomString;
exports.createCookie = createCookie;
exports.readCookie = readCookie;
exports.padutils = padutils;

View file

@ -28,17 +28,7 @@ require('/undo-xpopup');
var createCookie = require('/pad_utils').createCookie;
var readCookie = require('/pad_utils').readCookie;
function randomString() {
var chars = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
var string_length = 20;
var randomstring = '';
for (var i=0; i<string_length; i++) {
var rnum = Math.floor(Math.random() * chars.length);
randomstring += chars.substring(rnum,rnum+1);
}
return "t." + randomstring;
}
var randomString = require('/pad_utils').randomString;
var socket, token, padId, export_links;
@ -59,7 +49,7 @@ function init() {
token = readCookie("token");
if(token == null)
{
token = randomString();
token = "t." + randomString();
createCookie("token", token, 60);
}