| 1 |
<?php |
| 2 |
declare(strict_types=1); |
| 3 |
|
| 4 |
if (class_exists('ParagonIE_Sodium_Core_BLAKE2b', false)) { |
| 5 |
return; |
| 6 |
} |
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
abstract class ParagonIE_Sodium_Core_BLAKE2b extends ParagonIE_Sodium_Core_Util |
| 14 |
{ |
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
protected static SplFixedArray $iv; |
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
protected const SIGMA = [ |
| 24 |
[ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15], |
| 25 |
[ 14, 10, 4, 8, 9, 15, 13, 6, 1, 12, 0, 2, 11, 7, 5, 3], |
| 26 |
[ 11, 8, 12, 0, 5, 2, 15, 13, 10, 14, 3, 6, 7, 1, 9, 4], |
| 27 |
[ 7, 9, 3, 1, 13, 12, 11, 14, 2, 6, 5, 10, 4, 0, 15, 8], |
| 28 |
[ 9, 0, 5, 7, 2, 4, 10, 15, 14, 1, 11, 12, 6, 8, 3, 13], |
| 29 |
[ 2, 12, 6, 10, 0, 11, 8, 3, 4, 13, 7, 5, 15, 14, 1, 9], |
| 30 |
[ 12, 5, 1, 15, 14, 13, 4, 10, 0, 7, 6, 3, 9, 2, 8, 11], |
| 31 |
[ 13, 11, 7, 14, 12, 1, 3, 9, 5, 0, 15, 4, 8, 6, 2, 10], |
| 32 |
[ 6, 15, 14, 9, 11, 3, 0, 8, 12, 2, 13, 7, 1, 4, 10, 5], |
| 33 |
[ 10, 2, 8, 4, 7, 6, 1, 5, 15, 11, 9, 14, 3, 12, 13 , 0], |
| 34 |
[ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15], |
| 35 |
[ 14, 10, 4, 8, 9, 15, 13, 6, 1, 12, 0, 2, 11, 7, 5, 3] |
| 36 |
]; |
| 37 |
|
| 38 |
const KEYBYTES = 64; |
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
public static function new64(int $high, int $low): SplFixedArray |
| 50 |
{ |
| 51 |
$i64 = new SplFixedArray(2); |
| 52 |
$i64[0] = $high & 0xffffffff; |
| 53 |
$i64[1] = $low & 0xffffffff; |
| 54 |
return $i64; |
| 55 |
} |
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
protected static function to64(int $num): SplFixedArray |
| 67 |
{ |
| 68 |
list($hi, $lo) = self::numericTo64BitInteger($num); |
| 69 |
return self::new64($hi, $lo); |
| 70 |
} |
| 71 |
|
| 72 |
|
| 73 |
|
| 74 |
|
| 75 |
|
| 76 |
|
| 77 |
|
| 78 |
|
| 79 |
|
| 80 |
|
| 81 |
|
| 82 |
public static function add64(SplFixedArray $x, SplFixedArray $y): SplFixedArray |
| 83 |
{ |
| 84 |
$l = ($x[1] + $y[1]) & 0xffffffff; |
| 85 |
return self::new64( |
| 86 |
(int) ($x[0] + $y[0] + ( |
| 87 |
($l < $x[1]) ? 1 : 0 |
| 88 |
)), |
| 89 |
$l |
| 90 |
); |
| 91 |
} |
| 92 |
|
| 93 |
|
| 94 |
|
| 95 |
|
| 96 |
|
| 97 |
|
| 98 |
|
| 99 |
|
| 100 |
|
| 101 |
|
| 102 |
public static function add364( |
| 103 |
SplFixedArray $x, |
| 104 |
SplFixedArray $y, |
| 105 |
SplFixedArray $z |
| 106 |
): SplFixedArray { |
| 107 |
return self::add64($x, self::add64($y, $z)); |
| 108 |
} |
| 109 |
|
| 110 |
|
| 111 |
|
| 112 |
|
| 113 |
|
| 114 |
|
| 115 |
|
| 116 |
|
| 117 |
|
| 118 |
|
| 119 |
public static function xor64(SplFixedArray $x, SplFixedArray $y): SplFixedArray |
| 120 |
{ |
| 121 |
if (!is_numeric($x[0])) { |
| 122 |
throw new SodiumException('x[0] is not an integer'); |
| 123 |
} |
| 124 |
if (!is_numeric($x[1])) { |
| 125 |
throw new SodiumException('x[1] is not an integer'); |
| 126 |
} |
| 127 |
if (!is_numeric($y[0])) { |
| 128 |
throw new SodiumException('y[0] is not an integer'); |
| 129 |
} |
| 130 |
if (!is_numeric($y[1])) { |
| 131 |
throw new SodiumException('y[1] is not an integer'); |
| 132 |
} |
| 133 |
return self::new64( |
| 134 |
(int) (($x[0] ^ $y[0]) & 0xffffffff), |
| 135 |
(int) (($x[1] ^ $y[1]) & 0xffffffff) |
| 136 |
); |
| 137 |
} |
| 138 |
|
| 139 |
|
| 140 |
|
| 141 |
|
| 142 |
|
| 143 |
|
| 144 |
|
| 145 |
|
| 146 |
|
| 147 |
|
| 148 |
public static function rotr64(SplFixedArray $x, int $c): SplFixedArray |
| 149 |
{ |
| 150 |
if ($c >= 64) { |
| 151 |
$c %= 64; |
| 152 |
} |
| 153 |
if ($c >= 32) { |
| 154 |
|
| 155 |
$tmp = $x[0]; |
| 156 |
$x[0] = $x[1]; |
| 157 |
$x[1] = $tmp; |
| 158 |
$c -= 32; |
| 159 |
} |
| 160 |
if ($c === 0) { |
| 161 |
return $x; |
| 162 |
} |
| 163 |
|
| 164 |
$l0 = 0; |
| 165 |
|
| 166 |
$c = 64 - $c; |
| 167 |
|
| 168 |
if ($c < 32) { |
| 169 |
$h0 = ((int) ($x[0]) << $c) | ( |
| 170 |
( |
| 171 |
(int) ($x[1]) & ((1 << $c) - 1) |
| 172 |
<< |
| 173 |
(32 - $c) |
| 174 |
) >> (32 - $c) |
| 175 |
); |
| 176 |
$l0 = (int) ($x[1]) << $c; |
| 177 |
} else { |
| 178 |
$h0 = (int) ($x[1]) << ($c - 32); |
| 179 |
} |
| 180 |
|
| 181 |
$h1 = 0; |
| 182 |
$c1 = 64 - $c; |
| 183 |
|
| 184 |
if ($c1 < 32) { |
| 185 |
$h1 = (int) ($x[0]) >> $c1; |
| 186 |
$l1 = ((int) ($x[1]) >> $c1) | ((int) ($x[0]) & ((1 << $c1) - 1)) << (32 - $c1); |
| 187 |
} else { |
| 188 |
$l1 = (int) ($x[0]) >> ($c1 - 32); |
| 189 |
} |
| 190 |
|
| 191 |
return self::new64($h0 | $h1, $l0 | $l1); |
| 192 |
} |
| 193 |
|
| 194 |
|
| 195 |
|
| 196 |
|
| 197 |
|
| 198 |
|
| 199 |
|
| 200 |
protected static function flatten64(SplFixedArray $x): int |
| 201 |
{ |
| 202 |
return (int) ($x[0] * 4294967296 + $x[1]); |
| 203 |
} |
| 204 |
|
| 205 |
|
| 206 |
|
| 207 |
|
| 208 |
|
| 209 |
|
| 210 |
|
| 211 |
|
| 212 |
protected static function load64(SplFixedArray $x, int $i): SplFixedArray |
| 213 |
{ |
| 214 |
$l = (int) ($x[$i]) |
| 215 |
| ((int) ($x[$i+1]) << 8) |
| 216 |
| ((int) ($x[$i+2]) << 16) |
| 217 |
| ((int) ($x[$i+3]) << 24); |
| 218 |
$h = (int) ($x[$i+4]) |
| 219 |
| ((int) ($x[$i+5]) << 8) |
| 220 |
| ((int) ($x[$i+6]) << 16) |
| 221 |
| ((int) ($x[$i+7]) << 24); |
| 222 |
return self::new64($h, $l); |
| 223 |
} |
| 224 |
|
| 225 |
|
| 226 |
|
| 227 |
|
| 228 |
|
| 229 |
|
| 230 |
|
| 231 |
|
| 232 |
|
| 233 |
protected static function store64(SplFixedArray $x, int $i, SplFixedArray $u): void |
| 234 |
{ |
| 235 |
$maxLength = $x->getSize() - 1; |
| 236 |
for ($j = 0; $j < 8; ++$j) { |
| 237 |
|
| 238 |
|
| 239 |
|
| 240 |
|
| 241 |
|
| 242 |
$uIdx = ((7 - $j) & 4) >> 2; |
| 243 |
$x[$i] = ((int) ($u[$uIdx]) & 0xff); |
| 244 |
if (++$i > $maxLength) { |
| 245 |
return; |
| 246 |
} |
| 247 |
$u[$uIdx] >>= 8; |
| 248 |
} |
| 249 |
} |
| 250 |
|
| 251 |
|
| 252 |
|
| 253 |
|
| 254 |
|
| 255 |
|
| 256 |
|
| 257 |
|
| 258 |
|
| 259 |
|
| 260 |
public static function pseudoConstructor(): void |
| 261 |
{ |
| 262 |
static $called = false; |
| 263 |
if ($called) { |
| 264 |
return; |
| 265 |
} |
| 266 |
self::$iv = new SplFixedArray(8); |
| 267 |
self::$iv[0] = self::new64(0x6a09e667, 0xf3bcc908); |
| 268 |
self::$iv[1] = self::new64(0xbb67ae85, 0x84caa73b); |
| 269 |
self::$iv[2] = self::new64(0x3c6ef372, 0xfe94f82b); |
| 270 |
self::$iv[3] = self::new64(0xa54ff53a, 0x5f1d36f1); |
| 271 |
self::$iv[4] = self::new64(0x510e527f, 0xade682d1); |
| 272 |
self::$iv[5] = self::new64(0x9b05688c, 0x2b3e6c1f); |
| 273 |
self::$iv[6] = self::new64(0x1f83d9ab, 0xfb41bd6b); |
| 274 |
self::$iv[7] = self::new64(0x5be0cd19, 0x137e2179); |
| 275 |
|
| 276 |
$called = true; |
| 277 |
} |
| 278 |
|
| 279 |
|
| 280 |
|
| 281 |
|
| 282 |
|
| 283 |
|
| 284 |
|
| 285 |
|
| 286 |
|
| 287 |
|
| 288 |
protected static function context(): SplFixedArray |
| 289 |
{ |
| 290 |
$ctx = new SplFixedArray(6); |
| 291 |
$ctx[0] = new SplFixedArray(8); |
| 292 |
$ctx[1] = new SplFixedArray(2); |
| 293 |
$ctx[2] = new SplFixedArray(2); |
| 294 |
$ctx[3] = new SplFixedArray(256); |
| 295 |
$ctx[4] = 0; |
| 296 |
$ctx[5] = 0; |
| 297 |
|
| 298 |
for ($i = 8; $i--;) { |
| 299 |
$ctx[0][$i] = self::$iv[$i]; |
| 300 |
} |
| 301 |
for ($i = 256; $i--;) { |
| 302 |
$ctx[3][$i] = 0; |
| 303 |
} |
| 304 |
|
| 305 |
$zero = self::new64(0, 0); |
| 306 |
$ctx[1][0] = $zero; |
| 307 |
$ctx[1][1] = $zero; |
| 308 |
$ctx[2][0] = $zero; |
| 309 |
$ctx[2][1] = $zero; |
| 310 |
|
| 311 |
return $ctx; |
| 312 |
} |
| 313 |
|
| 314 |
|
| 315 |
|
| 316 |
|
| 317 |
|
| 318 |
|
| 319 |
|
| 320 |
|
| 321 |
|
| 322 |
|
| 323 |
protected static function compress(SplFixedArray $ctx, SplFixedArray $buf): void |
| 324 |
{ |
| 325 |
$m = new SplFixedArray(16); |
| 326 |
$v = new SplFixedArray(16); |
| 327 |
|
| 328 |
for ($i = 16; $i--;) { |
| 329 |
$m[$i] = self::load64($buf, $i << 3); |
| 330 |
} |
| 331 |
|
| 332 |
for ($i = 8; $i--;) { |
| 333 |
$v[$i] = $ctx[0][$i]; |
| 334 |
} |
| 335 |
|
| 336 |
$v[ 8] = self::$iv[0]; |
| 337 |
$v[ 9] = self::$iv[1]; |
| 338 |
$v[10] = self::$iv[2]; |
| 339 |
$v[11] = self::$iv[3]; |
| 340 |
|
| 341 |
$v[12] = self::xor64($ctx[1][0], self::$iv[4]); |
| 342 |
$v[13] = self::xor64($ctx[1][1], self::$iv[5]); |
| 343 |
$v[14] = self::xor64($ctx[2][0], self::$iv[6]); |
| 344 |
$v[15] = self::xor64($ctx[2][1], self::$iv[7]); |
| 345 |
|
| 346 |
for ($r = 0; $r < 12; ++$r) { |
| 347 |
$v = self::G($r, 0, 0, 4, 8, 12, $v, $m); |
| 348 |
$v = self::G($r, 1, 1, 5, 9, 13, $v, $m); |
| 349 |
$v = self::G($r, 2, 2, 6, 10, 14, $v, $m); |
| 350 |
$v = self::G($r, 3, 3, 7, 11, 15, $v, $m); |
| 351 |
$v = self::G($r, 4, 0, 5, 10, 15, $v, $m); |
| 352 |
$v = self::G($r, 5, 1, 6, 11, 12, $v, $m); |
| 353 |
$v = self::G($r, 6, 2, 7, 8, 13, $v, $m); |
| 354 |
$v = self::G($r, 7, 3, 4, 9, 14, $v, $m); |
| 355 |
} |
| 356 |
|
| 357 |
for ($i = 8; $i--;) { |
| 358 |
$ctx[0][$i] = self::xor64( |
| 359 |
$ctx[0][$i], self::xor64($v[$i], $v[$i+8]) |
| 360 |
); |
| 361 |
} |
| 362 |
} |
| 363 |
|
| 364 |
|
| 365 |
|
| 366 |
|
| 367 |
|
| 368 |
|
| 369 |
|
| 370 |
|
| 371 |
|
| 372 |
|
| 373 |
|
| 374 |
|
| 375 |
|
| 376 |
|
| 377 |
|
| 378 |
|
| 379 |
public static function G( |
| 380 |
int $r, |
| 381 |
int $i, |
| 382 |
int $a, |
| 383 |
int $b, |
| 384 |
int $c, |
| 385 |
int $d, |
| 386 |
SplFixedArray $v, |
| 387 |
SplFixedArray $m |
| 388 |
): SplFixedArray { |
| 389 |
$v[$a] = self::add364($v[$a], $v[$b], $m[self::SIGMA[$r][$i << 1]]); |
| 390 |
$v[$d] = self::rotr64(self::xor64($v[$d], $v[$a]), 32); |
| 391 |
$v[$c] = self::add64($v[$c], $v[$d]); |
| 392 |
$v[$b] = self::rotr64(self::xor64($v[$b], $v[$c]), 24); |
| 393 |
$v[$a] = self::add364($v[$a], $v[$b], $m[self::SIGMA[$r][($i << 1) + 1]]); |
| 394 |
$v[$d] = self::rotr64(self::xor64($v[$d], $v[$a]), 16); |
| 395 |
$v[$c] = self::add64($v[$c], $v[$d]); |
| 396 |
$v[$b] = self::rotr64(self::xor64($v[$b], $v[$c]), 63); |
| 397 |
return $v; |
| 398 |
} |
| 399 |
|
| 400 |
|
| 401 |
|
| 402 |
|
| 403 |
|
| 404 |
|
| 405 |
|
| 406 |
|
| 407 |
|
| 408 |
|
| 409 |
public static function increment_counter(SplFixedArray $ctx, int $inc): void |
| 410 |
{ |
| 411 |
if ($inc < 0) { |
| 412 |
throw new SodiumException('Increasing by a negative number makes no sense.'); |
| 413 |
} |
| 414 |
$t = self::to64($inc); |
| 415 |
|
| 416 |
|
| 417 |
|
| 418 |
$ctx[1][0] = self::add64($ctx[1][0], $t); |
| 419 |
|
| 420 |
|
| 421 |
if (self::flatten64($ctx[1][0]) < $inc) { |
| 422 |
$ctx[1][1] = self::add64($ctx[1][1], self::to64(1)); |
| 423 |
} |
| 424 |
} |
| 425 |
|
| 426 |
|
| 427 |
|
| 428 |
|
| 429 |
|
| 430 |
|
| 431 |
|
| 432 |
|
| 433 |
|
| 434 |
|
| 435 |
|
| 436 |
|
| 437 |
public static function update(SplFixedArray $ctx, SplFixedArray $p, int $plen): void |
| 438 |
{ |
| 439 |
self::pseudoConstructor(); |
| 440 |
|
| 441 |
$offset = 0; |
| 442 |
while ($plen > 0) { |
| 443 |
$left = $ctx[4]; |
| 444 |
$fill = 256 - $left; |
| 445 |
|
| 446 |
if ($plen > $fill) { |
| 447 |
|
| 448 |
for ($i = $fill; $i--;) { |
| 449 |
$ctx[3][$i + $left] = $p[(int) ($i + $offset)]; |
| 450 |
} |
| 451 |
|
| 452 |
|
| 453 |
$ctx[4] += $fill; |
| 454 |
|
| 455 |
|
| 456 |
self::increment_counter($ctx, 128); |
| 457 |
|
| 458 |
|
| 459 |
self::compress($ctx, $ctx[3]); |
| 460 |
|
| 461 |
|
| 462 |
for ($i = 128; $i--;) { |
| 463 |
$ctx[3][$i] = $ctx[3][$i + 128]; |
| 464 |
} |
| 465 |
|
| 466 |
|
| 467 |
$ctx[4] -= 128; |
| 468 |
|
| 469 |
|
| 470 |
$offset += $fill; |
| 471 |
|
| 472 |
|
| 473 |
$plen -= $fill; |
| 474 |
} else { |
| 475 |
for ($i = $plen; $i--;) { |
| 476 |
$ctx[3][$i + $left] = $p[(int) ($i + $offset)]; |
| 477 |
} |
| 478 |
$ctx[4] += $plen; |
| 479 |
$offset += $plen; |
| 480 |
$plen -= $plen; |
| 481 |
} |
| 482 |
} |
| 483 |
} |
| 484 |
|
| 485 |
|
| 486 |
|
| 487 |
|
| 488 |
|
| 489 |
|
| 490 |
|
| 491 |
|
| 492 |
|
| 493 |
|
| 494 |
|
| 495 |
public static function finish(SplFixedArray $ctx, SplFixedArray $out): SplFixedArray |
| 496 |
{ |
| 497 |
self::pseudoConstructor(); |
| 498 |
if ($ctx[4] > 128) { |
| 499 |
self::increment_counter($ctx, 128); |
| 500 |
self::compress($ctx, $ctx[3]); |
| 501 |
$ctx[4] -= 128; |
| 502 |
if ($ctx[4] > 128) { |
| 503 |
throw new SodiumException('Failed to assert that buflen <= 128 bytes'); |
| 504 |
} |
| 505 |
for ($i = $ctx[4]; $i--;) { |
| 506 |
$ctx[3][$i] = $ctx[3][$i + 128]; |
| 507 |
} |
| 508 |
} |
| 509 |
|
| 510 |
self::increment_counter($ctx, $ctx[4]); |
| 511 |
$ctx[2][0] = self::new64(0xffffffff, 0xffffffff); |
| 512 |
|
| 513 |
for ($i = 256 - $ctx[4]; $i--;) { |
| 514 |
$ctx[3][$i+$ctx[4]] = 0; |
| 515 |
} |
| 516 |
|
| 517 |
self::compress($ctx, $ctx[3]); |
| 518 |
|
| 519 |
$i = (int) (($out->getSize() - 1) / 8); |
| 520 |
for (; $i >= 0; --$i) { |
| 521 |
self::store64($out, $i << 3, $ctx[0][$i]); |
| 522 |
} |
| 523 |
return $out; |
| 524 |
} |
| 525 |
|
| 526 |
|
| 527 |
|
| 528 |
|
| 529 |
|
| 530 |
|
| 531 |
|
| 532 |
|
| 533 |
|
| 534 |
|
| 535 |
|
| 536 |
|
| 537 |
|
| 538 |
public static function init( |
| 539 |
?SplFixedArray $key = null, |
| 540 |
int $outlen = 64, |
| 541 |
?SplFixedArray $salt = null, |
| 542 |
?SplFixedArray $personal = null |
| 543 |
): SplFixedArray { |
| 544 |
self::pseudoConstructor(); |
| 545 |
$klen = 0; |
| 546 |
|
| 547 |
if ($key !== null) { |
| 548 |
if (count($key) > 64) { |
| 549 |
throw new SodiumException('Invalid key size'); |
| 550 |
} |
| 551 |
$klen = count($key); |
| 552 |
} |
| 553 |
|
| 554 |
if ($outlen > 64) { |
| 555 |
throw new SodiumException('Invalid output size'); |
| 556 |
} |
| 557 |
|
| 558 |
$ctx = self::context(); |
| 559 |
|
| 560 |
$p = new SplFixedArray(64); |
| 561 |
|
| 562 |
for ($i = 64; --$i;) { |
| 563 |
$p[$i] = 0; |
| 564 |
} |
| 565 |
|
| 566 |
$p[0] = $outlen; |
| 567 |
$p[1] = $klen; |
| 568 |
$p[2] = 1; |
| 569 |
$p[3] = 1; |
| 570 |
|
| 571 |
if ($salt instanceof SplFixedArray) { |
| 572 |
|
| 573 |
for ($i = 0; $i < 16; ++$i) { |
| 574 |
$p[32 + $i] = (int) $salt[$i]; |
| 575 |
} |
| 576 |
} |
| 577 |
if ($personal instanceof SplFixedArray) { |
| 578 |
|
| 579 |
for ($i = 0; $i < 16; ++$i) { |
| 580 |
$p[48 + $i] = (int) $personal[$i]; |
| 581 |
} |
| 582 |
} |
| 583 |
|
| 584 |
$ctx[0][0] = self::xor64( |
| 585 |
$ctx[0][0], |
| 586 |
self::load64($p, 0) |
| 587 |
); |
| 588 |
if ($salt instanceof SplFixedArray || $personal instanceof SplFixedArray) { |
| 589 |
|
| 590 |
for ($i = 1; $i < 8; ++$i) { |
| 591 |
$ctx[0][$i] = self::xor64( |
| 592 |
$ctx[0][$i], |
| 593 |
self::load64($p, $i << 3) |
| 594 |
); |
| 595 |
} |
| 596 |
} |
| 597 |
|
| 598 |
if ($klen > 0 && $key instanceof SplFixedArray) { |
| 599 |
$block = new SplFixedArray(128); |
| 600 |
for ($i = 128; $i--;) { |
| 601 |
$block[$i] = 0; |
| 602 |
} |
| 603 |
for ($i = $klen; $i--;) { |
| 604 |
$block[$i] = $key[$i]; |
| 605 |
} |
| 606 |
self::update($ctx, $block, 128); |
| 607 |
$ctx[4] = 128; |
| 608 |
} |
| 609 |
|
| 610 |
return $ctx; |
| 611 |
} |
| 612 |
|
| 613 |
|
| 614 |
|
| 615 |
|
| 616 |
|
| 617 |
|
| 618 |
|
| 619 |
|
| 620 |
|
| 621 |
public static function stringToSplFixedArray(string $str = ''): SplFixedArray |
| 622 |
{ |
| 623 |
$values = unpack('C*', $str); |
| 624 |
return SplFixedArray::fromArray(array_values($values)); |
| 625 |
} |
| 626 |
|
| 627 |
|
| 628 |
|
| 629 |
|
| 630 |
|
| 631 |
|
| 632 |
|
| 633 |
|
| 634 |
|
| 635 |
|
| 636 |
public static function SplFixedArrayToString(SplFixedArray $a): string |
| 637 |
{ |
| 638 |
|
| 639 |
|
| 640 |
|
| 641 |
$arr = $a->toArray(); |
| 642 |
$c = $a->count(); |
| 643 |
array_unshift($arr, str_repeat('C', $c)); |
| 644 |
return (string) (call_user_func_array('pack', $arr)); |
| 645 |
} |
| 646 |
|
| 647 |
|
| 648 |
|
| 649 |
|
| 650 |
|
| 651 |
|
| 652 |
|
| 653 |
|
| 654 |
public static function contextToString(SplFixedArray $ctx): string |
| 655 |
{ |
| 656 |
$str = ''; |
| 657 |
|
| 658 |
$ctxA = $ctx[0]->toArray(); |
| 659 |
|
| 660 |
|
| 661 |
for ($i = 0; $i < 8; ++$i) { |
| 662 |
$str .= self::store32_le($ctxA[$i][1]); |
| 663 |
$str .= self::store32_le($ctxA[$i][0]); |
| 664 |
} |
| 665 |
|
| 666 |
|
| 667 |
|
| 668 |
for ($i = 1; $i < 3; ++$i) { |
| 669 |
$ctxA = $ctx[$i]->toArray(); |
| 670 |
$str .= self::store32_le($ctxA[0][1]); |
| 671 |
$str .= self::store32_le($ctxA[0][0]); |
| 672 |
$str .= self::store32_le($ctxA[1][1]); |
| 673 |
$str .= self::store32_le($ctxA[1][0]); |
| 674 |
} |
| 675 |
|
| 676 |
|
| 677 |
$str .= self::SplFixedArrayToString($ctx[3]); |
| 678 |
|
| 679 |
|
| 680 |
$ctx4 = (int) $ctx[4]; |
| 681 |
|
| 682 |
|
| 683 |
$str .= implode('', array( |
| 684 |
self::intToChr($ctx4 & 0xff), |
| 685 |
self::intToChr(($ctx4 >> 8) & 0xff), |
| 686 |
self::intToChr(($ctx4 >> 16) & 0xff), |
| 687 |
self::intToChr(($ctx4 >> 24) & 0xff), |
| 688 |
self::intToChr(($ctx4 >> 32) & 0xff), |
| 689 |
self::intToChr(($ctx4 >> 40) & 0xff), |
| 690 |
self::intToChr(($ctx4 >> 48) & 0xff), |
| 691 |
self::intToChr(($ctx4 >> 56) & 0xff) |
| 692 |
)); |
| 693 |
|
| 694 |
return $str . self::intToChr($ctx[5]) . str_repeat("\x00", 23); |
| 695 |
} |
| 696 |
|
| 697 |
|
| 698 |
|
| 699 |
|
| 700 |
|
| 701 |
|
| 702 |
|
| 703 |
|
| 704 |
|
| 705 |
|
| 706 |
|
| 707 |
|
| 708 |
public static function stringToContext(string $string): SplFixedArray |
| 709 |
{ |
| 710 |
$ctx = self::context(); |
| 711 |
|
| 712 |
|
| 713 |
for ($i = 0; $i < 8; ++$i) { |
| 714 |
$ctx[0][$i] = SplFixedArray::fromArray( |
| 715 |
array( |
| 716 |
self::load_4( |
| 717 |
self::substr($string, (($i << 3) + 4), 4) |
| 718 |
), |
| 719 |
self::load_4( |
| 720 |
self::substr($string, (($i << 3)), 4) |
| 721 |
) |
| 722 |
) |
| 723 |
); |
| 724 |
} |
| 725 |
|
| 726 |
|
| 727 |
|
| 728 |
for ($i = 1; $i < 3; ++$i) { |
| 729 |
$ctx[$i][1] = SplFixedArray::fromArray( |
| 730 |
array( |
| 731 |
self::load_4(self::substr($string, 76 + (($i - 1) << 4), 4)), |
| 732 |
self::load_4(self::substr($string, 72 + (($i - 1) << 4), 4)) |
| 733 |
) |
| 734 |
); |
| 735 |
$ctx[$i][0] = SplFixedArray::fromArray( |
| 736 |
array( |
| 737 |
self::load_4(self::substr($string, 68 + (($i - 1) << 4), 4)), |
| 738 |
self::load_4(self::substr($string, 64 + (($i - 1) << 4), 4)) |
| 739 |
) |
| 740 |
); |
| 741 |
} |
| 742 |
|
| 743 |
|
| 744 |
$ctx[3] = self::stringToSplFixedArray(self::substr($string, 96, 256)); |
| 745 |
|
| 746 |
|
| 747 |
$int = 0; |
| 748 |
for ($i = 0; $i < 8; ++$i) { |
| 749 |
$int |= self::chrToInt($string[352 + $i]) << ($i << 3); |
| 750 |
} |
| 751 |
$ctx[4] = $int; |
| 752 |
|
| 753 |
return $ctx; |
| 754 |
} |
| 755 |
} |