3 * Random_* Compatibility Library
4 * for using the new PHP 7 random_* API in PHP 5 projects
6 * The MIT License (MIT)
8 * Copyright (c) 2015 Paragon Initiative Enterprises
10 * Permission is hereby granted, free of charge, to any person obtaining a copy
11 * of this software and associated documentation files (the "Software"), to deal
12 * in the Software without restriction, including without limitation the rights
13 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
14 * copies of the Software, and to permit persons to whom the Software is
15 * furnished to do so, subject to the following conditions:
17 * The above copyright notice and this permission notice shall be included in
18 * all copies or substantial portions of the Software.
20 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
21 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
22 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
23 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
24 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
25 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
29 if (!defined('PHP_VERSION_ID')) {
30 // This constant was introduced in PHP 5.2.7
31 $RandomCompatversion = explode('.', PHP_VERSION);
32 define('PHP_VERSION_ID', ($RandomCompatversion[0] * 10000 + $RandomCompatversion[1] * 100 + $RandomCompatversion[2]));
33 $RandomCompatversion = null;
35 if (PHP_VERSION_ID < 70000) {
36 if (!defined('RANDOM_COMPAT_READ_BUFFER')) {
37 define('RANDOM_COMPAT_READ_BUFFER', 8);
39 $RandomCompatDIR = dirname(__FILE__);
40 require_once $RandomCompatDIR.'/byte_safe_strings.php';
41 require_once $RandomCompatDIR.'/cast_to_int.php';
42 require_once $RandomCompatDIR.'/error_polyfill.php';
43 if (!function_exists('random_bytes')) {
45 * PHP 5.2.0 - 5.6.x way to implement random_bytes()
47 * We use conditional statements here to define the function in accordance
48 * to the operating environment. It's a micro-optimization.
50 * In order of preference:
51 * 1. Use libsodium if available.
52 * 2. fread() /dev/urandom if available (never on Windows)
53 * 3. mcrypt_create_iv($bytes, MCRYPT_CREATE_IV)
54 * 4. COM('CAPICOM.Utilities.1')->GetRandom()
55 * 5. openssl_random_pseudo_bytes() (absolute last resort)
57 * See ERRATA.md for our reasoning behind this particular order
59 if (PHP_VERSION_ID >= 50300 && extension_loaded('libsodium') && function_exists('\\Sodium\\randombytes_buf')) {
60 // See random_bytes_libsodium.php
61 require_once $RandomCompatDIR.'/random_bytes_libsodium.php';
64 !function_exists('random_bytes') &&
65 DIRECTORY_SEPARATOR === '/' &&
66 @is_readable('/dev/urandom')
68 // DIRECTORY_SEPARATOR === '/' on Unix-like OSes -- this is a fast
69 // way to exclude Windows.
71 // Error suppression on is_readable() in case of an open_basedir or
72 // safe_mode failure. All we care about is whether or not we can
73 // read it at this point. If the PHP environment is going to panic
74 // over trying to see if the file can be read in the first place,
75 // that is not helpful to us here.
77 // See random_bytes_dev_urandom.php
78 require_once $RandomCompatDIR.'/random_bytes_dev_urandom.php';
81 !function_exists('random_bytes') &&
82 PHP_VERSION_ID >= 50307 &&
83 extension_loaded('mcrypt')
85 // See random_bytes_mcrypt.php
86 require_once $RandomCompatDIR.'/random_bytes_mcrypt.php';
89 !function_exists('random_bytes') &&
90 extension_loaded('com_dotnet') &&
93 $RandomCompat_disabled_classes = preg_split(
95 strtolower(ini_get('disable_classes'))
98 if (!in_array('com', $RandomCompat_disabled_classes)) {
100 $RandomCompatCOMtest = new COM('CAPICOM.Utilities.1');
101 if (method_exists($RandomCompatCOMtest, 'GetRandom')) {
102 // See random_bytes_com_dotnet.php
103 require_once $RandomCompatDIR.'/random_bytes_com_dotnet.php';
105 } catch (com_exception $e) {
106 // Don't try to use it.
109 $RandomCompat_disabled_classes = null;
110 $RandomCompatCOMtest = null;
113 !function_exists('random_bytes') &&
114 extension_loaded('openssl') &&
116 // Unix-like with PHP >= 5.3.0 or
118 DIRECTORY_SEPARATOR === '/' &&
119 PHP_VERSION_ID >= 50300
121 // Windows with PHP >= 5.4.1
122 PHP_VERSION_ID >= 50401
125 // See random_bytes_openssl.php
126 require_once $RandomCompatDIR.'/random_bytes_openssl.php';
128 if (!function_exists('random_bytes')) {
130 * We don't have any more options, so let's throw an exception right now
131 * and hope the developer won't let it fail silently.
133 function random_bytes()
136 'There is no suitable CSPRNG installed on your system'
141 if (!function_exists('random_int')) {
142 require_once $RandomCompatDIR.'/random_int.php';
144 $RandomCompatDIR = null;