random() public static method

$bits = String::random(8); // 64 bits $hex = bin2hex($bits); // [0-9a-f]+ Optionally base64-encodes the resulting random string per the following. The alphabet used by base64_encode() is different than the one we should be using. When considering the meaty part of the resulting string, however, a bijection allows to go the from one to another. Given that we're working on random bytes, we can use safely use base64_encode() without losing any entropy.
public static random ( integer $bytes, array $options = [] ) : string
$bytes integer The number of random bytes to generate.
$options array The options used when generating random bytes: - `'encode'` _integer_: If specified, and set to `String::ENCODE_BASE_64`, the resulting value will be base64-encoded, per the notes above.
return string Returns a string of random bytes.
 public static function __init()
 {
     parent::__init();
     static::applyFilter('create', function ($self, $params, $chain) {
         if (empty($params['data']['secret'])) {
             $params['data']['secret'] = bin2hex(String::random(16));
         }
         return $chain->next($self, $params, $chain);
     });
     static::finder('getApplicationByIdAndSecret', function ($self, $params, $chain) {
         // Do stuff
         $data = $chain->next($self, $params, $chain);
         return $data ?: null;
     });
 }
Esempio n. 2
0
 /**
  * Generates an MD5 salt for use in `lithium\security\Password::hash()`.
  *
  * @return string The MD5 salt.
  */
 protected static function _generateSaltMd5()
 {
     return '$1$' . String::random(6, array('encode' => String::ENCODE_BASE_64));
 }
Esempio n. 3
0
 /**
  * Tests the random number generator with base64 encoding.
  */
 public function testRandom64Generator()
 {
     $check = array();
     $count = 25;
     $pattern = "/^[0-9A-Za-z\\.\\/]{11}\$/";
     for ($i = 0; $i < $count; $i++) {
         $result = String::random(8, array('encode' => String::ENCODE_BASE_64));
         $this->assertPattern($pattern, $result);
         $this->assertFalse(in_array($result, $check));
         $check[] = $result;
     }
 }