(为清楚起见进行了编辑-在接受的答案中滚动)
Libsodium 已经为 PHP 7 做好了准备。在此过程中,命名空间被删除,并添加了用于方法的前缀 sodium_ 和用于常量的 SODIUM_ 前缀。版本方法也被删除。
此 github 页面记录了所有新函数和常量,并且该项目提供与 \Sodium 命名空间的向后兼容性:https ://github.com/Firehed/sodium/blob/master/src/we_cant_have_nice_things.php
配方:在 PHP 7 和 AWS AMI 上安装 Libsodium
# PHP 7.0 Libsodium install AWS AMI
yum install -y php7-pear re2c php70-devel
yum groupinstall -y "Development Tools"
pecl7 install libsodium
vi /etc/php-7.0.d/20-libsodium.ini
; Enable libsodium extension module
extension=sodium.so
service httpd restart
命令行测试以验证是否安装了钠
php7 --info | grep sodium
测试 php 函数以验证密码哈希的调用模式
<?php
$password = "hello";
$hash_str = sodium_crypto_pwhash_str(
$password,
\SODIUM_CRYPTO_PWHASH_OPSLIMIT_INTERACTIVE,
\SODIUM_CRYPTO_PWHASH_MEMLIMIT_INTERACTIVE
);
var_dump($password, $hash_str);
感谢@GracefulRestart 的帮助。