PageRenderTime 49ms CodeModel.GetById 21ms RepoModel.GetById 0ms app.codeStats 0ms

/Auth/OpenID/CryptUtil.php

http://github.com/openid/php-openid
PHP | 121 lines | 65 code | 13 blank | 43 comment | 16 complexity | 619ffe6d839538a49709210131dc3100 MD5 | raw file
Possible License(s): Apache-2.0
  1. <?php
  2. /**
  3. * CryptUtil: A suite of wrapper utility functions for the OpenID
  4. * library.
  5. *
  6. * PHP versions 4 and 5
  7. *
  8. * LICENSE: See the COPYING file included in this distribution.
  9. *
  10. * @access private
  11. * @package OpenID
  12. * @author JanRain, Inc. <openid@janrain.com>
  13. * @copyright 2005-2008 Janrain, Inc.
  14. * @license http://www.apache.org/licenses/LICENSE-2.0 Apache
  15. */
  16. if (!defined('Auth_OpenID_RAND_SOURCE')) {
  17. /**
  18. * The filename for a source of random bytes. Define this yourself
  19. * if you have a different source of randomness.
  20. */
  21. define('Auth_OpenID_RAND_SOURCE', '/dev/urandom');
  22. }
  23. class Auth_OpenID_CryptUtil {
  24. /**
  25. * Get the specified number of random bytes.
  26. *
  27. * Attempts to use a cryptographically secure (not predictable)
  28. * source of randomness if available. If there is no high-entropy
  29. * randomness source available, it will fail. As a last resort,
  30. * for non-critical systems, define
  31. * <code>Auth_OpenID_RAND_SOURCE</code> as <code>null</code>, and
  32. * the code will fall back on a pseudo-random number generator.
  33. *
  34. * @param int $num_bytes The length of the return value
  35. * @return string $bytes random bytes
  36. */
  37. static function getBytes($num_bytes)
  38. {
  39. static $f = null;
  40. if ($f === null) {
  41. if (Auth_OpenID_RAND_SOURCE === null) {
  42. $f = false;
  43. } else {
  44. $f = @fopen(Auth_OpenID_RAND_SOURCE, "r");
  45. if ($f === false) {
  46. $msg = 'Define Auth_OpenID_RAND_SOURCE as null to ' .
  47. ' continue with an insecure random number generator.';
  48. trigger_error($msg, E_USER_ERROR);
  49. }
  50. }
  51. }
  52. if ($f === false) {
  53. // pseudorandom used
  54. $bytes = '';
  55. for ($i = 0; $i < $num_bytes; $i += 4) {
  56. $bytes .= pack('L', mt_rand());
  57. }
  58. $bytes = substr($bytes, 0, $num_bytes);
  59. } else {
  60. $bytes = fread($f, $num_bytes);
  61. }
  62. return $bytes;
  63. }
  64. /**
  65. * Produce a string of length random bytes, chosen from chrs. If
  66. * $chrs is null, the resulting string may contain any characters.
  67. *
  68. * @param integer $length The length of the resulting
  69. * randomly-generated string
  70. * @param string|null $population A string of characters from which to choose
  71. * to build the new string
  72. * @return string $result A string of randomly-chosen characters
  73. * from $chrs
  74. */
  75. static function randomString($length, $population = null)
  76. {
  77. if ($population === null) {
  78. return Auth_OpenID_CryptUtil::getBytes($length);
  79. }
  80. $popsize = strlen($population);
  81. if ($popsize > 256) {
  82. $msg = 'More than 256 characters supplied to ' . __FUNCTION__;
  83. trigger_error($msg, E_USER_ERROR);
  84. }
  85. $duplicate = 256 % $popsize;
  86. $str = "";
  87. for ($i = 0; $i < $length; $i++) {
  88. do {
  89. $n = ord(Auth_OpenID_CryptUtil::getBytes(1));
  90. } while ($n < $duplicate);
  91. $n %= $popsize;
  92. $str .= $population[$n];
  93. }
  94. return $str;
  95. }
  96. static function constEq($s1, $s2)
  97. {
  98. if (strlen($s1) != strlen($s2)) {
  99. return false;
  100. }
  101. $result = true;
  102. $length = strlen($s1);
  103. for ($i = 0; $i < $length; $i++) {
  104. $result &= ($s1[$i] == $s2[$i]);
  105. }
  106. return $result;
  107. }
  108. }