public function decode(string $data): string
    {
        $this->validateInput($data);

        $data = str_split($data);
        $data = array_map(function ($character) {
            return strpos($this->options["characters"], $character);
        }, $data);

        $leadingZeroes = 0;
        while (!empty($data) && 0 === $data[0]) {
            $leadingZeroes++;
            array_shift($data);
        }

        $converted = $this->baseConvert($data, 58, 256);

        if (0 < $leadingZeroes) {
            $converted = array_merge(
                array_fill(0, $leadingZeroes, 0),
                $converted
            );
        }

        $decoded = implode("", array_map("chr", $converted));
        if (true === $this->options["check"]) {
            $hash = substr($decoded, 0, -(Base58::CHECKSUM_SIZE));
            $hash = hash("sha256", $hash, true);
            $hash = hash("sha256", $hash, true);
            $checksum = substr($hash, 0, Base58::CHECKSUM_SIZE);

            if (0 !== substr_compare($decoded, $checksum, -(Base58::CHECKSUM_SIZE))) {
                $message = sprintf(
                    'Checksum "%s" does not match the expected "%s"',
                    bin2hex(substr($decoded, -(Base58::CHECKSUM_SIZE))),
                    bin2hex($checksum)
                );
                throw new RuntimeException($message);
            }

            $version = substr($decoded, 0, Base58::VERSION_SIZE);
            $version = ord($version);

            if ($version !==  $this->options["version"]) {
                $message = sprintf(
                    'Version "%s" does not match the expected "%s"',
                    $version,
                    $this->options["version"]
                );
                throw new RuntimeException($message);
            }

            $decoded = substr($decoded, Base58::VERSION_SIZE, -(Base58::CHECKSUM_SIZE));
        }
        return $decoded;
    }
public function decode(string $data): string
    {
        $this->validateInput($data);

        $data = str_split($data);
        $data = array_map(function ($character) {
            return strpos($this->options["characters"], $character);
        }, $data);

        $leadingZeroes = 0;
        while (!empty($data) && 0 === $data[0]) {
            $leadingZeroes++;
            array_shift($data);
        }

        $converted = $this->baseConvert($data, 58, 256);

        if (0 < $leadingZeroes) {
            $converted = array_merge(
                array_fill(0, $leadingZeroes, 0),
                $converted
            );
        }

        $decoded = implode("", array_map("chr", $converted));
        if (true === $this->options["check"]) {
            $hash = substr($decoded, 0, -(Base58::CHECKSUM_SIZE));
            $hash = hash("sha256", $hash, true);
            $hash = hash("sha256", $hash, true);
            $checksum = substr($hash, 0, Base58::CHECKSUM_SIZE);

            if (0 !== substr_compare($decoded, $checksum, -(Base58::CHECKSUM_SIZE))) {
                $message = sprintf(
                    'Checksum "%s" does not match the expected "%s"',
                    bin2hex(substr($decoded, -(Base58::CHECKSUM_SIZE))),
                    bin2hex($checksum)
                );
                throw new RuntimeException($message);
            }

            $version = substr($decoded, 0, Base58::VERSION_SIZE);
            $version = ord($version);

            if ($version !==  $this->options["version"]) {
                $message = sprintf(
                    'Version "%s" does not match the expected "%s"',
                    $version,
                    $this->options["version"]
                );
                throw new RuntimeException($message);
            }

            $decoded = substr($decoded, Base58::VERSION_SIZE, -(Base58::CHECKSUM_SIZE));
        }
        return $decoded;
    }
public function decode ( string $ data ) : string { $ this -> validateInput ( $ data ) ; $ data = str_split ( $ data ) ; $ data = array_map ( function ( $ character ) { return strpos ( $ this -> options [ "characters" ] , $ character ) ; } , $ data ) ; $ leadingZeroes = 0 ; while ( ! empty ( $ data ) && 0 === $ data [ 0 ] ) { $ leadingZeroes ++ ; array_shift ( $ data ) ; } $ converted = $ this -> baseConvert ( $ data , 58 , 256 ) ; if ( 0 < $ leadingZeroes ) { $ converted = array_merge ( array_fill ( 0 , $ leadingZeroes , 0 ) , $ converted ) ; } $ decoded = implode ( "" , array_map ( "chr" , $ converted ) ) ; if ( true === $ this -> options [ "check" ] ) { $ hash = substr ( $ decoded , 0 , - ( Base58 :: CHECKSUM_SIZE ) ) ; $ hash = hash ( "sha256" , $ hash , true ) ; $ hash = hash ( "sha256" , $ hash , true ) ; $ checksum = substr ( $ hash , 0 , Base58 :: CHECKSUM_SIZE ) ; if ( 0 !== substr_compare ( $ decoded , $ checksum , - ( Base58 :: CHECKSUM_SIZE ) ) ) { $ message = sprintf ( 'Checksum "%s" does not match the expected "%s"' , bin2hex ( substr ( $ decoded , - ( Base58 :: CHECKSUM_SIZE ) ) ) , bin2hex ( $ checksum ) ) ; throw new RuntimeException ( $ message ) ; } $ version = substr ( $ decoded , 0 , Base58 :: VERSION_SIZE ) ; $ version = ord ( $ version ) ; if ( $ version !== $ this -> options [ "version" ] ) { $ message = sprintf ( 'Version "%s" does not match the expected "%s"' , $ version , $ this -> options [ "version" ] ) ; throw new RuntimeException ( $ message ) ; } $ decoded = substr ( $ decoded , Base58 :: VERSION_SIZE , - ( Base58 :: CHECKSUM_SIZE ) ) ; } return $ decoded ; }
Decode given base58 string back to data
Decode given base58 string back to data