<commit_before><?php

/**
 * CryptUtil: A suite of wrapper utility functions for the OpenID
 * library.
 *
 * PHP versions 4 and 5
 *
 * LICENSE: See the COPYING file included in this distribution.
 *
 * @package OpenID
 * @author JanRain, Inc. <<EMAIL>>
 * @copyright 2005 Janrain, Inc.
 * @license http://www.gnu.org/copyleft/lesser.html LGPL
 */

/**
 * Require the HMAC/SHA-1 implementation for creating such hashes.
 */
require_once('HMACSHA1.php');

if (!defined('Net_OpenID_RAND_SOURCE')) {
    /**
     * The filename for a source of random bytes. Define this yourself
     * if you have a different source of randomness.
     */
    define('Net_OpenID_RAND_SOURCE', '/dev/urandom');
}

/**
 * Net_OpenID_CryptUtil houses static utility functions.
 *
 * @package OpenID
 * @static
 */
class Net_OpenID_CryptUtil {
    /** 
     * Get the specified number of random bytes.
     *
     * Attempts to use a cryptographically secure (not predictable)
     * source of randomness if available. If there is no high-entropy
     * randomness source available, it will fail. As a last resort,
     * for non-critical systems, define
     * <code>Net_OpenID_USE_INSECURE_RAND</code>, and the code will
     * fall back on a pseudo-random number generator.
     *
     * @param int $num_bytes The length of the return value
     * @return string $bytes random bytes
     */
    function getBytes($num_bytes)
    {
        $bytes = '';
        $f = @fopen("/dev/urandom", "r");
        if ($f === false) {
            if (!defined('Net_OpenID_USE_INSECURE_RAND')) {
                trigger_error('Set Net_OpenID_USE_INSECURE_RAND to ' .
                              'continue with insecure random.',
                              E_USER_ERROR);
            }
            $bytes = '';
            for ($i = 0; $i < $num_bytes; $i += 4) {
                $bytes .= pack('L', mt_rand());
            }
            $bytes = substr($bytes, 0, $num_bytes);
        } else {
            $bytes = fread($f, $num_bytes);
            fclose($f);
        }
        return $bytes;
    }

    /**
     * Computes the maximum integer value for this PHP installation.
     *
     * @return int $max_int_value The maximum integer value for this
     * PHP installation
     */
    function maxint()
    {
        /**
         * quick-and-dirty function for PHP int size -- assumes
         * largest integer is of form 2^n - 1
         */
        $to_test = pow(2, 16);
        while (1) {
            $last = $to_test;
            $to_test = 2 * $to_test;
            if (($to_test < $last) || (!is_int($to_test))) {
                return($last + ($last - 1));
            }
        }
    }

    /**
     * Computes the SHA1 hash.
     *
     * @param string $str The input string.
     * @return string The resulting SHA1 hash.
     */
    function sha1($str)
    {
        return base64_decode(sha1($str));
    }

    /**
     * Computes an HMAC-SHA1 digest.
     *
     * @param string $key The key used to generate the HMAC-SHA1 digest
     * @param string $text The text to be hashed
     * @return string $digest The raw HMAC-SHA1 digest
     */
    function hmacSha1($key, $text)
    {
        return Net_OpenID_HMACSHA1($key, $text);
    }

    /**
     * Converts a base64-encoded string to its raw binary equivalent.
     *
     * @param string $str The base64-encoded string to decode
     * @return string $raw The decoded binary data
     */
    function fromBase64($str)
    {
        return base64_decode($str);
    }

    /**
     * Converts a raw binary string to its base64-encoded equivalent.
     *
     * @param string $str The raw binary data to encode
     * @return string $raw The base64-encoded version of $str
     */
    function toBase64($str)
    {
        return base64_encode($str);
    }

