randomString() публичный статический Метод

Returns string of pseudo random characters
public static randomString ( integer $len = null, string $chars = null, callable $cb = null, integer $pri, boolean $hang = false ) : string
$len integer Length of desired string
$chars string String of allowed characters
$cb callable Callback
$pri integer Priority of EIO operation
$hang boolean If true, we shall use /dev/random instead of /dev/urandom and it may cause a delay
Результат string
Пример #1
0
 public function perform()
 {
     $hash = Request::getString($_REQUEST['x']);
     if (!strlen($hash) || base64_decode($hash, true) === false) {
         $this->req->setResult(['success' => false, 'error' => 'Wrong format of extTokenHash']);
         return;
     }
     $this->appInstance->externalAuthTokens->findByExtTokenHash($hash, function ($result) use($hash) {
         if ($result) {
             $this->req->setResult(['success' => false, 'error' => 'This token was already used.']);
             return;
         }
         $ip = $this->req->getIp();
         $intToken = Crypt::hash(Daemon::uniqid() . "" . $ip . "" . Crypt::randomString());
         $this->appInstance->externalAuthTokens->save(['extTokenHash' => $hash, 'intToken' => $intToken, 'ip' => $ip, 'useragent' => Request::getString($_SERVER['HTTP_USER_AGENT']), 'ctime' => microtime(true), 'status' => 'new'], function ($lastError) use($intToken) {
             if (!isset($lastError['n']) || $lastError['n'] === 0) {
                 $this->req->setResult(['success' => false, 'errors' => ['code' => 'Sorry, internal error.']]);
                 return;
             }
             $type = Request::getString($_REQUEST['type']);
             if ($type === 'email') {
                 // send email....
             } elseif ($type === 'redirect') {
                 $this->req->redirectTo(HTTPClient::buildUrl(['/' . $this->req->locale . '/account/extauth', 'i' => $intToken]), false);
             }
             $this->req->setResult(['success' => true, 'intToken' => $intToken]);
         });
     });
 }
Пример #2
0
 /**
  * Called when the connection is handshaked (at low-level), and peer is ready to recv. data
  * @return void
  */
 public function onReady()
 {
     $this->setWatermark(2, $this->pool->maxAllowedPacket);
     Crypt::randomString(16, null, function ($string) {
         $this->key = base64_encode($string);
         $this->write('GET /' . $this->path . " HTTP/1.1\r\nHost: " . $this->host . ($this->port != 80 ? ':' . $this->port : '') . "\r\nUpgrade: websocket\r\nConnection: Upgrade\r\nSec-WebSocket-Key: " . $this->key . "\r\nSec-WebSocket-Version: 13\r\n\r\n");
     });
 }
Пример #3
0
 public function setId($v)
 {
     $this->set('_id', $v);
     $this->set('idText', sprintf('%04d', ($v - 1) % 10000 + 1));
     $this->set('code', Crypt::randomString(5, '1234567890'));
     $this->set('ts', microtime(true));
     $this->set('tries', 10);
 }
Пример #4
0
 /**
  * @TODO
  * @param  callable $cb
  * @callback $cb ( )
  * @return this
  */
 public function acquire($cb)
 {
     Crypt::randomString(16, null, function ($token) use($cb) {
         $this->token = $token;
         $this->pool->set($this->key, $this->token, 'NX', 'EX', $this->timeout, function ($redis) use($cb) {
             $cb($this, $redis->result === 'OK', $redis);
         });
     });
     return $this;
 }
Пример #5
0
 /**
  * @param array $find
  * @param callable $cb
  */
 public function newToken($cb, $add = [])
 {
     $this->captcha->insertOne(['_id' => $id = new \MongoId(), 'rnd' => $rnd = \PHPDaemon\Utils\Crypt::randomString(8), 'text' => $text = \WakePHP\Utils\CaptchaDraw::getRandomText(), 'ctime' => time()] + $add, function ($lastError) use($id, $rnd, $cb, $text) {
         if (!$lastError['ok']) {
             call_user_func($cb, false);
             return;
         }
         $token = base64_encode($id . "" . $rnd);
         $this->appInstance->JobManager->enqueue(function ($result) use($token, $text, $cb) {
             if (!$result) {
                 call_user_func($cb, false);
                 return;
             }
             Daemon::log(Debug::dump([[$result]]));
             call_user_func($cb, $token);
         }, 'GenerateCaptchaImage', [$token, $text]);
     });
 }
Пример #6
0
 /**
  * @param $req
  * @return array
  */
 public function getAccountBase($req)
 {
     return ['email' => '', 'location' => '', 'ukey' => Crypt::randomString(16), 'confirmationcode' => substr(md5($req->attrs->server['REMOTE_ADDR'] . "" . Daemon::uniqid() . "" . $this->appInstance->config->cryptsalt->value . "" . microtime(true) . "" . mt_rand(0, mt_getrandmax())), 0, 6), 'regdate' => time(), 'etime' => time(), 'ttlSession' => 1200, 'ip' => $req->attrs->server['REMOTE_ADDR'], 'subscription' => 'daily', 'aclgroups' => array('Users'), 'acl' => array()];
 }
Пример #7
0
 /**
  * Function run task and wait result in callback
  *
  * @param $params
  * @param callable $cb = null
  * @param boolean $unique
  */
 public function submitJob($params, $cb = null)
 {
     $closure = function () use(&$params, $cb) {
         $this->sendCommand('SUBMIT_JOB' . (isset($params['pri']) ? '_ ' . strtoupper($params['pri']) : '') . (isset($params['bg']) && $params['bg'] ? '_BG' : ''), [$params['function'], $params['unique'], $params['payload']], $cb);
     };
     if (isset($params['unique'])) {
         $closure();
     } else {
         Crypt::randomString(10, '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ', function ($random) use($closure) {
             $params['unique'] = $random;
             $closure();
         });
     }
 }
Пример #8
0
 /**
  * @return array
  */
 public function startSession($add = [], $cb = null)
 {
     $session = ['id' => Crypt::randomString(), 'ctime' => microtime(true)] + $add;
     $this->sessions->upsertOne(['id' => (string) $session['id']], $session, $cb);
     return $session;
 }
Пример #9
0
 public static function getRandomText()
 {
     return \PHPDaemon\Utils\Crypt::randomString(4, '23456789abcdefghkmnpqrstuvwxyz23456789');
     //return \PHPDaemon\Utils\Crypt::randomString(4, 'ABCDEFGHKLMNPQRSTUVWXYZ23456789abcdefghkmnpqrstuvwxyz23456789');
 }