PageRenderTime 37ms CodeModel.GetById 12ms RepoModel.GetById 0ms app.codeStats 0ms

/sites/all/modules/service_container/lib/Drupal/Component/Utility/Crypt.php

https://gitlab.com/leoplanxxi/dr7-web-buap-2016
PHP | 143 lines | 47 code | 14 blank | 82 comment | 8 complexity | 7058473fa968d4e289d79d660e00f510 MD5 | raw file
  1. <?php
  2. /**
  3. * @file
  4. * Contains \Drupal\Component\Utility\Crypt.
  5. */
  6. namespace Drupal\Component\Utility;
  7. /**
  8. * Utility class for cryptographically-secure string handling routines.
  9. *
  10. * @ingroup utility
  11. */
  12. class Crypt {
  13. /**
  14. * Returns a string of highly randomized bytes (over the full 8-bit range).
  15. *
  16. * This function is better than simply calling mt_rand() or any other built-in
  17. * PHP function because it can return a long string of bytes (compared to < 4
  18. * bytes normally from mt_rand()) and uses the best available pseudo-random
  19. * source.
  20. *
  21. * @param int $count
  22. * The number of characters (bytes) to return in the string.
  23. *
  24. * @return string
  25. * A randomly generated string.
  26. */
  27. public static function randomBytes($count) {
  28. // $random_state does not use drupal_static as it stores random bytes.
  29. static $random_state, $bytes;
  30. $missing_bytes = $count - strlen($bytes);
  31. if ($missing_bytes > 0) {
  32. // openssl_random_pseudo_bytes() will find entropy in a system-dependent
  33. // way.
  34. if (function_exists('openssl_random_pseudo_bytes')) {
  35. $bytes .= openssl_random_pseudo_bytes($missing_bytes);
  36. }
  37. // Else, read directly from /dev/urandom, which is available on many *nix
  38. // systems and is considered cryptographically secure.
  39. elseif ($fh = @fopen('/dev/urandom', 'rb')) {
  40. // PHP only performs buffered reads, so in reality it will always read
  41. // at least 4096 bytes. Thus, it costs nothing extra to read and store
  42. // that much so as to speed any additional invocations.
  43. $bytes .= fread($fh, max(4096, $missing_bytes));
  44. fclose($fh);
  45. }
  46. // If we couldn't get enough entropy, this simple hash-based PRNG will
  47. // generate a good set of pseudo-random bytes on any system.
  48. // Note that it may be important that our $random_state is passed
  49. // through hash() prior to being rolled into $output, that the two hash()
  50. // invocations are different, and that the extra input into the first one -
  51. // the microtime() - is prepended rather than appended. This is to avoid
  52. // directly leaking $random_state via the $output stream, which could
  53. // allow for trivial prediction of further "random" numbers.
  54. if (strlen($bytes) < $count) {
  55. // Initialize on the first call. The contents of $_SERVER includes a mix
  56. // of user-specific and system information that varies a little with
  57. // each page.
  58. if (!isset($random_state)) {
  59. $random_state = print_r($_SERVER, TRUE);
  60. if (function_exists('getmypid')) {
  61. // Further initialize with the somewhat random PHP process ID.
  62. $random_state .= getmypid();
  63. }
  64. $bytes = '';
  65. }
  66. do {
  67. $random_state = hash('sha256', microtime() . mt_rand() . $random_state);
  68. $bytes .= hash('sha256', mt_rand() . $random_state, TRUE);
  69. } while (strlen($bytes) < $count);
  70. }
  71. }
  72. $output = substr($bytes, 0, $count);
  73. $bytes = substr($bytes, $count);
  74. return $output;
  75. }
  76. /**
  77. * Calculates a base-64 encoded, URL-safe sha-256 hmac.
  78. *
  79. * @param mixed $data
  80. * Scalar value to be validated with the hmac.
  81. * @param mixed $key
  82. * A secret key, this can be any scalar value.
  83. *
  84. * @return string
  85. * A base-64 encoded sha-256 hmac, with + replaced with -, / with _ and
  86. * any = padding characters removed.
  87. */
  88. public static function hmacBase64($data, $key) {
  89. // $data and $key being strings here is necessary to avoid empty string
  90. // results of the hash function if they are not scalar values. As this
  91. // function is used in security-critical contexts like token validation it
  92. // is important that it never returns an empty string.
  93. if (!is_scalar($data) || !is_scalar($key)) {
  94. throw new \InvalidArgumentException('Both parameters passed to \Drupal\Component\Utility\Crypt::hmacBase64 must be scalar values.');
  95. }
  96. $hmac = base64_encode(hash_hmac('sha256', $data, $key, TRUE));
  97. // Modify the hmac so it's safe to use in URLs.
  98. return strtr($hmac, array('+' => '-', '/' => '_', '=' => ''));
  99. }
  100. /**
  101. * Calculates a base-64 encoded, URL-safe sha-256 hash.
  102. *
  103. * @param string $data
  104. * String to be hashed.
  105. *
  106. * @return string
  107. * A base-64 encoded sha-256 hash, with + replaced with -, / with _ and
  108. * any = padding characters removed.
  109. */
  110. public static function hashBase64($data) {
  111. $hash = base64_encode(hash('sha256', $data, TRUE));
  112. // Modify the hash so it's safe to use in URLs.
  113. return strtr($hash, array('+' => '-', '/' => '_', '=' => ''));
  114. }
  115. /**
  116. * Returns a URL-safe, base64 encoded string of highly randomized bytes.
  117. *
  118. * @param $byte_count
  119. * The number of random bytes to fetch and base64 encode.
  120. *
  121. * @return string
  122. * The base64 encoded result will have a length of up to 4 * $byte_count.
  123. *
  124. * @see \Drupal\Component\Utility\Crypt::randomBytes()
  125. */
  126. public static function randomBytesBase64($count = 32) {
  127. return strtr(base64_encode(static::randomBytes($count)), array('+' => '-', '/' => '_', '=' => ''));
  128. }
  129. }