Decode a string with checksum

@param string $string  Encoded string
@param int    $errmode Error mode

@return int

@throws \RuntimeException
179933
public static function decodeWithChecksum($string, $errmode = self::NORMALIZE_ERRMODE_SILENT)
    {
        $checksum = substr($string, (strlen($string) -1), 1);
        $string = substr($string, 0, strlen($string) - 1);

        $value = static::internalDecode($string, $errmode);
        $checksumValue = static::internalDecode($checksum, self::NORMALIZE_ERRMODE_EXCEPTION, true);

        if ($checksumValue !== ($value % 37)) {
            throw new \RuntimeException("Checksum symbol '$checksum' is not correct value for '$string'");
        }

        return $value;
    }
693938
public static function decode($string)
    {
        $result = json_decode($string, true);

        switch (json_last_error()) {
            case JSON_ERROR_NONE:
                return $result;

            case JSON_ERROR_DEPTH:
                throw new \Exception("Maximum stack depth exceeded");

            case JSON_ERROR_STATE_MISMATCH:
                throw new \Exception("Underflow or the modes mismatch");

            case JSON_ERROR_CTRL_CHAR:
                throw new \Exception("Unexpected control character found");

            case JSON_ERROR_SYNTAX:
                throw new \Exception("Syntax error, malformed JSON");

            case JSON_ERROR_UTF8:
                throw new \Exception("Malformed UTF-8 characters, possibly incorrectly encoded");
            
            default:
                throw new \Exception("Unknown error on JSON file");
        }
    }
262924
public function decode(string $value): ?int
    {
        $value = $this->hashids->decode($value);

        if (is_array($value) && sizeof($value) > 0) {
            return $value[0];
        } else {
            return null;
        }
    }
235705
public function encode($string)
    {

        if (empty($string) || empty($this->key)) {
            throw new \Exception("Can not encrypt with an empty key or no data");
            return false;
        }

        $publicKey = $this->generateKey($string);
        $privateKey = $this->key;


        //Get a SHA-1 hashKey 
        $hashKey = sha1($privateKey . "+" . (string)$publicKey);

        $stringArray = str_split($string);
        $hashArray = str_split($hashKey);
        $cipherNoise = str_split($publicKey, 2);

        $counter = 0;

        for ($i = 0; $i < sizeof($stringArray); $i++) {
            if ($counter > 40)
                $counter = 0;
            $cryptChar = ord((string)$stringArray[$i]) + ord((string)$hashArray[$counter]);
            $cryptChar -= floor($cryptChar / 127) * 127;
            $cipherStream[$i] = dechex($cryptChar);
            $counter++;
        }
        //print_R($cipherNoise);

        $cipherNoiseSize = count($cipherNoise);

        $cipher = implode("|x", $cipherStream);
        $cipher .= "|x::|x" . ord((string)$cipherNoiseSize) . "|x";
        $cipher .= implode("|x", $cipherNoise);


        //echo $cipher;

        return $cipher;
    }
890205
public static function decodeRecursive($string, $assoc = false)
    {
        list($firstHex, $firstOctal) = self::analyzeFirstChar($string[0]);

        switch (true) {
            case $firstOctal === '8': // fixmap 1000xxxx
            case $firstHex === 'de': // map 16
            case $firstHex === 'df': // map 32
                return self::decodeMap($string, $assoc);
            case $firstOctal === '9': // fixarray 1001xxxx
            case $firstHex === 'dc': // array 16
            case $firstHex === 'dd': // array 32
                return self::decodeArray($string, $assoc);
            case hexdec($firstHex) < pow(2, 7): // fixint 0xxxxxxx
            case hexdec($firstOctal) > 13: // negative fixint 111xxxxx
            case $firstHex === 'cc': // uint 8
            case $firstHex === 'cd': // uint 16
            case $firstHex === 'ce': // uint 32
            case $firstHex === 'cf': // uint 64
            case $firstHex === 'd0': // int 8
            case $firstHex === 'd1': // int 16
            case $firstHex === 'd2': // int 32
            case $firstHex === 'd3': // int 64
                return self::decodeInt($string);
            case in_array(hexdec($firstOctal), array(10, 11)): // fixstr 101xxxxx
            case $firstHex === 'd9': // str 8
            case $firstHex === 'da': // str 16
            case $firstHex === 'db': // str 32
                return self::decodeString($string);
            case $firstHex === 'c2': // false
                return array(false, substr($string, 1));
            case $firstHex === 'c3': // true
                return array(true, substr($string, 1));
            case $firstHex === 'c0': // nil
                return array(null, substr($string, 1));
            default:
                throw new UndecodeableException($firstHex . ' is not a valid identifier for a type.', 1);
        }
    }
