PageRenderTime 41ms CodeModel.GetById 15ms RepoModel.GetById 0ms app.codeStats 0ms

/core/lib/Drupal/Component/Utility/Crypt.php

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