public static function provideGenerators() { $factory = new \CryptLib\Random\Factory(); $generator = $factory->getLowStrengthGenerator(); $sources = $generator->getSources(); $ret = array(); $ret[] = array(new Generator($sources, new \CryptLib\Random\Mixer\Hash()), 10000, 'hash'); $ret[] = array(new Generator($sources, new \CryptLib\Random\Mixer\DES()), 10000, 'des'); $ret[] = array(new Generator($sources, new \CryptLib\Random\Mixer\Rijndael()), 10000, 'rijndael'); return $ret; }
* CryptLib\Random\Factory class to build a random number generator to suit * our needs here. */ //We first load the bootstrap file so we have access to the library require_once dirname(dirname(__DIR__)) . '/lib/CryptLib/bootstrap.php'; //Now, let's get a random number factory $factory = new \CryptLib\Random\Factory(); /** * Now, since we want a low strength random number, let's get a low strength * generator from the factory. * * If we wanted stronger random numbers, we could change this to medium or high * but both use significantly more resources to generate, so let's just stick * with low for the purposes of this example: */ $generator = $factory->getLowStrengthGenerator(); /** * We can now start generating our random strings. The generator by default * outputs full-byte strings (character 0 - 255), so it's not safe to display * them directly. Instead, let's convert them to hex to show the string. */ $number = $generator->generate(8); printf("\nHere's our first random string: %s\n", bin2hex($number)); /** * We can also base64 encode it to display the string */ $number = $generator->generate(8); printf("\nHere's a base64 encoded random string: %s\n", base64_encode($number)); /** * But, we can also generate random strings against a list of characters. That * way we can use the random string in user-facing situations: (this can be for
/** * Generates a Safe Hash to use throughout CiiMS * @param integer $length the hash length, default of 16 * @return string */ public static function generateSafeHash($length = 16) { $factory = new CryptLib\Random\Factory(); return preg_replace('/[^A-Za-z0-9\\-]/', '', $factory->getLowStrengthGenerator()->generateString($length)); }
/** * Generates a new API key for this application * @return string */ protected function generateApiKey() { // Load the hashing factory $factory = new CryptLib\Random\Factory(); $meta = UserMetadata::model()->getPrototype('UserMetadata', array('user_id' => $this->getUser()->id, 'key' => 'api_key' . $this->app_name), array('user_id' => $this->getUser()->id, 'key' => 'api_key' . $this->app_name)); $meta->value = $factory->getLowStrengthGenerator()->generateString(16); if ($meta->save()) { return $meta->value; } throw new CHttpException(500, Yii::t('ciims.models.LoginForm', 'Unable to create API key, please try again.')); }