mirror of
https://github.com/ether/etherpad-lite.git
synced 2025-04-22 00:16:15 -04:00
index.html: generate unique pad names
Etherpad-lite relies on the user's browser to generate a random pad name, but the current solution is not safe against collisions. In order to generate unique pad names, the following modifications are made: * use a PRNG instead of Math.random() and ensure uniform distribution when selecting chars. * choose the pad name length to achieve a specific number of bits of security. Closes: #3516
This commit is contained in:
parent
7df26840cb
commit
39fbc37dd8
1 changed files with 14 additions and 3 deletions
|
@ -187,12 +187,23 @@
|
||||||
|
|
||||||
function randomPadName()
|
function randomPadName()
|
||||||
{
|
{
|
||||||
var chars = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
|
// the number of distinct chars (64) is chosen to ensure that
|
||||||
var string_length = 10;
|
// the selection will be uniform when using the PRNG below
|
||||||
|
var chars = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz-_";
|
||||||
|
// the length of the pad name is chosen to get 120-bit security:
|
||||||
|
// log2(64^20) = 120
|
||||||
|
var string_length = 20;
|
||||||
|
// make room for 8-bit integer values that span from 0 to 255.
|
||||||
|
var randomarray = new Uint8Array(string_length);
|
||||||
|
// use browser's PRNG to generate a "unique" sequence
|
||||||
|
var cryptoObj = window.crypto || window.msCrypto; // for IE 11
|
||||||
|
cryptoObj.getRandomValues(randomarray);
|
||||||
var randomstring = '';
|
var randomstring = '';
|
||||||
for (var i = 0; i < string_length; i++)
|
for (var i = 0; i < string_length; i++)
|
||||||
{
|
{
|
||||||
var rnum = Math.floor(Math.random() * chars.length);
|
// instead of writing "Math.floor(randomarray[i]/256*64)"
|
||||||
|
// we can save some cycles.
|
||||||
|
var rnum = Math.floor(randomarray[i]/4);
|
||||||
randomstring += chars.substring(rnum, rnum + 1);
|
randomstring += chars.substring(rnum, rnum + 1);
|
||||||
}
|
}
|
||||||
return randomstring;
|
return randomstring;
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue