<commit_before><?php

namespace Bitcoin\Util;

use Bitcoin\Exceptions\Base58ChecksumFailure;

/**
 * Class Base58
 * @package Bitcoin
 */
class Base58
{
    /**
     * @var string
     */
    private static $base58chars = "123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz";

    /**
     * Encode a given hex string in base58
     *
     * @param $hex
     * @return string
     * @throws \Exception
     */
    public static function encode($hex)
    {
        if (Math::mod(strlen($hex), 2) !== '0') {
            throw new \Exception('Data must be of even length');
        }
        $orighex = $hex;

        $decimal = Math::hexDec($hex);
        $return = "";
        while (Math::cmp($decimal, 0) > 0) {
            list($decimal, $rem) = Math::divQr($decimal, 58);
            $return = $return . self::$base58chars[$rem];
        }
        $return = strrev($return);

        //leading zeros
        for ($i = 0; $i < strlen($orighex) && substr($orighex, $i, 2) == "00"; $i += 2) {
            $return = "1" . $return;
        }
        return $return;
    }

    /**
     * Decode a base58 string
     *
     * @param $base58
     * @return string
     */
    public static function decode($base58)
    {
        if (strlen($base58) == 0) {
            return '';
        }

        $original = $base58;
        $return = '0';

        for ($i = 0; $i < strlen($base58); $i++) {
            $return = Math::add(Math::mul($return, 58), strpos(self::$base58chars, $base58[$i]));
        }

        $hex = ($return == '0') ? '' : Math::decHex($return);

        for ($i = 0; $i < strlen($original) && $original[$i] == "1"; $i++) {
            $hex = "00" . $hex;
        }

        if (Math::mod(strlen($hex), 2) !== '0') {
            $hex = "0" . $hex;
        }

        return $hex;
    }

    /**
     * Calculate a checksum for the given data
     *
     * @param $data
     * @return string
     */
    public static function checksum($data)
    {
        $data = pack("H*", $data);
        $hash = Hash::sha256d($data);
        $checksum = substr($hash, 0, 8);

        return $checksum;
    }

    /**
     * Decode a base58 checksum string and validate checksum
     *
     * @param $base58
     * @return string
     * @throws Base58ChecksumFailure
     */
    public static function decodeCheck($base58)
    {
        $hex       = self::decode($base58);
        $cs_verify = substr($hex, -8);

        $data      = substr($hex, 0, -8);
        $checksum  = self::checksum($data);

        if ($checksum != $cs_verify) {
            throw new Base58ChecksumFailure('Failed to verify checksum');
        }

        return $data;
    }

    /**
     * Encode the given data in base58, with a checksum to check integrity.
     *
     * @param $data
     * @return string
     * @throws \Exception
     */
    public static function encodeCheck($data)
    {
        $checksum = self::checksum($data);
        $hex = $data . $checksum;
        return self::encode($hex);
    }
}
<commit_msg>php5.6.2 has issues with strings going into GMP<commit_after><?php

namespace Bitcoin\Util;

use Bitcoin\Exceptions\Base58ChecksumFailure;

/**
 * Class Base58
 * @package Bitcoin
 */
class Base58
{
    /**
     * @var string
     */
    private static $base58chars = "123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz";

    /**
     * Encode a given hex string in base58
     *
     * @param $hex
     * @return string
     * @throws \Exception
     */
    public static function encode($hex)
    {
        if ($hex == '') {
            return '';
        }

        if (Math::mod(strlen($hex), 2) !== '0') {
            throw new \Exception('Data must be of even length');
        }
        $orighex = $hex;

        $decimal = Math::hexDec($hex);
        $return = "";
        while (Math::cmp($decimal, 0) > 0) {
            list($decimal, $rem) = Math::divQr($decimal, 58);
            $return = $return . self::$base58chars[$rem];
        }
        $return = strrev($return);

        //leading zeros
        for ($i = 0; $i < strlen($orighex) && substr($orighex, $i, 2) == "00"; $i += 2) {
            $return = "1" . $return;
        }
        return $return;
    }

    /**
     * Decode a base58 string
     *
     * @param $base58
     * @return string
     */
    public static function decode($base58)
    {
        if (strlen($base58) == 0) {
            return '';
        }

        $original = $base58;
        $return = '0';

        for ($i = 0; $i < strlen($base58); $i++) {
            $return = Math::add(Math::mul($return, 58), strpos(self::$base58chars, $base58[$i]));
        }

        $hex = ($return == '0') ? '' : Math::decHex($return);

        for ($i = 0; $i < strlen($original) && $original[$i] == "1"; $i++) {
            $hex = "00" . $hex;
        }

        if (Math::mod(strlen($hex), 2) !== '0') {
            $hex = "0" . $hex;
        }

        return $hex;
    }

    /**
     * Calculate a checksum for the given data
     *
     * @param $data
     * @return string
     */
    public static function checksum($data)
    {
        $data = pack("H*", $data);
        $hash = Hash::sha256d($data);
        $checksum = substr($hash, 0, 8);

        return $checksum;
    }

    /**
     * Decode a base58 checksum string and validate checksum
     *
     * @param $base58
     * @return string
     * @throws Base58ChecksumFailure
     */
    public static function decodeCheck($base58)
    {
        $hex       = self::decode($base58);
        $cs_verify = substr($hex, -8);

        $data      = substr($hex, 0, -8);
        $checksum  = self::checksum($data);

        if ($checksum != $cs_verify) {
            throw new Base58ChecksumFailure('Failed to verify checksum');
        }

        return $data;
    }

    /**
     * Encode the given data in base58, with a checksum to check integrity.
     *
     * @param $data
     * @return string
     * @throws \Exception
     */
    public static function encodeCheck($data)
    {
        $checksum = self::checksum($data);
        $hex = $data . $checksum;
        return self::encode($hex);
    }
}

{'fraction_non_alphanumeric': 0.15975171512577588, 'fraction_numerical': 0.02793204835021235, 'mean_word_length': 1.812586127698668, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4968882', 'n_tokens_mistral': 2082, 'n_tokens_neox': 1950, 'n_words': 474}