PageRenderTime 56ms CodeModel.GetById 30ms RepoModel.GetById 1ms app.codeStats 0ms

/lib/CryptLib/Random/Source/MicroTime.php

http://github.com/ircmaxell/PHP-CryptLib
PHP | 91 lines | 30 code | 8 blank | 53 comment | 2 complexity | ace6fe6c7c6f407a0ee784464000e1d4 MD5 | raw file
  1. <?php
  2. /**
  3. * The Microtime Random Number Source
  4. *
  5. * This uses the current micro-second (looped several times) for a **very** weak
  6. * random number source. This is only useful when combined with several other
  7. * stronger sources
  8. *
  9. * PHP version 5.3
  10. *
  11. * @category PHPCryptLib
  12. * @package Random
  13. * @subpackage Source
  14. * @author Anthony Ferrara <ircmaxell@ircmaxell.com>
  15. * @copyright 2011 The Authors
  16. * @license http://www.opensource.org/licenses/mit-license.html MIT License
  17. * @version Build @@version@@
  18. */
  19. namespace CryptLib\Random\Source;
  20. use CryptLib\Core\Strength;
  21. /**
  22. * The Microtime Random Number Source
  23. *
  24. * This uses the current micro-second (looped several times) for a **very** weak
  25. * random number source. This is only useful when combined with several other
  26. * stronger sources
  27. *
  28. * @category PHPCryptLib
  29. * @package Random
  30. * @subpackage Source
  31. * @author Anthony Ferrara <ircmaxell@ircmaxell.com>
  32. * @codeCoverageIgnore
  33. */
  34. class MicroTime implements \CryptLib\Random\Source {
  35. private $state = null;
  36. /**
  37. * Return an instance of Strength indicating the strength of the source
  38. *
  39. * @return Strength An instance of one of the strength classes
  40. */
  41. public static function getStrength() {
  42. return new Strength(Strength::VERYLOW);
  43. }
  44. public function __construct() {
  45. $state = '';
  46. if (function_exists('posix_times')) {
  47. $state .= serialize(posix_times());
  48. }
  49. $state .= getmypid() . memory_get_usage();
  50. $state .= serialize($_ENV);
  51. $this->state = hash('sha512', $state, true);
  52. }
  53. /**
  54. * Generate a random string of the specified size
  55. *
  56. * @param int $size The size of the requested random string
  57. *
  58. * @return string A string of the requested size
  59. */
  60. public function generate($size) {
  61. $result = '';
  62. $seed = microtime() . memory_get_usage();
  63. $this->state = hash('sha512', $this->state . $seed, true);
  64. /**
  65. * Make the generated randomness a bit better by forcing a GC run which
  66. * should complete in a indeterminate amount of time, hence improving
  67. * the strength of the randomness a bit. It's still not crypto-safe,
  68. * but at least it's more difficult to predict.
  69. */
  70. gc_collect_cycles();
  71. for ($i = 0; $i < $size; $i += 8) {
  72. $seed = $this->state . microtime() . pack('N', $i);
  73. $this->state = hash('sha512', $seed, true);
  74. /**
  75. * We only use the first 8 bytes here to prevent exposing the state
  76. * in its entirety, which could potentially expose other random
  77. * generations in the future (in the same process)...
  78. */
  79. $result .= substr($this->state, 0, 8);
  80. }
  81. return substr($result, 0, $size);
  82. }
  83. }