PageRenderTime 45ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 0ms

/lib/RandomLib/Mixer/Hash.php

https://github.com/ezimuel/RandomLib
PHP | 105 lines | 24 code | 11 blank | 70 comment | 0 complexity | 92d04b4701fdb1a5755650524a2273ad MD5 | raw file
  1. <?php
  2. /**
  3. * The Hash medium strength mixer class
  4. *
  5. * This class implements a mixer based upon the recommendations in RFC 4086
  6. * section 5.2
  7. *
  8. * PHP version 5.3
  9. *
  10. * @see http://tools.ietf.org/html/rfc4086#section-5.2
  11. * @category PHPCryptLib
  12. * @package Random
  13. * @subpackage Mixer
  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 RandomLib\Mixer;
  20. use \SecurityLib\Strength;
  21. /**
  22. * The Hash medium strength mixer class
  23. *
  24. * This class implements a mixer based upon the recommendations in RFC 4086
  25. * section 5.2
  26. *
  27. * @see http://tools.ietf.org/html/rfc4086#section-5.2
  28. * @category PHPCryptLib
  29. * @package Random
  30. * @subpackage Mixer
  31. * @author Anthony Ferrara <ircmaxell@ircmaxell.com>
  32. */
  33. class Hash extends \RandomLib\AbstractMixer {
  34. /**
  35. * @var string The hash instance to use
  36. */
  37. protected $hash = null;
  38. /**
  39. * Build the hash mixer
  40. *
  41. * @param string $hash The hash instance to use (defaults to sha512)
  42. *
  43. * @return void
  44. */
  45. public function __construct($hash = 'sha512') {
  46. $this->hash = $hash;
  47. }
  48. /**
  49. * Return an instance of Strength indicating the strength of the source
  50. *
  51. * @return Strength An instance of one of the strength classes
  52. */
  53. public static function getStrength() {
  54. return new Strength(Strength::MEDIUM);
  55. }
  56. /**
  57. * Test to see if the mixer is available
  58. *
  59. * @return boolean If the mixer is available on the system
  60. */
  61. public static function test() {
  62. return true;
  63. }
  64. /**
  65. * Get the block size (the size of the individual blocks used for the mixing)
  66. *
  67. * @return int The block size
  68. */
  69. protected function getPartSize() {
  70. return strlen(hash($this->hash, '', true));
  71. }
  72. /**
  73. * Mix 2 parts together using one method
  74. *
  75. * @param string $part1 The first part to mix
  76. * @param string $part2 The second part to mix
  77. *
  78. * @return string The mixed data
  79. */
  80. protected function mixParts1($part1, $part2) {
  81. return hash_hmac($this->hash, $part1, $part2, true);
  82. }
  83. /**
  84. * Mix 2 parts together using another different method
  85. *
  86. * @param string $part1 The first part to mix
  87. * @param string $part2 The second part to mix
  88. *
  89. * @return string The mixed data
  90. */
  91. protected function mixParts2($part1, $part2) {
  92. return hash_hmac($this->hash, $part2, $part1, true);
  93. }
  94. }