AbstractMixer.php 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  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. * An abstract mixer to implement a common mixing strategy
  12. *
  13. * PHP version 5.3
  14. *
  15. * @category PHPSecurityLib
  16. * @package Random
  17. *
  18. * @author Anthony Ferrara <ircmaxell@ircmaxell.com>
  19. * @author Paragon Initiative Enterprises <security@paragonie.com>
  20. * @copyright 2011 The Authors
  21. * @license http://www.opensource.org/licenses/mit-license.html MIT License
  22. *
  23. * @version Build @@version@@
  24. */
  25. namespace RandomLib;
  26. use SecurityLib\Util;
  27. /**
  28. * An abstract mixer to implement a common mixing strategy
  29. *
  30. * @see http://tools.ietf.org/html/rfc4086#section-5.2
  31. *
  32. * @category PHPSecurityLib
  33. * @package Random
  34. *
  35. * @author Anthony Ferrara <ircmaxell@ircmaxell.com>
  36. * @author Paragon Initiative Enterprises <security@paragonie.com>
  37. */
  38. abstract class AbstractMixer implements \RandomLib\Mixer
  39. {
  40. /**
  41. * Get the block size (the size of the individual blocks used for the mixing)
  42. *
  43. * @return int The block size
  44. */
  45. abstract protected function getPartSize();
  46. /**
  47. * Mix 2 parts together using one method
  48. *
  49. * @param string $part1 The first part to mix
  50. * @param string $part2 The second part to mix
  51. *
  52. * @return string The mixed data
  53. */
  54. abstract protected function mixParts1($part1, $part2);
  55. /**
  56. * Mix 2 parts together using another different method
  57. *
  58. * @param string $part1 The first part to mix
  59. * @param string $part2 The second part to mix
  60. *
  61. * @return string The mixed data
  62. */
  63. abstract protected function mixParts2($part1, $part2);
  64. /**
  65. * @return bool
  66. */
  67. public static function advisable()
  68. {
  69. return static::test();
  70. }
  71. /**
  72. * Mix the provided array of strings into a single output of the same size
  73. *
  74. * All elements of the array should be the same size.
  75. *
  76. * @param array<int, string> $parts The parts to be mixed
  77. *
  78. * @return string The mixed result
  79. * @psalm-suppress MixedArgument
  80. */
  81. public function mix(array $parts)
  82. {
  83. if (empty($parts)) {
  84. return '';
  85. }
  86. /** @var int $len */
  87. $len = Util::safeStrlen($parts[0]);
  88. /** @var array<int, array<int, string>> $parts */
  89. $parts = $this->normalizeParts($parts);
  90. $stringSize = \count($parts[0]);
  91. $partsSize = \count($parts);
  92. /** @var string $result */
  93. $result = '';
  94. /** @var int $offset */
  95. $offset = 0;
  96. for ($i = 0; $i < $stringSize; ++$i) {
  97. /** @var string $stub */
  98. $stub = (string) $parts[$offset][$i];
  99. for ($j = 1; $j < $partsSize; ++$j) {
  100. /** @var string $newKey */
  101. $newKey = $parts[($j + $offset) % $partsSize][$i];
  102. //Alternately mix the output for each source
  103. if ($j % 2 == 1) {
  104. $stub ^= $this->mixParts1($stub, $newKey);
  105. } else {
  106. $stub ^= $this->mixParts2($stub, $newKey);
  107. }
  108. }
  109. $result .= $stub;
  110. $offset = ($offset + 1) % $partsSize;
  111. }
  112. /** @var string $final */
  113. $final = Util::safeSubstr($result, 0, $len);
  114. return $final;
  115. }
  116. /**
  117. * Normalize the part array and split it block part size.
  118. *
  119. * This will make all parts the same length and a multiple
  120. * of the part size
  121. *
  122. * @param array<int, string> $parts The parts to normalize
  123. *
  124. * @return array The normalized and split parts
  125. * @psalm-suppress MissingClosureReturnType
  126. * @psalm-suppress UntypedParam
  127. * @psalm-suppress MissingArgument
  128. */
  129. protected function normalizeParts(array $parts)
  130. {
  131. if (empty($parts)) {
  132. return $parts;
  133. }
  134. $blockSize = $this->getPartSize();
  135. $callback =
  136. /**
  137. * @var callable $callback
  138. * @param string $value
  139. * @return int
  140. */
  141. function ($value) {
  142. return (int) Util::safeStrlen($value);
  143. };
  144. $mapped = array_map($callback, $parts);
  145. if (count($mapped) < 1) {
  146. return array();
  147. }
  148. /** @var int $maxSize */
  149. $maxSize = count($mapped) > 1
  150. ? max($mapped)
  151. : array_shift($mapped);
  152. if ($maxSize % $blockSize != 0) {
  153. $maxSize += $blockSize - ($maxSize % $blockSize);
  154. }
  155. foreach ($parts as &$part) {
  156. $part = $this->str_pad($part, $maxSize, chr(0));
  157. $part = str_split($part, $blockSize);
  158. }
  159. return $parts;
  160. }
  161. /**
  162. * @param string $string
  163. * @param int $size
  164. * @param string $character
  165. * @return string
  166. */
  167. private function str_pad($string, $size, $character)
  168. {
  169. $start = Util::safeStrlen($string);
  170. $inc = Util::safeStrlen($character);
  171. for ($i = $start; $i < $size; $i+= $inc) {
  172. $string = $string . $character;
  173. }
  174. return Util::safeSubstr($string, 0, $size);
  175. }
  176. }