Changeset 28675


Ignore:
Timestamp:
Jun 11, 2014, 9:49:38 AM (10 years ago)
Author:
plg
Message:

bug 3082: increase randomness on generate_key

File:
1 edited

Legend:

Unmodified
Added
Removed
  • branches/2.6/include/functions_session.inc.php

    r26461 r28675  
    6363function generate_key($size)
    6464{
    65   global $conf;
    66 
    67   $md5 = md5(substr(microtime(), 2, 6));
    68   $init = '';
    69   for ( $i = 0; $i < strlen( $md5 ); $i++ )
    70   {
    71     if ( is_numeric( $md5[$i] ) ) $init.= $md5[$i];
    72   }
    73   $init = substr( $init, 0, 8 );
    74   mt_srand( $init );
    75   $key = '';
    76   for ( $i = 0; $i < $size; $i++ )
    77   {
    78     $c = mt_rand( 0, 2 );
    79     if ( $c == 0 )      $key .= chr( mt_rand( 65, 90 ) );
    80     else if ( $c == 1 ) $key .= chr( mt_rand( 97, 122 ) );
    81     else                $key .= mt_rand( 0, 9 );
    82   }
    83   return $key;
     65  if (
     66    is_callable('openssl_random_pseudo_bytes')
     67    and !(version_compare(PHP_VERSION, '5.3.4') < 0 and defined('PHP_WINDOWS_VERSION_MAJOR'))
     68    )
     69  {
     70    return substr(
     71      str_replace(
     72        array('+', '/'),
     73        '',
     74        base64_encode(openssl_random_pseudo_bytes($size))
     75        ),
     76      0,
     77      $size
     78      );
     79  }
     80  else
     81  {
     82    $alphabet = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789';
     83    $l = strlen($alphabet)-1;
     84    $key = '';
     85    for ($i=0; $i<$size; $i++)
     86    {
     87      $key.= $alphabet[mt_rand(0, $l)];
     88    }
     89    return $key;
     90  }
    8491}
    8592
Note: See TracChangeset for help on using the changeset viewer.