/** * Constructor... * * @param \r8\Seed $seed The seed to feed into the random number generator */ public function __construct(\r8\Seed $seed) { if (!extension_loaded('bcmath')) { throw new \r8\Exception\Extension("BC Math extension required"); } $this->num = abs($seed->getInteger()); // Throw away the first value $this->nextInteger(); }
/** * Runs the initialization routine on the resource and handles the results * * @param Resource $resource The encryption resource * @param String $iv The initialization vector * @return NULL */ private function initialize($resource, $iv) { $key = substr($this->key->getString(), 0, mcrypt_enc_get_key_size($resource)); $result = mcrypt_generic_init($resource, $key, $iv); if ($result == -3) { throw new \r8\Exception\Interaction("Incorrect encryption key length"); } else { if ($result == -4) { throw new \r8\Exception\Interaction("Unable to allocate memory for encryption"); } else { if ($result === FALSE || $result < 0) { throw new \r8\Exception\Interaction("An unknown error occured while initializing encryption resource"); } } } }
/** * Reverses the transformation on a string * * @param mixed $value The value to reverse transform * @return mixed The original, untransformed value */ public function from($string) { $string = (string) $string; $hashLength = $this->hashLength * ($this->readable ? 2 : 1); $mac = substr($string, 0, $hashLength); if (strlen($mac) !== $hashLength) { throw new \r8\Exception\Data($string, "Transform String", "Unable to extract data verification hash"); } $string = substr($string, $hashLength); $compareHash = self::pbkdf2($string, $this->salt->getString(), $this->hashLength); if ($this->readable) { $compareHash = \bin2hex($compareHash); } if ($mac != $compareHash) { throw new \r8\Exception\Data($string, "Transform String", "Data integrity verification failed"); } return $string; }
public function testRandom() { $this->assertThat(\r8\Seed::random(), $this->isInstanceOf('\\r8\\Seed')); $this->assertNotEquals(\r8\Seed::random()->getSource(), \r8\Seed::random()->getSource()); $this->assertNotEquals(\r8\Seed::random()->getSource(), \r8\Seed::random()->getSource()); }