PHPDaemon\Utils\Crypt::hash PHP Method

hash() public static method

Generate keccak hash for string with salt
Deprecation:
public static hash ( string $str, string $salt = '', boolean $plain = false ) : string
$str string Data
$salt string Salt
$plain boolean Is plain text?
return string
    public static function hash($str, $salt = '', $plain = false)
    {
        $size = 512;
        $rounds = 1;
        if (strncmp($salt, '$', 1) === 0) {
            $e = explode('$', $salt, 3);
            $ee = explode('=', $e[1]);
            if (ctype_digit($ee[0])) {
                $size = (int) $e[1];
            }
            if (isset($ee[1]) && ctype_digit($e[1])) {
                $size = (int) $e[1];
            }
        }
        $hash = $str . $salt;
        if ($rounds < 1) {
            $rounds = 1;
        } elseif ($rounds > 128) {
            $rounds = 128;
        }
        for ($i = 0; $i < $rounds; ++$i) {
            $hash = \keccak_hash($hash, $size);
        }
        if ($plain) {
            return $hash;
        }
        return base64_encode($hash);
    }

Usage Example

Beispiel #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::hash