Example #1
0
<?php

include 'phprandom.php';
$img = imageCreateTrueColor(256, 256);
$colors = array();
for ($i = 0; $i < 256; $i++) {
    $colors[$i] = imageColorAllocate($img, $i, $i, $i);
}
for ($i = 0; $i < 256; $i++) {
    $random = PHPRandom::getBinary(256);
    for ($j = 0; $j < 256; $j++) {
        $value = ord(substr($random, $j, 1));
        imageSetPixel($img, $i, $j, $colors[$value]);
    }
}
header('Content-Type: image/png');
imagePNG($img);
imageDestroy($img);
Example #2
0
 public function test_list_sources()
 {
     $list = PHPRandom::listSources();
     $this->assertGreaterThan(0, count($list));
 }
<?
	include_once "../config.php";
	
	$gift = "WATER";
	for ($i=0; $i < 20;$i++)
	{
		$serial = PHPRandom::getHexString("12");
		$query 	= "INSERT INTO serial_info_(serial_code,gift) values('".$serial."','".$gift."')";
		$result 	= mysqli_query($my_db, $query);
	}
?>
Example #4
0
 public static function getBinary($length = 32)
 {
     if ($length < 1) {
         return '';
     }
     // There's not much point reading more than 256 bits of entropy from any single source.
     $capped_length = min($length, 32);
     // As usual, Windows requires special consideration.
     $is_windows = strtoupper(substr(PHP_OS, 0, 3)) === 'WIN';
     // Variables to store state during entropy collection.
     $entropy = array();
     $sources = array();
     $total_strength = 0;
     $required_strength = 5;
     // Try getting entropy from various sources that are known to be good.
     if (function_exists('openssl_random_pseudo_bytes') && (!$is_windows || version_compare(PHP_VERSION, '5.4', '>='))) {
         $entropy[] = openssl_random_pseudo_bytes($capped_length, $crypto_strong);
         $sources[] = 'openssl';
         $total_strength += $crypto_strong ? 3 : 1;
     } elseif (function_exists('mcrypt_create_iv') && (!$is_windows || version_compare(PHP_VERSION, '5.3.7', '>='))) {
         $entropy[] = mcrypt_create_iv($capped_length, MCRYPT_DEV_URANDOM);
         $sources[] = 'mcrypt_dev_urandom';
         $total_strength += 4;
     } elseif ($is_windows && function_exists('mcrypt_create_iv') && defined('MCRYPT_RAND')) {
         $entropy[] = mcrypt_create_iv($capped_length, MCRYPT_RAND);
         $sources[] = 'mcrypt_rand';
         $total_strength += 2;
     } elseif (!$is_windows && file_exists('/dev/urandom') && is_readable('/dev/urandom')) {
         $entropy[] = fread($fp = fopen('/dev/urandom', 'rb'), $capped_length);
         fclose($fp);
         $sources[] = 'dev_urandom';
         $total_strength += 4;
     }
     // Supplement with multiple calls to rand() and mt_rand().
     while ($total_strength < $required_strength) {
         $rand = '';
         for ($i = 0; $i < $capped_length; $i += 4) {
             $rand .= pack('L', rand(0, 0x7fffffff) ^ mt_rand(0, 0x7fffffff));
         }
         $entropy[] = $rand;
         $sources[] = 'mt_rand';
         $total_strength += 1;
     }
     // Mix the entropy sources together using SHA-512.
     $mixer_content = end($entropy);
     $mixer_output = '';
     if (function_exists('hash_hmac') && in_array('sha256', hash_algos())) {
         for ($i = 0; $i < $length; $i += 32) {
             foreach ($entropy as $item) {
                 $mixer_content = hash_hmac('sha256', $item, $mixer_content . $i, true);
             }
             $mixer_output .= $mixer_content;
         }
     } else {
         for ($i = 0; $i < $length; $i += 20) {
             foreach ($entropy as $item) {
                 $mixer_content = sha1($item . $mixer_content . $i . microtime(), true);
             }
             $mixer_output .= $mixer_content;
         }
     }
     self::$_sources = $sources;
     return substr($mixer_output, 0, $length);
 }
Example #5
0
 /**
  * Protected function to overload the generator for unit testing purposes
  */
 protected function genRandom()
 {
     $buffer = $this->mergeBuffers(self::$state, $this->genNormal());
     if ($this->secure) {
         $buffer = $this->mergeBuffers($buffer, $this->genSecure());
     }
     self::$state = $this->mergeBuffers(self::$state, $buffer);
     return $buffer;
 }