PHPDaemon\Utils\Crypt::randomInts PHP Method

randomInts() public static method

Returns array of pseudo random integers of machine-dependent size
public static randomInts ( integer $numInts, callable $cb, integer $pri, boolean $hang = false ) : integer
$numInts integer Number of integers
$cb callable Callback
$pri integer Priority of EIO operation
$hang boolean If true, we shall use /dev/random instead of /dev/urandom and it may cause a delay
return integer
    public static function randomInts($numInts, $cb, $pri = 0, $hang = false)
    {
        static::randomBytes(PHP_INT_SIZE * $numInts, function ($bytes) use($cb, $numInts) {
            if ($bytes === false) {
                $cb(false);
                return;
            }
            $ints = [];
            for ($i = 0; $i < $numInts; ++$i) {
                $thisInt = 0;
                for ($j = 0; $j < PHP_INT_SIZE; ++$j) {
                    $thisInt = $thisInt << 8 | ord($bytes[$i * PHP_INT_SIZE + $j]) & 0xff;
                }
                // Absolute value in two's compliment (with min int going to zero)
                $thisInt = $thisInt & PHP_INT_MAX;
                $ints[] = $thisInt;
            }
            $cb($ints);
        }, $pri, $hang);
    }

Usage Example

Exemplo n.º 1
0
 /**
  * @covers Crypt::randomInts
  */
 public function testRandomInts()
 {
     $this->prepareAsync();
     Crypt::randomInts(5, function ($ints) {
         self::assertCount(5, $ints, '$ints must contain 5 elements');
         $this->completeAsync();
     });
     $this->runAsync(__METHOD__);
 }
All Usage Examples Of PHPDaemon\Utils\Crypt::randomInts