PHPDaemon\Utils\Crypt::randomString PHP Method

randomString() public static method

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
return string
    public static function randomString($len = null, $chars = null, $cb = null, $pri = 0, $hang = false)
    {
        if ($len === null) {
            $len = 64;
        }
        if ($chars === null) {
            $chars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789_-.';
        }
        if ($cb === null) {
            Daemon::log('[CODE WARN] \\PHPDaemon\\Utils\\Crypt::randomString: non-callback way is not secure.' . ' Please rewrite your code with callback function in third argument' . PHP_EOL . Debug::backtrace());
            $r = '';
            $m = mb_orig_strlen($chars) - 1;
            for ($i = 0; $i < $len; ++$i) {
                $r .= $chars[mt_rand(0, $m)];
            }
            return $r;
        }
        $charsLen = mb_orig_strlen($chars);
        $mask = static::getMinimalBitMask($charsLen - 1);
        $iterLimit = max($len, $len * 64);
        static::randomInts(2 * $len, function ($ints) use($cb, $chars, $charsLen, $len, $mask, &$iterLimit) {
            if ($ints === false) {
                $cb(false);
                return;
            }
            $r = '';
            for ($i = 0, $s = sizeof($ints); $i < $s; ++$i) {
                // This is wasteful, but RNGs are fast and doing otherwise adds complexity and bias
                $c = $ints[$i] & $mask;
                // Only use the random number if it is in range, otherwise try another (next iteration)
                if ($c < $charsLen) {
                    $r .= static::stringIdx($chars, $c);
                }
                // Guarantee termination
                if (--$iterLimit <= 0) {
                    return false;
                }
            }
            $d = $len - mb_orig_strlen($r);
            if ($d > 0) {
                static::randomString($d, $chars, function ($r2) use($r, $cb) {
                    $cb($r . $r2);
                });
                return;
            }
            $cb($r);
        }, $pri, $hang);
    }

Usage Example

Exemplo n.º 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]);
         });
     });
 }
All Usage Examples Of PHPDaemon\Utils\Crypt::randomString