includes/rijndael.php
changeset 801 eb8b23f11744
parent 613 c08670a77871
child 832 7152ca0a0ce9
equal deleted inserted replaced
800:9cdfe82c56cd 801:eb8b23f11744
  1656    * Generates a random key suitable for encryption
  1656    * Generates a random key suitable for encryption
  1657    * @param int $len the length of the key, in bytes
  1657    * @param int $len the length of the key, in bytes
  1658    * @return string a BINARY key
  1658    * @return string a BINARY key
  1659    */
  1659    */
  1660   
  1660   
  1661   function randkey($len = 32)
  1661   static function randkey($len = 32)
  1662   {
  1662   {
  1663     $key = '';
       
  1664     for($i=0;$i<$len;$i++)
       
  1665     {
       
  1666       $key .= chr(mt_rand(0, 255));
       
  1667     }
       
  1668     if ( @file_exists('/dev/urandom') && @is_readable('/dev/urandom') )
  1663     if ( @file_exists('/dev/urandom') && @is_readable('/dev/urandom') )
  1669     {
  1664     {
  1670       // Let's use something a little more secure
  1665       // Let's use something a little more secure
  1671       $ur = @fopen('/dev/urandom', 'r');
  1666       $ur = @fopen('/dev/urandom', 'r');
  1672       if ( !$ur )
  1667       if ( !$ur )
  1673         return $key;
  1668         return self::randkey_safe($len);
  1674       $ukey = @fread($ur, $len);
  1669       $ukey = @fread($ur, $len);
  1675       fclose($ur);
  1670       fclose($ur);
  1676       if ( strlen($ukey) != $len )
  1671       if ( strlen($ukey) != $len )
  1677         return $key;
  1672         return self::randkey_safe($len);
  1678       return $ukey;
  1673       return $ukey;
       
  1674     }
       
  1675     return self::randkey_safe($len);
       
  1676   }
       
  1677   
       
  1678   static function randkey_safe($len = 32)
       
  1679   {
       
  1680     $key = '';
       
  1681     for($i=0;$i<$len;$i++)
       
  1682     {
       
  1683       $key .= chr(mt_rand(0, 255));
  1679     }
  1684     }
  1680     return $key;
  1685     return $key;
  1681   }
  1686   }
  1682   
  1687   
  1683   function gen_readymade_key()
  1688   function gen_readymade_key()