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

https://github.com/pjwiseman/joomla-cms · PHP · 137 lines · 73 code · 2 blank · 62 comment · 15 complexity · 558e08e371ec1904b89bab945e6f4d89 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. * The MIT License (MIT)
  7. *
  8. * Copyright (c) 2015 Paragon Initiative Enterprises
  9. *
  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:
  16. *
  17. * The above copyright notice and this permission notice shall be included in
  18. * all copies or substantial portions of the Software.
  19. *
  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
  26. * SOFTWARE.
  27. */
  28. if (!defined('PHP_VERSION_ID')) {
  29. // This constant was introduced in PHP 5.2.7
  30. $RandomCompatversion = explode('.', PHP_VERSION);
  31. define('PHP_VERSION_ID', ($RandomCompatversion[0] * 10000 + $RandomCompatversion[1] * 100 + $RandomCompatversion[2]));
  32. $RandomCompatversion = null;
  33. }
  34. if (PHP_VERSION_ID < 70000) {
  35. if (!defined('RANDOM_COMPAT_READ_BUFFER')) {
  36. define('RANDOM_COMPAT_READ_BUFFER', 8);
  37. }
  38. $RandomCompatDIR = dirname(__FILE__);
  39. require_once $RandomCompatDIR.'/byte_safe_strings.php';
  40. require_once $RandomCompatDIR.'/cast_to_int.php';
  41. require_once $RandomCompatDIR.'/error_polyfill.php';
  42. if (!function_exists('random_bytes')) {
  43. /**
  44. * PHP 5.2.0 - 5.6.x way to implement random_bytes()
  45. *
  46. * We use conditional statements here to define the function in accordance
  47. * to the operating environment. It's a micro-optimization.
  48. *
  49. * In order of preference:
  50. * 1. Use libsodium if available.
  51. * 2. fread() /dev/urandom if available (never on Windows)
  52. * 3. mcrypt_create_iv($bytes, MCRYPT_CREATE_IV)
  53. * 4. COM('CAPICOM.Utilities.1')->GetRandom()
  54. * 5. openssl_random_pseudo_bytes() (absolute last resort)
  55. *
  56. * See ERRATA.md for our reasoning behind this particular order
  57. */
  58. if (extension_loaded('libsodium')) {
  59. // See random_bytes_libsodium.php
  60. require_once $RandomCompatDIR.'/random_bytes_libsodium.php';
  61. }
  62. if (
  63. !function_exists('random_bytes') &&
  64. DIRECTORY_SEPARATOR === '/' &&
  65. @is_readable('/dev/urandom')
  66. ) {
  67. // DIRECTORY_SEPARATOR === '/' on Unix-like OSes -- this is a fast
  68. // way to exclude Windows.
  69. //
  70. // Error suppression on is_readable() in case of an open_basedir or
  71. // safe_mode failure. All we care about is whether or not we can
  72. // read it at this point. If the PHP environment is going to panic
  73. // over trying to see if the file can be read in the first place,
  74. // that is not helpful to us here.
  75. // See random_bytes_dev_urandom.php
  76. require_once $RandomCompatDIR.'/random_bytes_dev_urandom.php';
  77. }
  78. if (
  79. !function_exists('random_bytes') &&
  80. PHP_VERSION_ID >= 50307 &&
  81. extension_loaded('mcrypt')
  82. ) {
  83. // See random_bytes_mcrypt.php
  84. require_once $RandomCompatDIR.'/random_bytes_mcrypt.php';
  85. }
  86. if (
  87. !function_exists('random_bytes') &&
  88. extension_loaded('com_dotnet') &&
  89. class_exists('COM')
  90. ) {
  91. try {
  92. $RandomCompatCOMtest = new COM('CAPICOM.Utilities.1');
  93. if (method_exists($RandomCompatCOMtest, 'GetRandom')) {
  94. // See random_bytes_com_dotnet.php
  95. require_once $RandomCompatDIR.'/random_bytes_com_dotnet.php';
  96. }
  97. } catch (com_exception $e) {
  98. // Don't try to use it.
  99. }
  100. $RandomCompatCOMtest = null;
  101. }
  102. if (
  103. !function_exists('random_bytes') &&
  104. extension_loaded('openssl') &&
  105. (
  106. // Unix-like with PHP >= 5.3.0 or
  107. (
  108. DIRECTORY_SEPARATOR === '/' &&
  109. PHP_VERSION_ID >= 50300
  110. ) ||
  111. // Windows with PHP >= 5.3.4
  112. PHP_VERSION_ID >= 50304
  113. )
  114. ) {
  115. // See random_bytes_openssl.php
  116. require_once $RandomCompatDIR.'/random_bytes_openssl.php';
  117. }
  118. if (!function_exists('random_bytes')) {
  119. /**
  120. * We don't have any more options, so let's throw an exception right now
  121. * and hope the developer won't let it fail silently.
  122. */
  123. function random_bytes()
  124. {
  125. throw new Exception(
  126. 'There is no suitable CSPRNG installed on your system'
  127. );
  128. }
  129. }
  130. }
  131. if (!function_exists('random_int')) {
  132. require_once $RandomCompatDIR.'/random_int.php';
  133. }
  134. $RandomCompatDIR = null;
  135. }