예제 #1
0
/**
 * Turn a binary back into a long
 * @param string $b
 * @return integer
 * @url http://openidenabled.com Borrowed from PHP-OpenID
 */
function long($b)
{
    $bytes = array_merge(unpack('C*', $b));
    $n = 0;
    foreach ($bytes as $byte) {
        $n = bmmul($n, bmpow(2, 8));
        $n = bmadd($n, $byte);
    }
    return $n;
}
예제 #2
0
function bmpowmod($value, $exponent, $mod)
{
    if (function_exists('bcpowmod')) {
        return bcpowmod($value, $exponent, $mod);
    }
    if (function_exists('gmp_powm')) {
        return gmp_strval(gmp_powm($value, $exponent, $mod));
    }
    $r = '';
    while ($exponent != '0') {
        $t = bmmod($exponent, '4096');
        $r = substr("000000000000" . decbin(intval($t)), -12) . $r;
        $exponent = bmdiv($exponent, '4096');
    }
    $r = preg_replace("!^0+!", "", $r);
    if ($r == '') {
        $r = '0';
    }
    $value = bmmod($value, $mod);
    $erb = strrev($r);
    $q = '1';
    $a[0] = $value;
    for ($i = 1; $i < strlen($erb); $i++) {
        $a[$i] = bmmod(bmmul($a[$i - 1], $a[$i - 1]), $mod);
    }
    for ($i = 0; $i < strlen($erb); $i++) {
        if ($erb[$i] == "1") {
            $q = bmmod(bmmul($q, $a[$i]), $mod);
        }
    }
    return $q;
}