PHP:将 Libsodium 安装到 PHP v5.5

PHP: Installing Libsodium to PHP v5.5

如何使用 PHP 5.5 版正确安装 Libsodium。 我正在尝试按照 https://paragonie.com/book/pecl-libsodium/read/00-intro.md#installing-libsodium

上的说明进行操作

以下是我执行的步骤:

  1. 转到http://windows.php.net/downloads/pecl/releases/libsodium/1.0.6/

  2. 下载 "php_libsodium-1.0.6-5.5-nts-vc11-x64.zip" 并提取文件。

  3. 将"libsodium.dll"复制到我的目录"C:\Program Files (x86)\PHP\v5.5"中"php.exe"

  4. 复制"php_libsodium.dll"到我的目录"C:\Program Files (x86)\PHP\v5.5\ext"

  5. 在 php.ini 文件中启用 "extension=php_libsodium.dll"

  6. 重启服务器

但是当我通过编写一个简单的 PHP 测试文件对其进行测试时:

<?php
error_reporting(E_ALL);
ini_set('display_errors', TRUE);
ini_set('display_startup_errors', TRUE);

// hash the password and return an ASCII string suitable for storage
$hash_str = sodium_crypto_pwhash_str(
        "mypassword",
        SODIUM_CRYPTO_PWHASH_OPSLIMIT_INTERACTIVE,
        SODIUM_CRYPTO_PWHASH_MEMLIMIT_INTERACTIVE
        );

echo "hash: " . $hash_str;
?>

结果页面显示错误:

Fatal error: Call to undefined function sodium_crypto_pwhash_str() in C:\PHP\testLibsodium.php on line 7

库 Libsodium 似乎没有安装,因为它不知道函数。 在 PHP 5.5 版中安装 PHP Libsodium 需要做什么? 非常感谢。

更新:我已经按照@iann 的建议安装了 X86 版本,运行 此代码:

$storeInDatabase = \Sodium\crypto_pwhash_str(
        "safasfdwr32sfdfas234",
        SODIUM_CRYPTO_PWHASH_OPSLIMIT_INTERACTIVE,
        SODIUM_CRYPTO_PWHASH_MEMLIMIT_INTERACTIVE
        );

现在似乎正在读取函数,但出现错误:

Notice: Use of undefined constant SODIUM_CRYPTO_PWHASH_OPSLIMIT_INTERACTIVE - assumed 'SODIUM_CRYPTO_PWHASH_OPSLIMIT_INTERACTIVE'

Notice: Use of undefined constant SODIUM_CRYPTO_PWHASH_MEMLIMIT_INTERACTIVE - assumed 'SODIUM_CRYPTO_PWHASH_MEMLIMIT_INTERACTIVE'

Warning: Sodium\crypto_pwhash_str() expects parameter 2 to be long

Catchable fatal error: crypto_pwhash_str(): invalid parameters

这是否意味着我的 libsodium 安装正确,但为什么会出现错误?再次感谢。

sodium_* 函数不在全局命名空间中,直到该库在版本 7.2 中移至本机 PHP。来自 the manual:

In PHP before 7.2 with libsodium from PECL, the functions below were defined in the Sodium name space. In PHP 7.2, the namespaces were dropped in favor of a sodium_ prefix (to conform to the PHP internal development standards).

因此,如果您从 PECL 安装,则需要使用以前的函数名称。在你的情况下:

$hash_str = \Sodium\crypto_pwhash_str(
...