PageRenderTime 47ms CodeModel.GetById 22ms RepoModel.GetById 0ms app.codeStats 0ms

/src/reference/DefaultRandomizer.php

http://owasp-esapi-php.googlecode.com/
PHP | 187 lines | 58 code | 17 blank | 112 comment | 3 complexity | d50ee928ba06ca60b72e447a41d09c75 MD5 | raw file
Possible License(s): BSD-3-Clause, Apache-2.0
  1. <?php
  2. /**
  3. * OWASP Enterprise Security API (ESAPI)
  4. *
  5. * This file is part of the Open Web Application Security Project (OWASP)
  6. * Enterprise Security API (ESAPI) project. For details, please see
  7. * <a href="http://www.owasp.org/index.php/ESAPI">http://www.owasp.org/index.php/ESAPI</a>.
  8. *
  9. * Copyright (c) 2009 The OWASP Foundation
  10. *
  11. * The ESAPI is published by OWASP under the BSD license. You should read and accept the
  12. * LICENSE before you use, modify, and/or redistribute this software.
  13. *
  14. * @author Andrew van der Stock
  15. * @created 2009
  16. * @since 1.6
  17. * @package ESAPI_Reference
  18. */
  19. require_once dirname(__FILE__) . '/../Randomizer.php';
  20. class DefaultRandomizer implements Randomizer
  21. {
  22. private $maxRand;
  23. function __construct()
  24. {
  25. $this->maxRand = mt_getrandmax();
  26. }
  27. /**
  28. * Gets a random string of a desired length and character set. The use of java.security.SecureRandom
  29. * is recommended because it provides a cryptographically strong pseudo-random number generator.
  30. * If SecureRandom is not used, the pseudo-random number gernerator used should comply with the
  31. * statistical random number generator tests specified in <a href="http://csrc.nist.gov/cryptval/140-2.htm">
  32. * FIPS 140-2, Security Requirements for Cryptographic Modules</a>, section 4.9.1.
  33. *
  34. * @param length
  35. * the length of the string
  36. * @param characterSet
  37. * the set of characters to include in the created random string
  38. *
  39. * @return
  40. * the random string of the desired length and character set
  41. */
  42. function getRandomString($numChars, $charset)
  43. {
  44. if ( $numChars < 1 || strlen($charset) < 2 ) {
  45. throw new InvalidArgumentException();
  46. }
  47. $l = strlen($charset) - 1;
  48. $rs = '';
  49. for ($i = 0; $i < $numChars; $i++)
  50. {
  51. $rs .= $charset[mt_rand(0, $l)];
  52. }
  53. return $rs;
  54. }
  55. /**
  56. * Returns a random boolean. The use of java.security.SecureRandom
  57. * is recommended because it provides a cryptographically strong pseudo-random number generator.
  58. * If SecureRandom is not used, the pseudo-random number gernerator used should comply with the
  59. * statistical random number generator tests specified in <a href="http://csrc.nist.gov/cryptval/140-2.htm">
  60. * FIPS 140-2, Security Requirements for Cryptographic Modules</a>, section 4.9.1.
  61. *
  62. * @return
  63. * true or false, randomly
  64. */
  65. function getRandomBoolean()
  66. {
  67. return (( mt_rand(0, 100) % 2) ? true : false);
  68. }
  69. /**
  70. * Gets the random integer. The use of java.security.SecureRandom
  71. * is recommended because it provides a cryptographically strong pseudo-random number generator.
  72. * If SecureRandom is not used, the pseudo-random number gernerator used should comply with the
  73. * statistical random number generator tests specified in <a href="http://csrc.nist.gov/cryptval/140-2.htm">
  74. * FIPS 140-2, Security Requirements for Cryptographic Modules</a>, section 4.9.1.
  75. *
  76. * @param min
  77. * the minimum integer that will be returned
  78. * @param max
  79. * the maximum integer that will be returned
  80. *
  81. * @return
  82. * the random integer
  83. */
  84. function getRandomInteger($min, $max)
  85. {
  86. return mt_rand($min, $max);
  87. }
  88. /**
  89. * Gets the random long. The use of java.security.SecureRandom
  90. * is recommended because it provides a cryptographically strong pseudo-random number generator.
  91. * If SecureRandom is not used, the pseudo-random number gernerator used should comply with the
  92. * statistical random number generator tests specified in <a href="http://csrc.nist.gov/cryptval/140-2.htm">
  93. * FIPS 140-2, Security Requirements for Cryptographic Modules</a>, section 4.9.1.
  94. *
  95. * mt_rand() without arguments will return between 0 and mt_getrandmax(). That's about as good as PHP gets
  96. *
  97. * @return
  98. * the random long
  99. */
  100. function getRandomLong()
  101. {
  102. return mt_rand();
  103. }
  104. /**
  105. * Returns an unguessable random filename with the specified extension. This method could call
  106. * getRandomString(length, charset) from this Class with the desired length and alphanumerics as the charset
  107. * then merely append "." + extension.
  108. *
  109. * @param extension
  110. * extension to add to the random filename
  111. *
  112. * @return
  113. * a random unguessable filename ending with the specified extension
  114. */
  115. function getRandomFilename($extension = '')
  116. {
  117. // Because PHP runs on case insensitive OS as well as case sensitive OS, only use lowercase
  118. $rs = $this->getRandomString(16, 'abcdefghijklmnopqrstuvxyz0123456789');
  119. $rs .= $extension;
  120. return $rs;
  121. }
  122. /**
  123. * Gets the random real. The use of java.security.SecureRandom
  124. * is recommended because it provides a cryptographically strong pseudo-random number generator.
  125. * If SecureRandom is not used, the pseudo-random number gernerator used should comply with the
  126. * statistical random number generator tests specified in <a href="http://csrc.nist.gov/cryptval/140-2.htm">
  127. * FIPS 140-2, Security Requirements for Cryptographic Modules</a>, section 4.9.1.
  128. *
  129. * @param min
  130. * the minimum real number that will be returned
  131. * @param max
  132. * the maximum real number that will be returned
  133. *
  134. * @return
  135. * the random real
  136. */
  137. function getRandomReal($min, $max)
  138. {
  139. $rf = (float) (mt_rand() / $this->maxRand); // Maximizes the random bit counts from the PHP PRNG
  140. $factor = $max - $min;
  141. return (float) ($rf * $factor + $min);
  142. }
  143. /**
  144. * Generates a random GUID. This method could use a hash of random Strings, the current time,
  145. * and any other random data available. The format is a well-defined sequence of 32 hex digits
  146. * grouped into chunks of 8-4-4-4-12.
  147. *
  148. * Function from comments found on http://php.net/uniqid
  149. *
  150. * @return
  151. * the GUID
  152. *
  153. * @throws
  154. * EncryptionException if hashing or encryption fails
  155. */
  156. function getRandomGUID()
  157. {
  158. return sprintf('%04x%04x-%04x-%04x-%04x-%04x%04x%04x',
  159. mt_rand(0, 65535), mt_rand(0, 65535), // 32 bits for "time_low"
  160. mt_rand(0, 65535), // 16 bits for "time_mid"
  161. mt_rand(0, 4095), // 12 bits before the 0100 of (version) 4 for "time_hi_and_version"
  162. bindec(substr_replace(sprintf('%016b', mt_rand(0, 65535)), '01', 6, 2)),
  163. // 8 bits, the last two of which (positions 6 and 7) are 01, for "clk_seq_hi_res"
  164. // (hence, the 2nd hex digit after the 3rd hyphen can only be 1, 5, 9 or d)
  165. // 8 bits for "clk_seq_low"
  166. mt_rand(0, 65535), mt_rand(0, 65535), mt_rand(0, 65535) // 48 bits for "node"
  167. );
  168. }
  169. }
  170. ?>