XorMixer.php 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  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. * @author Paragon Initiative Enterprises <security@paragonie.com>
  26. * @copyright 2011 The Authors
  27. * @license http://www.opensource.org/licenses/mit-license.html MIT License
  28. *
  29. * @version Build @@version@@
  30. */
  31. namespace RandomLib\Mixer;
  32. use SecurityLib\Strength;
  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. * @author Paragon Initiative Enterprises <security@paragonie.com>
  47. */
  48. class XorMixer extends \RandomLib\AbstractMixer
  49. {
  50. /**
  51. * Return an instance of Strength indicating the strength of the source
  52. *
  53. * @return \SecurityLib\Strength An instance of one of the strength classes
  54. */
  55. public static function getStrength()
  56. {
  57. return new Strength(Strength::VERYLOW);
  58. }
  59. /**
  60. * Test to see if the mixer is available
  61. *
  62. * @return bool If the mixer is available on the system
  63. */
  64. public static function test()
  65. {
  66. return true;
  67. }
  68. /**
  69. * Get the block size (the size of the individual blocks used for the mixing)
  70. *
  71. * @return int The block size
  72. */
  73. protected function getPartSize()
  74. {
  75. return 64;
  76. }
  77. /**
  78. * Mix 2 parts together using one method
  79. *
  80. * @param string $part1 The first part to mix
  81. * @param string $part2 The second part to mix
  82. *
  83. * @return string The mixed data
  84. */
  85. protected function mixParts1($part1, $part2)
  86. {
  87. return (string) ($part1 ^ $part2);
  88. }
  89. /**
  90. * Mix 2 parts together using another different method
  91. *
  92. * @param string $part1 The first part to mix
  93. * @param string $part2 The second part to mix
  94. *
  95. * @return string The mixed data
  96. */
  97. protected function mixParts2($part1, $part2)
  98. {
  99. // Both mixers are identical, this is for speed, not security
  100. return (string) ($part1 ^ $part2);
  101. }
  102. }