lithium\util\String::random PHP Method

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 random($bytes, array $options = array())
    {
        $defaults = array('encode' => null);
        $options += $defaults;
        $source = static::$_source ?: static::_source();
        $result = $source($bytes);
        if ($options['encode'] !== static::ENCODE_BASE_64) {
            return $result;
        }
        return strtr(rtrim(base64_encode($result), '='), '+', '.');
    }

Usage Example

 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;
     });
 }
All Usage Examples Of lithium\util\String::random