【 PHP 】项目使用 libsodium 扩展

开始

前面说了微信小微商户下载证书返回的密文用 AEAD_AES_256_GCM 算法 解密的方法。其中用到了 string sodium_crypto_aead_aes256gcm_decrypt ( string $ciphertext , string $ad , string $nonce , string $key ) 这个函数。但是这个函数可能你们调用的时候会报错,那是因为使用这个函数需要开启 libsodium 扩展才能使用。


官方文档上面对该扩展的说明如下:

As of PHP 7.2.0 this extension is bundled with PHP. For older PHP versions this extension is available via PECL.
libsodium需求
libsodium安装说明


windows 安装方法

(PHP ≥ 7.2) 直接去 php.ini 开启扩展就行 ,extension=php_sodium.dll 。因为 PHP7.2 版本后 PHP core 中就整合加密库 Libsodium
(PHP < 7.2.0) 需要安装,libsodium PECL 地址 http://pecl.php.net/package/libsodium然后下载 DLL 文件
在这里插入图片描述
在这里插入图片描述
下载完成后解压,然后

  1. php_sodium.dll 文件放到你的php安装目录下的ext目录下(我这运行的是phpStudy安装的php环境)
  2. libsodium.dll 文件直接放到PHP安装的根目录
  3. 配置下php.ini,开启使用扩展extension=php_sodium.dll
  4. 最后重启下php,
  5. 查看phpinfo,大功告成
    在这里插入图片描述
    在这里插入图片描述
    在这里插入图片描述
    在这里插入图片描述

对于PHP 7以下版本windows上我测试了php 5.6,1.0.6 version 能安装但是不能使用 sodium_crypto_aead_aes256gcm_decrypt 函数。

在这里插入图片描述

我们可以在较旧的PHP上使用Libsodium和/或如果我们无法安装PHP扩展吗?

Sodium Compat PHP 类库

Sodium Compat 是用于 Sodium 加密库(libsodium)的纯 PHP 填充,它是 PHP 7.2.0+ 的核心扩展,也可用于 PECL。
这个库 tentativeley 支持PHP 5.2.4 - 7.x(最新版),但官方只支持非 EOL 版本的 PHP。
如果安装了 PHP 扩展,Sodium Compat 将机会性地透明地使用 PHP 扩展而不是我们的实现。
github 地址: https://github.com/paragonie/sodium_compat

2019-01-07新增

liunx 下安装方法

看完这个基本就行了点击查看《在PHP项目中使用Libsodium》

编译安装

下载一份sodium 安装包(最好是最新的稳定版本),然后遵循以下步骤

# 先解压,进入目录
./configure
make && make check
sudo make install
pecl安装

然后。。。对了,如果您的系统上没有安装PECL包管理器,请确保首先安装。在Internet上有针对PHP支持的几乎所有操作系统安装PECL的指南。然后执行下面的命令

pecl install libsodium

最后别忘了在 php.ini 中添加

extension=sodium.so

最后最后查看 phpinfo如下图就代表安装成功
在这里插入图片描述

  • 5
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 3
    评论
Halite 是PHP项目中一个简单的libsodium封装包。Halite提供高级的加密接口,依靠其所有潜在的密码操作libsodium。Halite 基本 API:EncryptionAnonymousAuthenticatedAsymmetric\Crypto::seal(HiddenString, EncryptionPublicKey): stringAsymmetric\Crypto::unseal(string, EncryptionSecretKey): HiddenStringAsymmetric\Crypto::encrypt(HiddenString, EncryptionSecretKey, EncryptionPublicKey): stringAsymmetric\Crypto::decrypt(string, EncryptionSecretKey, EncryptionPublicKey): HiddenStringSymmetric\Crypto::encrypt(HiddenString, EncryptionKey): stringSymmetric\Crypto::decrypt(string, EncryptionKey): HiddenStringSymmetricAsymmetricAuthenticationAsymmetric\Crypto::sign(string, SignatureSecretKey): stringAsymmetric\Crypto::verify(string, SignaturePublicKey, string): boolSymmetric\Crypto::authenticate(string, AuthenticationKey): stringSymmetric\Crypto::verify(string, AuthenticationKey, string): boolSymmetricAsymmetric示例1:加密解密消息首先,生成一个key<?php use ParagonIE\Halite\KeyFactory; $encKey = KeyFactory::generateEncryptionKey(); KeyFactory::save($encKey, '/path/outside/webroot/encryption.key');然后,加密解密消息<?php use ParagonIE\Halite\HiddenString; use ParagonIE\Halite\KeyFactory; use ParagonIE\Halite\Symmetric\Crypto as Symmetric; $encryptionKey = KeyFactory::loadEncryptionKey('/path/outside/webroot/encryption.key'); $message = new HiddenString('This is a confidential message for your eyes only.'); $ciphertext = Symmetric::encrypt($message, $encryptionKey); $decrypted = Symmetric::decrypt($ciphertext, $encryptionKey); var_dump($decrypted === $message); // bool(true)示例2:用password-derived key 加密消息<?php use ParagonIE\Halite\HiddenString; use ParagonIE\Halite\KeyFactory; use ParagonIE\Halite\Symmetric\Crypto as Symmetric; $passwd = new HiddenString('correct horse battery staple'); // Use random_bytes(16); to generate the salt: $salt = "\xdd\x7b\x1e\x38\x75\x9f\x72\x86\x0a\xe9\xc8\x58\xf6\x16\x0d\x3b"; $encryptionKey = KeyFactory::deriveEncryptionKey($passwd, $salt); $message = new HiddenString('This is a confidential message for your eyes only.'); $ciphertext = Symmetric::encrypt($message, $encryptionKey); echo $ciphertext, "\n"; 标签:Halite

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值