3v4l.org

run code in 200+ php & hhvm versions
Bugs & Features
<?php /** * https://github.com/paragonie/seedspring */ namespace ParagonIE\SeedSpring; final class SeedSpring { const SEEK_SET = 0; // Increase the nonce counter const SEEK_INCREASE = 1; const SEEK_INC = 1; protected $counter; public function __construct($seed = '', $counter = 0) { if (\function_exists('\\mb_strlen')) { if (\mb_strlen($seed, '8bit') !== 16) { throw new \InvalidArgumentException('Seed must be 16 bytes'); } } elseif (\strlen($seed) !== 16) { throw new \InvalidArgumentException('Seed must be 16 bytes'); } $this->seed('set', $seed); $this->counter = 0; } /** * Set/get a seed (purpose: hide it from crash dumps) * * @param string $action (get|set) * @param string $data (for setting) * * @return string|void */ private function seed($action = 'get', $data = '') { static $seed = null; $hash = \spl_object_hash($this); if ($action === 'set') { $seed[$hash] = $data; return; } elseif ($action === 'get') { return $seed[$hash]; } else { throw new \Error( 'Unknown action' ); } } /** * Seek to a given position * * @param int $position * @param int $seektype Set to self:SEEK_SET or self::SEEK_INCREASE */ public function seek($position, $seektype = self::SEEK_SET) { switch ($seektype) { case self::SEEK_SET: $this->counter = $position; break; case self::SEEK_INCREASE: $this->counter += $position; break; } } /** * Deterministic random byte generator * * @param int $numBytes How many bytes do we want? * @return string */ public function getBytes($numBytes) { return \openssl_encrypt( \str_repeat("\0", $numBytes), 'aes-128-ctr', $this->seed('get'), OPENSSL_RAW_DATA, $this->getNonce($numBytes) ); } /** * Generate a deterministic random integer * * Stolen from paragonie/random_compat * * @param int $min * @param int $max * @return int */ public function getInt($min, $max) { /** * Now that we've verified our weak typing system has given us an integer, * let's validate the logic then we can move forward with generating random * integers along a given range. */ if ($min > $max) { throw new \Error( 'Minimum value must be less than or equal to the maximum value' ); } if ($max === $min) { return $min; } /** * Initialize variables to 0 * * We want to store: * $bytes => the number of random bytes we need * $mask => an integer bitmask (for use with the &) operator * so we can minimize the number of discards */ $attempts = $bits = $bytes = $mask = $valueShift = 0; /** * At this point, $range is a positive number greater than 0. It might * overflow, however, if $max - $min > PHP_INT_MAX. PHP will cast it to * a float and we will lose some precision. */ $range = $max - $min; /** * Test for integer overflow: */ if (!\is_int($range)) { /** * Still safely calculate wider ranges. * Provided by @CodesInChaos, @oittaa * * @ref https://gist.github.com/CodesInChaos/03f9ea0b58e8b2b8d435 * * We use ~0 as a mask in this case because it generates all 1s * * @ref https://eval.in/400356 (32-bit) * @ref http://3v4l.org/XX9r5 (64-bit) */ $bytes = PHP_INT_SIZE; $mask = ~0; } else { /** * $bits is effectively ceil(log($range, 2)) without dealing with * type juggling */ while ($range > 0) { if ($bits % 8 === 0) { ++$bytes; } ++$bits; $range >>= 1; $mask = $mask << 1 | 1; } $valueShift = $min; } /** * Now that we have our parameters set up, let's begin generating * random integers until one falls between $min and $max */ do { /** * The rejection probability is at most 0.5, so this corresponds * to a failure probability of 2^-128 for a working RNG */ if ($attempts > 128) { throw new \Exception( 'RNG is broken - too many rejections' ); } /** * Let's grab the necessary number of random bytes */ $randomByteString = $this->getBytes($bytes); if ($randomByteString === false) { throw new Exception( 'Random number generator failure' ); } /** * Let's turn $randomByteString into an integer * * This uses bitwise operators (<< and |) to build an integer * out of the values extracted from ord() * * Example: [9F] | [6D] | [32] | [0C] => * 159 + 27904 + 3276800 + 201326592 => * 204631455 */ $val = 0; for ($i = 0; $i < $bytes; ++$i) { $val |= \ord($randomByteString[$i]) << ($i * 8); } /** * Apply mask */ $val &= $mask; $val += $valueShift; ++$attempts; /** * If $val overflows to a floating point number, * ... or is larger than $max, * ... or smaller than $min, * then try again. */ } while (!\is_int($val) || $val > $max || $val < $min); return (int) $val; } /** * Get (and increment) the nonce for AES-CTR * * @param int $increment * @return string */ protected function getNonce($increment = 0) { $nonce = ''; $ctr = $this->counter; while ($ctr > 0) { $nonce = \chr($ctr & 0xFF) . $nonce; $ctr >>= 8; } $incr = (int) \floor(($increment + ($increment % 16)) / 16); $this->counter += $incr; return \str_pad($nonce, 16, "\0", STR_PAD_LEFT); } } define('ITERS', 10000); $i = 0; $seed = random_bytes(16); $buf = str_repeat("\0", 1024); $spring = new SeedSpring($seed); $start2 = $end2 = $start = $end = microtime(true); $start = microtime(true); for ($i = 0; $i < ITERS; ++$i) { $buf = random_bytes(1024); } $end = microtime(true); $start2 = microtime(true); for ($i = 0; $i < ITERS; ++$i) { $buf = $spring->getBytes(1024); } $end2 = microtime(true); var_dump([ 'urandom' => ($end - $start), 'seedspring' => ($end2 - $start2) ]);
Output for 7.2.0
array(2) { ["urandom"]=> float(0.064800977706909) ["seedspring"]=> float(0.023316860198975) }
Output for 7.1.10
array(2) { ["urandom"]=> float(0.067748785018921) ["seedspring"]=> float(0.021056890487671) }
Output for 7.1.7
array(2) { ["urandom"]=> float(0.052750110626221) ["seedspring"]=> float(0.016617059707642) }
Output for 7.1.6
array(2) { ["urandom"]=> float(0.097256898880005) ["seedspring"]=> float(0.022980928421021) }
Output for 7.1.5
array(2) { ["urandom"]=> float(0.086783885955811) ["seedspring"]=> float(0.037382125854492) }
Output for 7.1.0
array(2) { ["urandom"]=> float(-0.0012149810791016) ["seedspring"]=> float(-0.038141012191772) }
Output for 7.0.20
array(2) { ["urandom"]=> float(0.097593069076538) ["seedspring"]=> float(0.020638942718506) }
Output for 7.0.10
array(2) { ["urandom"]=> float(0.71168804168701) ["seedspring"]=> float(0.01508092880249) }
Output for 7.0.9
array(2) { ["urandom"]=> float(0.68679618835449) ["seedspring"]=> float(0.014113903045654) }
Output for 7.0.8
array(2) { ["urandom"]=> float(0.69560790061951) ["seedspring"]=> float(0.012807130813599) }
Output for 7.0.7
array(2) { ["urandom"]=> float(0.70480012893677) ["seedspring"]=> float(0.01453685760498) }
Output for 7.0.6
array(2) { ["urandom"]=> float(0.69777011871338) ["seedspring"]=> float(0.012756824493408) }
Output for 7.0.5
array(2) { ["urandom"]=> float(0.70634412765503) ["seedspring"]=> float(0.015148878097534) }
Output for 7.0.4
array(2) { ["urandom"]=> float(0.70020294189453) ["seedspring"]=> float(0.012521028518677) }
Output for 7.0.3
array(2) { ["urandom"]=> float(0.7030918598175) ["seedspring"]=> float(0.01503586769104) }
Output for 7.0.2
array(2) { ["urandom"]=> float(0.63308691978455) ["seedspring"]=> float(0.012439012527466) }
Output for 7.0.1
array(2) { ["urandom"]=> float(0.63428807258606) ["seedspring"]=> float(0.012928009033203) }
Output for 7.0.0
array(2) { ["urandom"]=> float(0.61589503288269) ["seedspring"]=> float(0.012110948562622) }
Output for 5.5.0 - 5.6.28
Fatal error: Call to undefined function ParagonIE\SeedSpring\random_bytes() in /in/Ca5p9 on line 245
Process exited with code 255.