|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace Casper\Util; |
| 4 | + |
| 5 | +class CEP57ChecksumUtil |
| 6 | +{ |
| 7 | + private const BLAKE2B_HASH_LENGTH = 64; |
| 8 | + private const HEX_CHARS = ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f']; |
| 9 | + |
| 10 | + /** |
| 11 | + * @param string $hex |
| 12 | + * @return bool |
| 13 | + * |
| 14 | + * @throws \Exception |
| 15 | + */ |
| 16 | + public static function hasChecksum(string $hex): bool |
| 17 | + { |
| 18 | + $mix = 0; |
| 19 | + |
| 20 | + foreach (str_split($hex) as $char) { |
| 21 | + if ($char >= '0' && $char <= '9') { |
| 22 | + $mix |= 0x00; |
| 23 | + } |
| 24 | + elseif ($char >= 'a' && $char <= 'f') { |
| 25 | + $mix |= 0x01; |
| 26 | + } |
| 27 | + elseif ($char >= 'A' && $char <= 'F') { |
| 28 | + $mix |= 0x02; |
| 29 | + } |
| 30 | + else { |
| 31 | + throw new \Exception('Input is not an hexadecimal string'); |
| 32 | + } |
| 33 | + } |
| 34 | + |
| 35 | + return $mix > 2; |
| 36 | + } |
| 37 | + |
| 38 | + /** |
| 39 | + * @param array $bytes |
| 40 | + * @return string |
| 41 | + * |
| 42 | + * @throws \Exception |
| 43 | + */ |
| 44 | + public static function encode(array $bytes): string |
| 45 | + { |
| 46 | + $nibbles = self::byteToNibbles($bytes); |
| 47 | + $hashBits = self::bytesToBitsCycle(HashUtil::blake2bHash($bytes, self::BLAKE2B_HASH_LENGTH)); |
| 48 | + |
| 49 | + $bitIndex = 0; |
| 50 | + foreach ($nibbles as $nibble) { |
| 51 | + $char = self::HEX_CHARS[$nibble]; |
| 52 | + $result[] = ($char >= 'a' && $char <= 'f') && $hashBits[$bitIndex++] |
| 53 | + ? strtoupper($char) |
| 54 | + : $char; |
| 55 | + } |
| 56 | + |
| 57 | + return implode($result ?? []); |
| 58 | + } |
| 59 | + |
| 60 | + /** |
| 61 | + * @param string $hex |
| 62 | + * @return array |
| 63 | + * |
| 64 | + * @throws \Exception |
| 65 | + */ |
| 66 | + public static function decode(string $hex): array |
| 67 | + { |
| 68 | + $bytes = ByteUtil::hexToByteArray($hex); |
| 69 | + |
| 70 | + if (!self::hasChecksum($hex)) { |
| 71 | + return $bytes; |
| 72 | + } |
| 73 | + |
| 74 | + $encoded = self::encode($bytes); |
| 75 | + |
| 76 | + if ($encoded !== $hex) { |
| 77 | + throw new \Exception('Invalid checksum'); |
| 78 | + } |
| 79 | + |
| 80 | + return $bytes; |
| 81 | + } |
| 82 | + |
| 83 | + /** |
| 84 | + * @param array $bytes |
| 85 | + * @return int[] |
| 86 | + * |
| 87 | + * @throws \Exception |
| 88 | + */ |
| 89 | + private static function byteToNibbles(array $bytes): array |
| 90 | + { |
| 91 | + foreach ($bytes as $byte) { |
| 92 | + $nibbles[] = $byte >> 4; |
| 93 | + $nibbles[] = $byte & 0x0F; |
| 94 | + } |
| 95 | + |
| 96 | + return $nibbles; |
| 97 | + } |
| 98 | + |
| 99 | + /** |
| 100 | + * @param array $bytes |
| 101 | + * @return int[] |
| 102 | + */ |
| 103 | + private static function bytesToBitsCycle(array $bytes): array |
| 104 | + { |
| 105 | + for ($i = 0; $i < count($bytes); $i++) { |
| 106 | + for ($j = 0; $j < 8; $j++) { |
| 107 | + $bits[] = ($bytes[$i] >> $j) & 0x01; |
| 108 | + } |
| 109 | + } |
| 110 | + |
| 111 | + return $bits; |
| 112 | + } |
| 113 | +} |
0 commit comments