PageRenderTime 56ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 0ms

/vendor/paragonie/random_compat/lib/random.php

https://gitlab.com/alamindev/videogallery
PHP | 215 lines | 106 code | 15 blank | 94 comment | 20 complexity | 0df92b5c5908409a852b19f830039615 MD5 | raw file
  1. <?php
  2. /**
  3. * Random_* Compatibility Library
  4. * for using the new PHP 7 random_* API in PHP 5 projects
  5. *
  6. * @version 2.0.4
  7. * @released 2016-11-07
  8. *
  9. * The MIT License (MIT)
  10. *
  11. * Copyright (c) 2015 - 2016 Paragon Initiative Enterprises
  12. *
  13. * Permission is hereby granted, free of charge, to any person obtaining a copy
  14. * of this software and associated documentation files (the "Software"), to deal
  15. * in the Software without restriction, including without limitation the rights
  16. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  17. * copies of the Software, and to permit persons to whom the Software is
  18. * furnished to do so, subject to the following conditions:
  19. *
  20. * The above copyright notice and this permission notice shall be included in
  21. * all copies or substantial portions of the Software.
  22. *
  23. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  24. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  25. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  26. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  27. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  28. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  29. * SOFTWARE.
  30. */
  31. if (!defined('PHP_VERSION_ID')) {
  32. // This constant was introduced in PHP 5.2.7
  33. $RandomCompatversion = array_map('intval', explode('.', PHP_VERSION));
  34. define(
  35. 'PHP_VERSION_ID',
  36. $RandomCompatversion[0] * 10000
  37. + $RandomCompatversion[1] * 100
  38. + $RandomCompatversion[2]
  39. );
  40. $RandomCompatversion = null;
  41. }
  42. /**
  43. * PHP 7.0.0 and newer have these functions natively.
  44. */
  45. if (PHP_VERSION_ID < 70000) {
  46. if (!defined('RANDOM_COMPAT_READ_BUFFER')) {
  47. define('RANDOM_COMPAT_READ_BUFFER', 8);
  48. }
  49. $RandomCompatDIR = dirname(__FILE__);
  50. require_once $RandomCompatDIR.'/byte_safe_strings.php';
  51. require_once $RandomCompatDIR.'/cast_to_int.php';
  52. require_once $RandomCompatDIR.'/error_polyfill.php';
  53. if (!is_callable('random_bytes')) {
  54. /**
  55. * PHP 5.2.0 - 5.6.x way to implement random_bytes()
  56. *
  57. * We use conditional statements here to define the function in accordance
  58. * to the operating environment. It's a micro-optimization.
  59. *
  60. * In order of preference:
  61. * 1. Use libsodium if available.
  62. * 2. fread() /dev/urandom if available (never on Windows)
  63. * 3. mcrypt_create_iv($bytes, MCRYPT_DEV_URANDOM)
  64. * 4. COM('CAPICOM.Utilities.1')->GetRandom()
  65. * 5. openssl_random_pseudo_bytes() (absolute last resort)
  66. *
  67. * See RATIONALE.md for our reasoning behind this particular order
  68. */
  69. if (extension_loaded('libsodium')) {
  70. // See random_bytes_libsodium.php
  71. if (PHP_VERSION_ID >= 50300 && is_callable('\\Sodium\\randombytes_buf')) {
  72. require_once $RandomCompatDIR.'/random_bytes_libsodium.php';
  73. } elseif (method_exists('Sodium', 'randombytes_buf')) {
  74. require_once $RandomCompatDIR.'/random_bytes_libsodium_legacy.php';
  75. }
  76. }
  77. /**
  78. * Reading directly from /dev/urandom:
  79. */
  80. if (DIRECTORY_SEPARATOR === '/') {
  81. // DIRECTORY_SEPARATOR === '/' on Unix-like OSes -- this is a fast
  82. // way to exclude Windows.
  83. $RandomCompatUrandom = true;
  84. $RandomCompat_basedir = ini_get('open_basedir');
  85. if (!empty($RandomCompat_basedir)) {
  86. $RandomCompat_open_basedir = explode(
  87. PATH_SEPARATOR,
  88. strtolower($RandomCompat_basedir)
  89. );
  90. $RandomCompatUrandom = (array() !== array_intersect(
  91. array('/dev', '/dev/', '/dev/urandom'),
  92. $RandomCompat_open_basedir
  93. ));
  94. $RandomCompat_open_basedir = null;
  95. }
  96. if (
  97. !is_callable('random_bytes')
  98. &&
  99. $RandomCompatUrandom
  100. &&
  101. @is_readable('/dev/urandom')
  102. ) {
  103. // Error suppression on is_readable() in case of an open_basedir
  104. // or safe_mode failure. All we care about is whether or not we
  105. // can read it at this point. If the PHP environment is going to
  106. // panic over trying to see if the file can be read in the first
  107. // place, that is not helpful to us here.
  108. // See random_bytes_dev_urandom.php
  109. require_once $RandomCompatDIR.'/random_bytes_dev_urandom.php';
  110. }
  111. // Unset variables after use
  112. $RandomCompat_basedir = null;
  113. } else {
  114. $RandomCompatUrandom = false;
  115. }
  116. /**
  117. * mcrypt_create_iv()
  118. *
  119. * We only want to use mcypt_create_iv() if:
  120. *
  121. * - random_bytes() hasn't already been defined
  122. * - PHP >= 5.3.7
  123. * - the mcrypt extensions is loaded
  124. * - One of these two conditions is true:
  125. * - We're on Windows (DIRECTORY_SEPARATOR !== '/')
  126. * - We're not on Windows and /dev/urandom is readabale
  127. * (i.e. we're not in a chroot jail)
  128. * - Special case:
  129. * - If we're not on Windows, but the PHP version is between
  130. * 5.6.10 and 5.6.12, we don't want to use mcrypt. It will
  131. * hang indefinitely. This is bad.
  132. */
  133. if (
  134. !is_callable('random_bytes')
  135. &&
  136. PHP_VERSION_ID >= 50307
  137. &&
  138. extension_loaded('mcrypt')
  139. ) {
  140. // Prevent this code from hanging indefinitely on non-Windows;
  141. // see https://bugs.php.net/bug.php?id=69833
  142. if (
  143. DIRECTORY_SEPARATOR !== '/' ||
  144. (PHP_VERSION_ID <= 50609 || PHP_VERSION_ID >= 50613)
  145. ) {
  146. // See random_bytes_mcrypt.php
  147. require_once $RandomCompatDIR.'/random_bytes_mcrypt.php';
  148. }
  149. }
  150. $RandomCompatUrandom = null;
  151. /**
  152. * This is a Windows-specific fallback, for when the mcrypt extension
  153. * isn't loaded.
  154. */
  155. if (
  156. !is_callable('random_bytes')
  157. &&
  158. extension_loaded('com_dotnet')
  159. &&
  160. class_exists('COM')
  161. ) {
  162. $RandomCompat_disabled_classes = preg_split(
  163. '#\s*,\s*#',
  164. strtolower(ini_get('disable_classes'))
  165. );
  166. if (!in_array('com', $RandomCompat_disabled_classes)) {
  167. try {
  168. $RandomCompatCOMtest = new COM('CAPICOM.Utilities.1');
  169. if (method_exists($RandomCompatCOMtest, 'GetRandom')) {
  170. // See random_bytes_com_dotnet.php
  171. require_once $RandomCompatDIR.'/random_bytes_com_dotnet.php';
  172. }
  173. } catch (com_exception $e) {
  174. // Don't try to use it.
  175. }
  176. }
  177. $RandomCompat_disabled_classes = null;
  178. $RandomCompatCOMtest = null;
  179. }
  180. /**
  181. * throw new Exception
  182. */
  183. if (!is_callable('random_bytes')) {
  184. /**
  185. * We don't have any more options, so let's throw an exception right now
  186. * and hope the developer won't let it fail silently.
  187. */
  188. function random_bytes($length)
  189. {
  190. throw new Exception(
  191. 'There is no suitable CSPRNG installed on your system'
  192. );
  193. }
  194. }
  195. }
  196. if (!is_callable('random_int')) {
  197. require_once $RandomCompatDIR.'/random_int.php';
  198. }
  199. $RandomCompatDIR = null;
  200. }