resources: Strip '$' and 'mw' from file closures
[lhc/web/wiklou.git] / resources / src / mediawiki.user.js
index b2985d1..e41ed58 100644 (file)
@@ -2,8 +2,8 @@
  * @class mw.user
  * @singleton
  */
-/* global Uint32Array */
-( function ( mw, $ ) {
+/* global Uint16Array */
+( function () {
        var userInfoPromise, pageviewRandomId;
 
        /**
                 * mobile usages of this code is probably higher.
                 *
                 * Rationale:
-                * We need about 64 bits to make sure that probability of collision
-                * on 500 million (5*10^8) is <= 1%
-                * See https://en.wikipedia.org/wiki/Birthday_problem#Probability_table
+                * We need about 80 bits to make sure that probability of collision
+                * on 155 billion  is <= 1%
                 *
-                * @return {string} 64 bit integer in hex format, padded
+                * See https://en.wikipedia.org/wiki/Birthday_attack#Mathematics
+                * n(p;H) = n(0.01,2^80)= sqrt (2 * 2^80 * ln(1/(1-0.01)))
+
+                * @return {string} 80 bit integer in hex format, padded
                 */
                generateRandomSessionId: function () {
                        var rnds, i,
-                               hexRnds = new Array( 2 ),
                                // Support: IE 11
                                crypto = window.crypto || window.msCrypto;
 
-                       if ( crypto && crypto.getRandomValues && typeof Uint32Array === 'function' ) {
-                               // Fill an array with 2 random values, each of which is 32 bits.
-                               // Note that Uint32Array is array-like but does not implement Array.
-                               rnds = new Uint32Array( 2 );
+                       if ( crypto && crypto.getRandomValues && typeof Uint16Array === 'function' ) {
+                               // Fill an array with 5 random values, each of which is 16 bits.
+                               // Note that Uint16Array is array-like but does not implement Array.
+                               rnds = new Uint16Array( 5 );
                                crypto.getRandomValues( rnds );
                        } else {
-                               rnds = [
-                                       Math.floor( Math.random() * 0x100000000 ),
-                                       Math.floor( Math.random() * 0x100000000 )
-                               ];
-                       }
-                       // Convert number to a string with 16 hex characters
-                       for ( i = 0; i < 2; i++ ) {
-                               // Add 0x100000000 before converting to hex and strip the extra character
-                               // after converting to keep the leading zeros.
-                               hexRnds[ i ] = ( rnds[ i ] + 0x100000000 ).toString( 16 ).slice( 1 );
+                               rnds = new Array( 5 );
+                               // 0x10000 is 2^16 so the operation below will return a number
+                               // between 2^16 and zero
+                               for ( i = 0; i < 5; i++ ) {
+                                       rnds[ i ] = Math.floor( Math.random() * 0x10000 );
+                               }
                        }
 
+                       // Convert the 5 16bit-numbers into 20 characters (4 hex per 16 bits).
                        // Concatenation of two random integers with entropy n and m
-                       // returns a string with entropy n+m if those strings are independent
-                       return hexRnds.join( '' );
+                       // returns a string with entropy n+m if those strings are independent.
+                       // Tested that below code is faster than array + loop + join.
+                       return ( rnds[ 0 ] + 0x10000 ).toString( 16 ).slice( 1 ) +
+                               ( rnds[ 1 ] + 0x10000 ).toString( 16 ).slice( 1 ) +
+                               ( rnds[ 2 ] + 0x10000 ).toString( 16 ).slice( 1 ) +
+                               ( rnds[ 3 ] + 0x10000 ).toString( 16 ).slice( 1 ) +
+                               ( rnds[ 4 ] + 0x10000 ).toString( 16 ).slice( 1 );
                },
 
                /**
@@ -76,7 +79,7 @@
                 * cached within this class (also known as a page view token).
                 *
                 * @since 1.32
-                * @return {string} 64 bit integer in hex format, padded
+                * @return {string} 80 bit integer in hex format, padded
                 */
                getPageviewToken: function () {
                        if ( !pageviewRandomId ) {
         */
        mw.log.deprecate( mw.user, 'stickyRandomId', mw.user.getPageviewToken, 'Please use getPageviewToken instead' );
 
-}( mediaWiki, jQuery ) );
+}() );