Builtin.php
3 years ago
CRC32.php
3 years ago
CRCInterface.php
3 years ago
CRCTrait.php
3 years ago
Google.php
3 years ago
PHP.php
3 years ago
PHPSlicedBy4.php
3 years ago
Table.php
3 years ago
Builtin.php
88 lines
| 1 | <?php |
| 2 | /** |
| 3 | * Copyright 2019 Google LLC |
| 4 | * |
| 5 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | * you may not use this file except in compliance with the License. |
| 7 | * You may obtain a copy of the License at |
| 8 | * |
| 9 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | * |
| 11 | * Unless required by applicable law or agreed to in writing, software |
| 12 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | * See the License for the specific language governing permissions and |
| 15 | * limitations under the License. |
| 16 | */ |
| 17 | |
| 18 | namespace Google\CRC32; |
| 19 | |
| 20 | use Google\CRC32\CRC32; |
| 21 | use Google\CRC32\CRCInterface; |
| 22 | |
| 23 | /** |
| 24 | * A CRC32 implementation based on the PHP hash functions. |
| 25 | */ |
| 26 | final class Builtin implements CRCInterface |
| 27 | { |
| 28 | private $hc; |
| 29 | |
| 30 | private static $mapping = [ |
| 31 | CRC32::IEEE => 'crc32b', |
| 32 | CRC32::CASTAGNOLI => 'crc32c', |
| 33 | ]; |
| 34 | |
| 35 | /** |
| 36 | * Returns true if this $polynomial is supported by the builtin PHP hash function. |
| 37 | * |
| 38 | * @param integer $polynomial The polynomial |
| 39 | * |
| 40 | * @return boolean |
| 41 | */ |
| 42 | public static function supports($polynomial) |
| 43 | { |
| 44 | if (!isset(self::$mapping[$polynomial])) { |
| 45 | return false; |
| 46 | } |
| 47 | $algo = self::$mapping[$polynomial]; |
| 48 | return in_array($algo, hash_algos()); |
| 49 | } |
| 50 | |
| 51 | public function __construct($polynomial) |
| 52 | { |
| 53 | if (!self::supports($polynomial)) { |
| 54 | throw new \InvalidArgumentException("hash_algos() does not list this polynomial."); |
| 55 | } |
| 56 | |
| 57 | $this->algo = self::$mapping[$polynomial]; |
| 58 | $this->reset(); |
| 59 | } |
| 60 | |
| 61 | public function reset() |
| 62 | { |
| 63 | $this->hc = hash_init($this->algo); |
| 64 | } |
| 65 | |
| 66 | public function update($data) |
| 67 | { |
| 68 | hash_update($this->hc, $data); |
| 69 | } |
| 70 | |
| 71 | public function hash($raw_output = null) |
| 72 | { |
| 73 | // hash_final will destory the Hash Context resource, so operate on a copy. |
| 74 | $hc = hash_copy($this->hc); |
| 75 | return hash_final($hc, $raw_output); |
| 76 | } |
| 77 | |
| 78 | public function version() |
| 79 | { |
| 80 | return $this->algo . ' PHP HASH'; |
| 81 | } |
| 82 | |
| 83 | public function __clone() |
| 84 | { |
| 85 | $this->hc = hash_copy($this->hc); |
| 86 | } |
| 87 | } |
| 88 |