Hash.php 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. <?php
  2. /*
  3. * The RandomLib library for securely generating random numbers and strings in PHP
  4. *
  5. * @author Anthony Ferrara <ircmaxell@ircmaxell.com>
  6. * @copyright 2011 The Authors
  7. * @license http://www.opensource.org/licenses/mit-license.html MIT License
  8. * @version Build @@version@@
  9. */
  10. /**
  11. * The Hash medium strength mixer class
  12. *
  13. * This class implements a mixer based upon the recommendations in RFC 4086
  14. * section 5.2
  15. *
  16. * PHP version 5.3
  17. *
  18. * @see http://tools.ietf.org/html/rfc4086#section-5.2
  19. *
  20. * @category PHPCryptLib
  21. * @package Random
  22. * @subpackage Mixer
  23. *
  24. * @author Anthony Ferrara <ircmaxell@ircmaxell.com>
  25. * @copyright 2011 The Authors
  26. * @license http://www.opensource.org/licenses/mit-license.html MIT License
  27. *
  28. * @version Build @@version@@
  29. */
  30. namespace RandomLib\Mixer;
  31. use SecurityLib\Strength;
  32. use SecurityLib\Util;
  33. /**
  34. * The Hash medium strength mixer class
  35. *
  36. * This class implements a mixer based upon the recommendations in RFC 4086
  37. * section 5.2
  38. *
  39. * @see http://tools.ietf.org/html/rfc4086#section-5.2
  40. *
  41. * @category PHPCryptLib
  42. * @package Random
  43. * @subpackage Mixer
  44. *
  45. * @author Anthony Ferrara <ircmaxell@ircmaxell.com>
  46. */
  47. class Hash extends \RandomLib\AbstractMixer
  48. {
  49. /**
  50. * @var string The hash instance to use
  51. */
  52. protected $hash = null;
  53. /**
  54. * Build the hash mixer
  55. *
  56. * @param string $hash The hash instance to use (defaults to sha512)
  57. *
  58. * @return void
  59. */
  60. public function __construct($hash = 'sha512')
  61. {
  62. $this->hash = $hash;
  63. }
  64. /**
  65. * Return an instance of Strength indicating the strength of the source
  66. *
  67. * @return \SecurityLib\Strength An instance of one of the strength classes
  68. */
  69. public static function getStrength()
  70. {
  71. return new Strength(Strength::MEDIUM);
  72. }
  73. /**
  74. * Test to see if the mixer is available
  75. *
  76. * @return bool If the mixer is available on the system
  77. */
  78. public static function test()
  79. {
  80. return true;
  81. }
  82. /**
  83. * Get the block size (the size of the individual blocks used for the mixing)
  84. *
  85. * @return int The block size
  86. */
  87. protected function getPartSize()
  88. {
  89. return Util::safeStrlen(hash($this->hash, '', true));
  90. }
  91. /**
  92. * Mix 2 parts together using one method
  93. *
  94. * @param string $part1 The first part to mix
  95. * @param string $part2 The second part to mix
  96. *
  97. * @return string The mixed data
  98. */
  99. protected function mixParts1($part1, $part2)
  100. {
  101. return hash_hmac($this->hash, $part1, $part2, true);
  102. }
  103. /**
  104. * Mix 2 parts together using another different method
  105. *
  106. * @param string $part1 The first part to mix
  107. * @param string $part2 The second part to mix
  108. *
  109. * @return string The mixed data
  110. */
  111. protected function mixParts2($part1, $part2)
  112. {
  113. return hash_hmac($this->hash, $part2, $part1, true);
  114. }
  115. }