    /**
     * Given a long integer, returns the number converted to a binary
     * string.
     *
     * @param integer $long The long number (can be a normal PHP
     * integer or a number created by one of the available long number
     * libraries)
     * @return string $binary The binary version of $long
     */
    function longToBinary($long)
    {

        $lib =& Net_OpenID_MathLibrary::getLibWrapper();

        if ($lib->cmp($long, 0) < 0) {
            print "numToBytes takes only positive integers.";
            return null;
        }

        if ($long == 0) {
            return "\x00";
        }

        $bytes = array();

        while ($long) {
            array_unshift($bytes, $lib->mod($long, 256));
            $long = $lib->div($long, pow(2, 8));
        }

        if ($bytes && ($bytes[0] > 127)) {<commit_msg>[project @ Change loop condition for longToBinary to use math library]<commit_after><?php

/**
 * CryptUtil: A suite of wrapper utility functions for the OpenID
 * library.
 *
 * PHP versions 4 and 5
 *
 * LICENSE: See the COPYING file included in this distribution.
 *
 * @package OpenID
 * @author JanRain, Inc. <<EMAIL>>
 * @copyright 2005 Janrain, Inc.
 * @license http://www.gnu.org/copyleft/lesser.html LGPL
 */

/**
 * Require the HMAC/SHA-1 implementation for creating such hashes.
 */
require_once('HMACSHA1.php');

if (!defined('Net_OpenID_RAND_SOURCE')) {
    /**
     * The filename for a source of random bytes. Define this yourself
     * if you have a different source of randomness.
     */
    define('Net_OpenID_RAND_SOURCE', '/dev/urandom');
}

/**
 * Net_OpenID_CryptUtil houses static utility functions.
 *
 * @package OpenID
 * @static
 */
class Net_OpenID_CryptUtil {
    /** 
     * Get the specified number of random bytes.
     *
     * Attempts to use a cryptographically secure (not predictable)
     * source of randomness if available. If there is no high-entropy
     * randomness source available, it will fail. As a last resort,
     * for non-critical systems, define
     * <code>Net_OpenID_USE_INSECURE_RAND</code>, and the code will
     * fall back on a pseudo-random number generator.
     *
     * @param int $num_bytes The length of the return value
     * @return string $bytes random bytes
     */
    function getBytes($num_bytes)
    {
        $bytes = '';
        $f = @fopen("/dev/urandom", "r");
        if ($f === false) {
            if (!defined('Net_OpenID_USE_INSECURE_RAND')) {
                trigger_error('Set Net_OpenID_USE_INSECURE_RAND to ' .
                              'continue with insecure random.',
                              E_USER_ERROR);
            }
            $bytes = '';
            for ($i = 0; $i < $num_bytes; $i += 4) {
                $bytes .= pack('L', mt_rand());
            }
            $bytes = substr($bytes, 0, $num_bytes);
        } else {
            $bytes = fread($f, $num_bytes);
            fclose($f);
        }
        return $bytes;
    }

    /**
     * Computes the maximum integer value for this PHP installation.
     *
     * @return int $max_int_value The maximum integer value for this
     * PHP installation
     */
    function maxint()
    {
        /**
         * quick-and-dirty function for PHP int size -- assumes
         * largest integer is of form 2^n - 1
         */
        $to_test = pow(2, 16);
        while (1) {
            $last = $to_test;
            $to_test = 2 * $to_test;
            if (($to_test < $last) || (!is_int($to_test))) {
                return($last + ($last - 1));
            }
        }
    }

    /**
     * Computes the SHA1 hash.
     *
     * @param string $str The input string.
     * @return string The resulting SHA1 hash.
     */
    function sha1($str)
    {
        return base64_decode(sha1($str));
    }

    /**
     * Computes an HMAC-SHA1 digest.
     *
     * @param string $key The key used to generate the HMAC-SHA1 digest
     * @param string $text The text to be hashed
     * @return string $digest The raw HMAC-SHA1 digest
     */
    function hmacSha1($key, $text)
    {
        return Net_OpenID_HMACSHA1($key, $text);
    }

    /**
     * Converts a base64-encoded string to its raw binary equivalent.
     *
     * @param string $str The base64-encoded string to decode
     * @return string $raw The decoded binary data
     */
    function fromBase64($str)
    {
        return base64_decode($str);
    }

    /**
     * Converts a raw binary string to its base64-encoded equivalent.
     *
     * @param string $str The raw binary data to encode
     * @return string $raw The base64-encoded version of $str
     */
    function toBase64($str)
    {
        return base64_encode($str);
    }

    /**
     * Given a long integer, returns the number converted to a binary
     * string.
     *
     * @param integer $long The long number (can be a normal PHP
     * integer or a number created by one of the available long number
     * libraries)
     * @return string $binary The binary version of $long
     */
    function longToBinary($long)
    {

        $lib =& Net_OpenID_MathLibrary::getLibWrapper();

        if ($lib->cmp($long, 0) < 0) {
            print "numToBytes takes only positive integers.";
            return null;
        }

        if ($long == 0) {
            return "\x00";
        }

        $bytes = array();

        while ($lib->cmp($long, 0) > 0) {
            array_unshift($bytes, $lib->mod($long, 256));
            $long = $lib->div($long, pow(2, 8));
        }

        if ($bytes && ($bytes[0] > 127)) {
{'fraction_non_alphanumeric': 0.11546259737487995, 'fraction_numerical': 0.013018888058905134, 'mean_word_length': 1.9425431711145997, 'pattern_counts': {'":': 0, '<': 19, '<?xml version=': 0, '>': 21, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, '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': '3185090', 'n_tokens_mistral': 2887, 'n_tokens_neox': 2751, 'n_words': 879}