| 1 |
<?php |
| 2 |
|
| 3 |
namespace TablePress\PhpOffice\PhpSpreadsheet\Reader\Xls; |
| 4 |
|
| 5 |
use TablePress\PhpOffice\PhpSpreadsheet\Reader\Exception as ReaderException; |
| 6 |
|
| 7 |
class MD5 |
| 8 |
{ |
| 9 |
private int $a; |
| 10 |
|
| 11 |
private int $b; |
| 12 |
|
| 13 |
private int $c; |
| 14 |
|
| 15 |
private int $d; |
| 16 |
|
| 17 |
private static int $allOneBits; |
| 18 |
|
| 19 |
/** |
| 20 |
* MD5 stream constructor. |
| 21 |
*/ |
| 22 |
public function __construct() |
| 23 |
{ |
| 24 |
self::$allOneBits = self::signedInt(0xFFFFFFFF); |
| 25 |
$this->reset(); |
| 26 |
} |
| 27 |
|
| 28 |
/** |
| 29 |
* Reset the MD5 stream context. |
| 30 |
*/ |
| 31 |
public function reset(): void |
| 32 |
{ |
| 33 |
$this->a = 0x67452301; |
| 34 |
$this->b = self::signedInt(0xEFCDAB89); |
| 35 |
$this->c = self::signedInt(0x98BADCFE); |
| 36 |
$this->d = 0x10325476; |
| 37 |
} |
| 38 |
|
| 39 |
/** |
| 40 |
* Get MD5 stream context. |
| 41 |
*/ |
| 42 |
public function getContext(): string |
| 43 |
{ |
| 44 |
$s = ''; |
| 45 |
foreach (['a', 'b', 'c', 'd'] as $i) { |
| 46 |
$v = $this->{$i}; |
| 47 |
$s .= chr($v & 0xFF); |
| 48 |
$s .= chr(($v >> 8) & 0xFF); |
| 49 |
$s .= chr(($v >> 16) & 0xFF); |
| 50 |
$s .= chr(($v >> 24) & 0xFF); |
| 51 |
} |
| 52 |
|
| 53 |
return $s; |
| 54 |
} |
| 55 |
|
| 56 |
/** |
| 57 |
* Add data to context. |
| 58 |
* |
| 59 |
* @param string $data Data to add |
| 60 |
*/ |
| 61 |
public function add(string $data): void |
| 62 |
{ |
| 63 |
if (!unpack('V16', $data)) { |
| 64 |
throw new ReaderException('unable to unpack data'); |
| 65 |
} |
| 66 |
$unpacked = unpack('V16', $data); |
| 67 |
/** @var int[] */ |
| 68 |
$words = array_values($unpacked); |
| 69 |
|
| 70 |
$A = $this->a; |
| 71 |
$B = $this->b; |
| 72 |
$C = $this->c; |
| 73 |
$D = $this->d; |
| 74 |
|
| 75 |
$F = [self::class, 'f']; |
| 76 |
$G = [self::class, 'g']; |
| 77 |
$H = [self::class, 'h']; |
| 78 |
$I = [self::class, 'i']; |
| 79 |
|
| 80 |
// ROUND 1 |
| 81 |
self::step($F, $A, $B, $C, $D, $words[0], 7, 0xD76AA478); |
| 82 |
self::step($F, $D, $A, $B, $C, $words[1], 12, 0xE8C7B756); |
| 83 |
self::step($F, $C, $D, $A, $B, $words[2], 17, 0x242070DB); |
| 84 |
self::step($F, $B, $C, $D, $A, $words[3], 22, 0xC1BDCEEE); |
| 85 |
self::step($F, $A, $B, $C, $D, $words[4], 7, 0xF57C0FAF); |
| 86 |
self::step($F, $D, $A, $B, $C, $words[5], 12, 0x4787C62A); |
| 87 |
self::step($F, $C, $D, $A, $B, $words[6], 17, 0xA8304613); |
| 88 |
self::step($F, $B, $C, $D, $A, $words[7], 22, 0xFD469501); |
| 89 |
self::step($F, $A, $B, $C, $D, $words[8], 7, 0x698098D8); |
| 90 |
self::step($F, $D, $A, $B, $C, $words[9], 12, 0x8B44F7AF); |
| 91 |
self::step($F, $C, $D, $A, $B, $words[10], 17, 0xFFFF5BB1); |
| 92 |
self::step($F, $B, $C, $D, $A, $words[11], 22, 0x895CD7BE); |
| 93 |
self::step($F, $A, $B, $C, $D, $words[12], 7, 0x6B901122); |
| 94 |
self::step($F, $D, $A, $B, $C, $words[13], 12, 0xFD987193); |
| 95 |
self::step($F, $C, $D, $A, $B, $words[14], 17, 0xA679438E); |
| 96 |
self::step($F, $B, $C, $D, $A, $words[15], 22, 0x49B40821); |
| 97 |
|
| 98 |
// ROUND 2 |
| 99 |
self::step($G, $A, $B, $C, $D, $words[1], 5, 0xF61E2562); |
| 100 |
self::step($G, $D, $A, $B, $C, $words[6], 9, 0xC040B340); |
| 101 |
self::step($G, $C, $D, $A, $B, $words[11], 14, 0x265E5A51); |
| 102 |
self::step($G, $B, $C, $D, $A, $words[0], 20, 0xE9B6C7AA); |
| 103 |
self::step($G, $A, $B, $C, $D, $words[5], 5, 0xD62F105D); |
| 104 |
self::step($G, $D, $A, $B, $C, $words[10], 9, 0x02441453); |
| 105 |
self::step($G, $C, $D, $A, $B, $words[15], 14, 0xD8A1E681); |
| 106 |
self::step($G, $B, $C, $D, $A, $words[4], 20, 0xE7D3FBC8); |
| 107 |
self::step($G, $A, $B, $C, $D, $words[9], 5, 0x21E1CDE6); |
| 108 |
self::step($G, $D, $A, $B, $C, $words[14], 9, 0xC33707D6); |
| 109 |
self::step($G, $C, $D, $A, $B, $words[3], 14, 0xF4D50D87); |
| 110 |
self::step($G, $B, $C, $D, $A, $words[8], 20, 0x455A14ED); |
| 111 |
self::step($G, $A, $B, $C, $D, $words[13], 5, 0xA9E3E905); |
| 112 |
self::step($G, $D, $A, $B, $C, $words[2], 9, 0xFCEFA3F8); |
| 113 |
self::step($G, $C, $D, $A, $B, $words[7], 14, 0x676F02D9); |
| 114 |
self::step($G, $B, $C, $D, $A, $words[12], 20, 0x8D2A4C8A); |
| 115 |
|
| 116 |
// ROUND 3 |
| 117 |
self::step($H, $A, $B, $C, $D, $words[5], 4, 0xFFFA3942); |
| 118 |
self::step($H, $D, $A, $B, $C, $words[8], 11, 0x8771F681); |
| 119 |
self::step($H, $C, $D, $A, $B, $words[11], 16, 0x6D9D6122); |
| 120 |
self::step($H, $B, $C, $D, $A, $words[14], 23, 0xFDE5380C); |
| 121 |
self::step($H, $A, $B, $C, $D, $words[1], 4, 0xA4BEEA44); |
| 122 |
self::step($H, $D, $A, $B, $C, $words[4], 11, 0x4BDECFA9); |
| 123 |
self::step($H, $C, $D, $A, $B, $words[7], 16, 0xF6BB4B60); |
| 124 |
self::step($H, $B, $C, $D, $A, $words[10], 23, 0xBEBFBC70); |
| 125 |
self::step($H, $A, $B, $C, $D, $words[13], 4, 0x289B7EC6); |
| 126 |
self::step($H, $D, $A, $B, $C, $words[0], 11, 0xEAA127FA); |
| 127 |
self::step($H, $C, $D, $A, $B, $words[3], 16, 0xD4EF3085); |
| 128 |
self::step($H, $B, $C, $D, $A, $words[6], 23, 0x04881D05); |
| 129 |
self::step($H, $A, $B, $C, $D, $words[9], 4, 0xD9D4D039); |
| 130 |
self::step($H, $D, $A, $B, $C, $words[12], 11, 0xE6DB99E5); |
| 131 |
self::step($H, $C, $D, $A, $B, $words[15], 16, 0x1FA27CF8); |
| 132 |
self::step($H, $B, $C, $D, $A, $words[2], 23, 0xC4AC5665); |
| 133 |
|
| 134 |
// ROUND 4 |
| 135 |
self::step($I, $A, $B, $C, $D, $words[0], 6, 0xF4292244); |
| 136 |
self::step($I, $D, $A, $B, $C, $words[7], 10, 0x432AFF97); |
| 137 |
self::step($I, $C, $D, $A, $B, $words[14], 15, 0xAB9423A7); |
| 138 |
self::step($I, $B, $C, $D, $A, $words[5], 21, 0xFC93A039); |
| 139 |
self::step($I, $A, $B, $C, $D, $words[12], 6, 0x655B59C3); |
| 140 |
self::step($I, $D, $A, $B, $C, $words[3], 10, 0x8F0CCC92); |
| 141 |
self::step($I, $C, $D, $A, $B, $words[10], 15, 0xFFEFF47D); |
| 142 |
self::step($I, $B, $C, $D, $A, $words[1], 21, 0x85845DD1); |
| 143 |
self::step($I, $A, $B, $C, $D, $words[8], 6, 0x6FA87E4F); |
| 144 |
self::step($I, $D, $A, $B, $C, $words[15], 10, 0xFE2CE6E0); |
| 145 |
self::step($I, $C, $D, $A, $B, $words[6], 15, 0xA3014314); |
| 146 |
self::step($I, $B, $C, $D, $A, $words[13], 21, 0x4E0811A1); |
| 147 |
self::step($I, $A, $B, $C, $D, $words[4], 6, 0xF7537E82); |
| 148 |
self::step($I, $D, $A, $B, $C, $words[11], 10, 0xBD3AF235); |
| 149 |
self::step($I, $C, $D, $A, $B, $words[2], 15, 0x2AD7D2BB); |
| 150 |
self::step($I, $B, $C, $D, $A, $words[9], 21, 0xEB86D391); |
| 151 |
|
| 152 |
$this->a = ($this->a + $A) & self::$allOneBits; |
| 153 |
$this->b = ($this->b + $B) & self::$allOneBits; |
| 154 |
$this->c = ($this->c + $C) & self::$allOneBits; |
| 155 |
$this->d = ($this->d + $D) & self::$allOneBits; |
| 156 |
} |
| 157 |
|
| 158 |
private static function f(int $X, int $Y, int $Z): int |
| 159 |
{ |
| 160 |
return ($X & $Y) | ((~$X) & $Z); // X AND Y OR NOT X AND Z |
| 161 |
} |
| 162 |
|
| 163 |
private static function g(int $X, int $Y, int $Z): int |
| 164 |
{ |
| 165 |
return ($X & $Z) | ($Y & (~$Z)); // X AND Z OR Y AND NOT Z |
| 166 |
} |
| 167 |
|
| 168 |
private static function h(int $X, int $Y, int $Z): int |
| 169 |
{ |
| 170 |
return $X ^ $Y ^ $Z; // X XOR Y XOR Z |
| 171 |
} |
| 172 |
|
| 173 |
private static function i(int $X, int $Y, int $Z): int |
| 174 |
{ |
| 175 |
return $Y ^ ($X | (~$Z)); // Y XOR (X OR NOT Z) |
| 176 |
} |
| 177 |
|
| 178 |
/** @param float|int $t may be float on 32-bit system */ |
| 179 |
private static function step(callable $func, int &$A, int $B, int $C, int $D, int $M, int $s, $t): void |
| 180 |
{ |
| 181 |
$t = self::signedInt($t); |
| 182 |
/** @var int */ |
| 183 |
$temp = call_user_func($func, $B, $C, $D); |
| 184 |
$A = (int) ($A + $temp + $M + $t) & self::$allOneBits; |
| 185 |
$A = self::rotate($A, $s); |
| 186 |
$A = (int) ($B + $A) & self::$allOneBits; |
| 187 |
} |
| 188 |
|
| 189 |
/** @param float|int $result may be float on 32-bit system */ |
| 190 |
private static function signedInt($result): int |
| 191 |
{ |
| 192 |
return is_int($result) ? $result : (int) (PHP_INT_MIN + $result - 1 - PHP_INT_MAX); |
| 193 |
} |
| 194 |
|
| 195 |
private static function rotate(int $decimal, int $bits): int |
| 196 |
{ |
| 197 |
$binary = str_pad(decbin($decimal), 32, '0', STR_PAD_LEFT); |
| 198 |
|
| 199 |
return self::signedInt(bindec(substr($binary, $bits) . substr($binary, 0, $bits))); |
| 200 |
} |
| 201 |
} |
| 202 |
|