Crc64.php 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. <?php
  2. namespace AlibabaCloud\Tea\OSSUtils;
  3. class Crc64
  4. {
  5. private static $crc64tab = [];
  6. private $value = 0;
  7. public function __construct()
  8. {
  9. if (!self::$crc64tab) {
  10. $crc64tab = [];
  11. $poly64rev = (0xC96C5795 << 32) | 0xD7870F42;
  12. for ($n = 0; $n < 256; ++$n) {
  13. $crc = $n;
  14. for ($k = 0; $k < 8; ++$k) {
  15. if ($crc & true) {
  16. $crc = ($crc >> 1) & ~(0x8 << 60) ^ $poly64rev;
  17. } else {
  18. $crc = ($crc >> 1) & ~(0x8 << 60);
  19. }
  20. }
  21. $crc64tab[$n] = $crc;
  22. }
  23. self::$crc64tab = $crc64tab;
  24. }
  25. }
  26. public function append($string)
  27. {
  28. for ($i = 0; $i < \strlen($string); ++$i) {
  29. $this->value = ~$this->value;
  30. $this->value = $this->value(\ord($string[$i]), $this->value);
  31. $this->value = ~$this->value;
  32. }
  33. }
  34. public function getValue()
  35. {
  36. return (string) (sprintf('%u', $this->value));
  37. }
  38. private function value($byte, $crc)
  39. {
  40. return self::$crc64tab[($crc ^ $byte) & 0xff] ^ (($crc >> 8) & ~(0xff << 56));
  41. }
  42. }