【问题标题】:Including Paragonie Halite in project doesn't find variables and functions在项目中包含 Paragonie Halite 找不到变量和函数
【发布时间】:2016-06-07 14:32:53
【问题描述】:

我已经在 Windows 上为 PHP 7 安装了 libsodium,并且正在使用 PHPStorm 开发我的项目。我还安装了来自 Paragonie 的 Halite,如果未正确安装 libsodium 扩展,它甚至无法安装。 IDE 还会找到使用的函数,并在单击变量等时打开 libsodium 的拟合文件。

但不幸的是,在我的设置中,我收到以下错误:

Uncaught Error: Undefined constant 'Sodium\CRYPTO_PWHASH_OPSLIMIT_INTERACTIVE' in C:\Server\nginx-1.11.1\html\mywebsite\vendor\paragonie\halite\src\KeyFactory.php:344

我的 index.php 文件如下所示:

    <?php
    require_once 'vendor/autoload.php';

    spl_autoload_register(function ($class) {
        require_once $class . '.php';
    });

    $router = new AltoRouter();

    $router->setBasePath('/' . basename(__DIR__));

    require_once 'config/routes.php';

    $match = $router->match();

    $GLOBALS['db'] = new \config\database(true, null);

    try
    {
        if ($match) {
            $routeController = new Modules\Core\RouteController($match);
            echo $routeController->bootstrap();
        }
    }
    catch (Exception $ex)
    {
        //@todo log every Exception
    }

我还使用 Jquery 提交我的表单,这会成功执行以下函数(未找到 libsodium 函数/变量):

public function hash($password)
{
    $this->setEncryptionKey();
    return Password::hash(
        $password, // string
        $this->encryption_key      // \ParagonIE\Halite\Symmetric\EncryptionKey
    );
}

public function check($stored_hash, $password)
{
    try {
        if (Password::verify(
            $password, // string
            $stored_hash,
            $this->encryption_key
        )) {
            return true;
        }
    } catch (\ParagonIE\Halite\Alerts\InvalidMessage $ex) {
        // Handle an invalid message here. This usually means tampered ciphertext.
        throw new \Exception($ex);
    }
}

我已经将php_libsodium.dll 安装在扩展目录中,并将libsodium.dll 放在php 服务器的目录中。只是为了尝试,我稍后也将它放在 system32 目录和 Syswow64 目录中 - 两者都没有改变任何东西。

我刚刚用 composer 安装了 paragonie 的 halite 库,但不幸的是,使用它比我想象的要难。我也尝试在没有 Halite 的情况下使用 libsodium 扩展,但这会导致类似的错误,即找不到所需的类、常量和函数等。

我也尝试过更改我的自动加载器,但奇怪的是,IDE 可以毫无问题地找到所有内容,但在浏览器中执行脚本却没有。

【问题讨论】:

  • 1) 您是否启用 php.ini 中的扩展程序以及 2) 您是否重新启动了 Web 服务器?
  • 对两者。我只是想了一下:可能是版本问题,因为我安装了 libsodium 的 1.0.6 版本,但也存在 1.0.10 版本。我没有自己编译它,而是使用了 Windows 的预编译 dll 文件。我还用 phpinfo() 检查了它,它说 libsodium 支持已启用,但库版本仅为 1.0.5,而编译版本为 1.0.6

标签: php php-extension libsodium


【解决方案1】:

要使用 Halite 2.x,您需要:

  • Libsodium 1.0.9+(如果您在编译 .9 时遇到问题,最好使用 1.0.10)
  • PECL Libsodium 1.0.3+(最好是 1.0.6)针对 libsodium 1.0.9+ 编译。

验证是否已设置的简单方法是:

<?php
use \ParagonIE\Halite\Halite;

var_dump(Halite::isLibsodiumSetupCorrectly());

如果您想了解更多关于失败原因的具体信息,只需运行以下代码:

<?php
use \ParagonIE\Halite\Halite;

// TRUE enables verbose output:
Halite::isLibsodiumSetupCorrectly(true);

您很可能需要卸载/重新安装针对较新版本的共享库编译的 PHP 扩展。

如果被接受,有一个RFC under discussion for PHP 7.1 right now 将使这在未来变得轻松。 RFC 几年后通过了; libsodium 将在 PHP 7.2 中。

【讨论】:

  • 这是一个有用的答案,因为验证返回 false。谢谢。
猜你喜欢
  • 2018-01-13
  • 1970-01-01
  • 2016-11-13
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-07-22
  • 2023-03-06
相关资源
最近更新 更多