示例#1
0
文件: bigint.php 项目: TedaLIEz/AUNET
    }
    function bigint_powmod($x, $y, $m)
    {
        return gmp_powm($x, $y, $m);
    }
} else {
    if (extension_loaded('big_int')) {
        function bigint_dec2num($dec)
示例#2
0
 /**
  * Generates random number wich bit length $bits_cnt,
  * using $random_generator as random generator function.
  * If is_set_higher_bit != false, then higer bit of result
  * will be set to 1.
  *
  * @param int $bits_cnt
  * @param string $rnd_generator
  * @return big_int resource
  * @access public
  */
 function getRand($bits_cnt, $random_generator, $is_set_higher_bit = false)
 {
     $tmp = bi_rand($bits_cnt, $random_generator);
     return $is_set_higher_bit ? bi_set_bit($tmp, $bits_cnt - 1) : $tmp;
 }
示例#3
0
 /**
  * Constructor
  *
  * @param	String	Private key, if not included one shall be generated
  */
 function Diffie_Hellman_BI($privkey = "")
 {
     if (empty($privkey)) {
         $privkey = bi_rand(160);
     }
     $this->privatekey = $privkey;
 }
示例#4
0
 public static function random($n, $s)
 {
     switch (BigInt::support()) {
         case 'gmp':
             $result = gmp_init(0);
             for ($i = 0; $i < $n; $i++) {
                 if (mt_rand(0, 1)) {
                     gmp_setbit($result, $i);
                 }
             }
             if ($s) {
                 gmp_setbit($result, $n - 1);
             }
             return $result;
         case 'big_int':
             $result = bi_rand($n);
             if ($s) {
                 $result = bi_set_bit($result, $n - 1);
             }
             return $result;
         case 'bcmath':
             bcscale(0);
             $t = BigInt::bcpow(2, $n);
             if ($s == 1) {
                 $m = bcdiv($t, 2);
                 $t = bcsub($m, 1);
             } else {
                 $m = 0;
                 $t = bcsub($t, 1);
             }
             $l = strlen($t);
             $n = (int) ($l / 9) + 1;
             $r = '';
             while ($n) {
                 $r .= substr('000000000' . mt_rand(0, 999999999), -9);
                 --$n;
             }
             $r = substr($r, 0, $l);
             while (bccomp($r, $t) == 1) {
                 $r = substr($r, 1, $l) . mt_rand(0, 9);
             }
             return bcadd($r, $m);
         case '':
         default:
             return BigInt::_random($n, $s);
     }
 }
示例#5
0
echo 'bit_len($a) = ', bi_bit_len($a), "<br/>\n";
echo 'bit1_cnt($a) = ', bi_bit1_cnt($a), "<br/>\n";
echo 'test_bit($a, 0) = ', bi_test_bit($a, 0), "<br/>\n";
echo 'scan0_bit($a, 0) = ', bi_scan0_bit($a, 0), "<br/>\n";
echo 'scan1_bit($a, 0) = ', bi_scan1_bit($a, 0), "<br/>\n";
echo '<h3>pseudorandom functions</h3>' . "\n";
/* 
    resource bi_rand(int $bit_len[, string $rand_func_name])
    Returns pseudorandom number with $bit_len bit length.
    Attention: do not use this function in cryptographic
    or security applications! The function can be used only
    for educational purposes :)
*/
$c = bi_rand(100);
echo 'rand(100) = ', bi_to_str($c), "<br/>\n";
$c = bi_rand(100, 'mt_rand');
// use mt_rand() as random generator
echo 'rand(100, "mt_rand") = ', bi_to_str($c), "<br/>\n";
/*
    functions for modular arithmetic calculations
*/
echo '<h3>modular arithmetic functions</h3>' . "\n";
// find next pseudoprime number after $c
$modulus = bi_next_prime($c);
echo '$modulus = next_prime($c) = [', bi_to_str($modulus), "]<br/>\n";
$c = bi_addmod($a, $b, $modulus);
echo '$a + $b (mod $modulus) = [', bi_to_str($c), "]<br/>\n";
$c = bi_submod($a, $b, $modulus);
echo '$a - $b (mod $modulus) = [', bi_to_str($c), "]<br/>\n";
$c = bi_mulmod($a, $b, $modulus);
echo '$a * $b (mod $modulus) = [', bi_to_str($c), "]<br/>\n";
示例#6
0
文件: RSA.php 项目: garybulin/php7
/**
    returns a "good" prime $a with length $bit_len with
    resrtiction:
        $a - 1 != 0 (mod $e)
*/
function get_prime($bit_len)
{
    return bi_next_prime(bi_set_bit(bi_rand($bit_len, 'microtime_generator'), $bit_len - 1));
}
示例#7
0
文件: rand.php 项目: garybulin/php7
/**
    example:
    usage of user function as random number generator
    for bi_rand() function.
    bi_rand() uses only lower byte of value, returned by
    user function.
*/
require_once dirname(__FILE__) . '/std_header.php';
echo "const generator:<br>\n";
echo bi_to_str(bi_rand(100, 'const_generator'), 16), "<br>\n\n";
echo "time generator:<br>\n";
echo bi_to_str(bi_rand(100, 'time_generator'), 16), "<br>\n\n";
echo "microtime generator:<br>\n";
echo bi_to_str(bi_rand(100, 'microtime_generator'), 16), "<br>\n\n";
echo "static generator:<br>\n";
echo bi_to_str(bi_rand(100, 'static_generator'), 16), "<br>\n\n";
exit;
/***************************************************************************/
/**
    'constant' number generator
*/
function const_generator()
{
    return 0xff;
}
/**
    time() number generator
*/
function time_generator()
{
    //sleep(1);