1168252
private static function checksum(string $number) : int
    {
        $number = strrev($number);
        $length = strlen($number);

        $sum = 0;

        for ($i = 0; $i < $length; $i++) {
            $value = $number[$i] * ($i % 2 + 1);
            $sum += ($value >= 10 ? $value - 9 : $value);
        }

        return $sum % 10;
    }
654971
public function decode($string)
    {
        if ($this->isSerialized($string)) {
            return $this->unserialize($string);
        }

        $result = json_decode($string, true);
        if (json_last_error() !== JSON_ERROR_NONE) {
            throw new \InvalidArgumentException('Unable to unserialize value.');
        }

        return $result;
    }
844593
public function decode(string $toDecode): string
    {
        return (string) base64_decode(
            $this->addPadding($this->toBase64($toDecode)),
            true
        );
    }
450667
private function getLength(string $string) : ?int
    {
        if (extension_loaded('mbstring') && ! defined('NO_PHP_EXTENSIONS')) { // constant used for tests
            if (! mb_check_encoding($string, 'UTF-8')) {
                return null;
            }

            return mb_strlen($string, 'UTF-8');
        }

        $charLength = 0;

        $length = strlen($string);

        for ($i = 0; $i < $length; $i++) {
            $charLength++;

            $char = ord($string[$i]);

            if ($char < 0x80) { // 0bbbbbbb
                continue;
            }

            if (($char & 0xE0) === 0xC0) { // 110bbbbb
                $n = 1;
            } elseif (($char & 0xF0) === 0xE0) { // 1110bbbb
                $n = 2;
            } elseif (($char & 0xF0) === 0xF0) { // 11110bbb
                $n = 3;
            } else { // invalid char
                return null;
            }

            for ($j = 0; $j < $n; $j++) {
                if (++$i === $length) { // unexpected end of string
                    return null;
                }

                if ((ord($string[$i]) & 0xC0) !== 0x80) { // does not match 10bbbbbb
                    return null;
                }
            }
        }

        return $charLength;
    }
67019
public static function decode($string)
    {
        return ($string === self::NULL) ? null : strtr($string, self::$decodeMap);
    }
451498
protected function getChecksum($string)
    {
        $length = strlen($string);
        $checksum = 0;

        for ($i = 0; $i < $length; ++$i) {
            $checksum += ord($string{$i}) * $i;
        }

        return $checksum;
    }
218191
public static function decode($string)
    {
        if (!is_string($string)) {
            throw new \InvalidArgumentException(sprintf(
                'Accept only string argument but got: "%s"',
                is_object($string) ? get_class($string) : gettype($string)
            ));
        }

        // PHP7 fix - empty string and null cause syntax error
        if (empty($string)) {
            return null;
        }

        $decoded = json_decode($string, true);
        if (JSON_ERROR_NONE !== json_last_error()) {
            throw new \InvalidArgumentException(sprintf(
                'The malformed json given. Error %s and message %s',
                json_last_error(),
                json_last_error_msg()
            ));
        }

        return $decoded;
    }
855601
public function decode($string)
    {
        $i = 0;

        foreach(str_split($string) as $char)
        {
            $i = $i * $this->radix + array_search($char, $this->dictionary);
        }

        return $i;
    }
871491
protected function decrypt($string, $key){
        return rtrim(mcrypt_decrypt(MCRYPT_RIJNDAEL_256, md5($key), base64_decode($string), MCRYPT_MODE_CBC, md5(md5($key))), "\0");
    }
50503
public function decrypt($string){
        $string = explode('|', $string.'|');
        $decoded = base64_decode($string[0]);
        $iv = base64_decode($string[1]);
        if(strlen($iv)!==mcrypt_get_iv_size(MCRYPT_RIJNDAEL_256, MCRYPT_MODE_CBC))
            return false;
        $key = pack('H*', $this->key);
        $decrypted = trim(mcrypt_decrypt(MCRYPT_RIJNDAEL_256, $key, $decoded, MCRYPT_MODE_CBC, $iv));
        $mac = substr($decrypted, -64);
        $decrypted = substr($decrypted, 0, -64);
        $calcmac = hash_hmac('sha256', $decrypted, substr(bin2hex($key), -32));
        if($calcmac!==$mac)
            return false;
        return unserialize($decrypted);
    }
1135251
public static function ord(string $char): int
    {
        $char = mb_convert_encoding($char, 'UTF-32BE', 'UTF-8');

        return (int) unpack('Ncode', $char)['code'];
    }
54059
private function baseDecode($string)
    {
        $b      = 61;
        $base   = '123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
        $limit  = strlen($string);
        $result = strpos($base, $string[0]);
        for ($i = 1; $i < $limit; ++$i) {
            $result = $b * $result + strpos($base, $string[$i]);
        }

        return $result;
    }