Namespaces

  • Latte
    • Loaders
    • Macros
    • Runtime
  • Nette
    • Application
      • Responses
      • Routers
      • UI
    • Bridges
      • ApplicationDI
      • ApplicationLatte
      • ApplicationTracy
      • CacheDI
      • CacheLatte
      • DatabaseDI
      • DatabaseTracy
      • DITracy
      • FormsDI
      • FormsLatte
      • Framework
      • HttpDI
      • HttpTracy
      • MailDI
      • ReflectionDI
      • SecurityDI
      • SecurityTracy
    • Caching
      • Storages
    • ComponentModel
    • Database
      • Conventions
      • Drivers
      • Reflection
      • Table
    • DI
      • Config
        • Adapters
      • Extensions
    • Diagnostics
    • Forms
      • Controls
      • Rendering
    • Http
    • Iterators
    • Latte
    • Loaders
    • Localization
    • Mail
    • Neon
    • PhpGenerator
    • Reflection
    • Security
    • Templating
    • Utils
  • NetteModule
  • none
  • Tracy
    • Bridges
      • Nette

Classes

  • ArrayHash
  • ArrayList
  • Arrays
  • Callback
  • DateTime
  • FileSystem
  • Finder
  • Html
  • Image
  • Json
  • LimitedScope
  • MimeTypeDetector
  • ObjectMixin
  • Paginator
  • Random
  • Strings
  • TokenIterator
  • Tokenizer
  • Validators

Interfaces

  • IHtmlString

Exceptions

  • AssertionException
  • ImageException
  • JsonException
  • RegexpException
  • TokenizerException
  • UnknownImageFileException
  • Overview
  • Namespace
  • Class
  • Tree
  • Deprecated
 1: <?php
 2: 
 3: /**
 4:  * This file is part of the Nette Framework (http://nette.org)
 5:  * Copyright (c) 2004 David Grudl (http://davidgrudl.com)
 6:  */
 7: 
 8: namespace Nette\Utils;
 9: 
10: use Nette;
11: 
12: 
13: /**
14:  * Secure random string generator.
15:  *
16:  * @author     David Grudl
17:  */
18: class Random
19: {
20: 
21:     /**
22:      * Generate random string.
23:      * @param  int
24:      * @param  string
25:      * @return string
26:      */
27:     public static function generate($length = 10, $charlist = '0-9a-z')
28:     {
29:         $charlist = str_shuffle(preg_replace_callback('#.-.#', function($m) {
30:             return implode('', range($m[0][0], $m[0][2]));
31:         }, $charlist));
32:         $chLen = strlen($charlist);
33: 
34:         if (function_exists('openssl_random_pseudo_bytes')
35:             && (PHP_VERSION_ID >= 50400 || !defined('PHP_WINDOWS_VERSION_BUILD')) // slow in PHP 5.3 & Windows
36:         ) {
37:             $rand3 = openssl_random_pseudo_bytes($length);
38:         }
39:         if (empty($rand3) && function_exists('mcrypt_create_iv') && (PHP_VERSION_ID >= 50307 || !defined('PHP_WINDOWS_VERSION_BUILD'))) { // PHP bug #52523
40:             $rand3 = mcrypt_create_iv($length, MCRYPT_DEV_URANDOM);
41:         }
42:         if (empty($rand3) && @is_readable('/dev/urandom')) {
43:             $rand3 = file_get_contents('/dev/urandom', FALSE, NULL, -1, $length);
44:         }
45:         if (empty($rand3)) {
46:             static $cache;
47:             $rand3 = $cache ?: $cache = md5(serialize($_SERVER), TRUE);
48:         }
49: 
50:         $s = '';
51:         for ($i = 0; $i < $length; $i++) {
52:             if ($i % 5 === 0) {
53:                 list($rand, $rand2) = explode(' ', microtime());
54:                 $rand += lcg_value();
55:             }
56:             $rand *= $chLen;
57:             $s .= $charlist[($rand + $rand2 + ord($rand3[$i % strlen($rand3)])) % $chLen];
58:             $rand -= (int) $rand;
59:         }
60:         return $s;
61:     }
62: 
63: }
64: 
Nette 2.3.1 API API documentation generated by ApiGen 2.8.0