Halite是一个高级加密接口,它的所有底层加密操作都依赖于 libsodium。
Halite 是由 Paragon Initiative Enterprises 创建的,是我们不断努力改善生态系统并使 PHP 中的加密技术更安全、更易于实施的结果。
您可以在线阅读Halite 文档。
Halite 是根据 Mozilla 公共许可证 2.0 发布的。如果您希望扩展 Halite 而不根据 MPL 条款提供衍生作品,可以从 Paragon Initiative Enterprises 获得商业许可证。
如果您对后端 Web 应用程序的 MPL 软件条款感到满意,但想为使用 Halite 的应用程序购买支持合同,Paragon Initiative Enterprises 也可以提供这些支持合同。
重要提示:早期版本的 Halite 可根据 GNU 公共许可证版本 3 (GPLv3) 获得。 Mozilla 公共许可条款下仅提供 Halite 4.0.1 及更高版本。
在使用 Halite 之前,您必须选择适合您项目要求的版本。下面简要强调了可用版本的 Halite 的要求之间的差异。
PHP | 钠 | PECL钠 | 支持 | |
---|---|---|---|---|
石盐 5.1 及更新版本 | 8.1.0 | 1.0.18 | 不适用(标准) | 积极的 |
石盐 5.0.x | 8.0.0 | 1.0.18 | 不适用(标准) | 积极的 |
岩盐 4.1+ | 7.2.0 | 1.0.15 | 不适用(标准) | 不支持 |
石盐4.0 | 7.2.0 | 1.0.13 | 不适用(标准) | 不支持 |
石盐3 | 7.0.0 | 1.0.9 | 1.0.6 / 2.0.4 | 不支持 |
石盐2 | 7.0.0 | 1.0.9 | 1.0.6 | 不支持 |
石盐1 | 5.6.0 | 1.0.6 | 1.0.2 | 不支持 |
注意:Halite 5.0.x 适用于 PHP 8.0,但性能比 PHP 8.1 差。
如果您需要 5.1 之前的 Halite 版本,请参阅与该特定分支相关的文档。
要安装 Halite,首先需要安装 libsodium。您可能需要也可能不需要 PHP 扩展。对于大多数人来说,这意味着跑步...
sudo apt-get install php7.2-sodium
...或适用于您的操作系统和 PHP 版本的等效命令。
如果您遇到困难,@aolko 提供的分步指南可能会有所帮助。
安装先决条件后,通过 Composer 安装 Halite:
composer require paragonie/halite:^5
对 Halite 的免费(免费)支持仅扩展到最新的主要版本(当前为 5)。
如果您的公司需要旧版本 Halite 的支持,请联系 Paragon Initiative Enterprises 询问商业支持选项。
如果您需要一种简单的方法来从旧版本的 Halite 迁移,请查看halite-legacy。
查看文档。基本的 Halite API 的设计是为了简单:
SymmetricCrypto::encrypt
( HiddenString
, EncryptionKey
): string
SymmetricCrypto::encryptWithAD
( HiddenString
, EncryptionKey
, string
): string
SymmetricCrypto::decrypt
( string
, EncryptionKey
): HiddenString
SymmetricCrypto::decryptWithAD
( string
, EncryptionKey
, string
): HiddenString
AsymmetricCrypto::seal
( HiddenString
, EncryptionPublicKey
): string
AsymmetricCrypto::unseal
( string
, EncryptionSecretKey
): HiddenString
AsymmetricCrypto::encrypt
( HiddenString
、 EncryptionSecretKey
、 EncryptionPublicKey
): string
AsymmetricCrypto::encryptWithAD
( HiddenString
, EncryptionSecretKey
, EncryptionPublicKey
, string
): string
AsymmetricCrypto::decrypt
( string
, EncryptionSecretKey
, EncryptionPublicKey
): HiddenString
AsymmetricCrypto::decryptWithAD
( string
, EncryptionSecretKey
, EncryptionPublicKey
, string
): HiddenString
SymmetricCrypto::authenticate
( string
, AuthenticationKey
): string
SymmetricCrypto::verify
( string
, AuthenticationKey
, string
): bool
AsymmetricCrypto::sign
( string
, SignatureSecretKey
): string
AsymmetricCrypto::verify
( string
, SignaturePublicKey
, string
): bool
首先,生成并保留密钥一次:
<?php
use ParagonIE Halite KeyFactory ;
$ encKey = KeyFactory:: generateEncryptionKey ();
KeyFactory:: save ( $ encKey , ' /path/outside/webroot/encryption.key ' );
然后你可以像这样加密/解密消息:
<?php
use ParagonIE Halite KeyFactory ;
use ParagonIE Halite Symmetric Crypto as Symmetric ;
use ParagonIE HiddenString HiddenString ;
$ 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 -> getString () === $ message -> getString ()); // bool(true)
这应该会产生类似于以下内容的内容:
MUIDAEpQznohvNlQ-ZRk-ZZ59Mmox75D_FgAIrXY2cUfStoeL-GIeAe0m-uaeURQdPsVmc5XxRw3-2x5ZAsZH_es37qqFuLFjUI-XK9uG0s30YTsorWfpHdbnqzhRuUOI09c-cKrfMQkNBNm0dDDwZazjTC48zWikRHSHXg8NXerVDebzng1aufc_S-osI_zQuLbZDODujEnpbPZhMMcm4-SWuyVXcBPdGZolJyT
重要提示:Halite 适用于
Key
对象,而不是字符串。
如果您尝试echo
关键对象,您将得到一个空字符串而不是其内容。如果您尝试var_dump()
一个密钥对象,您只会获得有关密钥类型的一些事实。
如果要检查密钥的原始二进制内容,则必须显式调用$obj->getRawKeyMaterial()
。对于大多数用例,不建议这样做。
<?php
use ParagonIE Halite KeyFactory ;
use ParagonIE HiddenString HiddenString ;
$ passwd = new HiddenString ( ' correct horse battery staple ' );
// Use random_bytes(16); to generate the salt:
$ salt = "xddx7bx1ex38x75x9fx72x86x0axe9xc8x58xf6x16x0dx3b" ;
$ encryptionKey = KeyFactory:: deriveEncryptionKey ( $ passwd , $ salt );
可以使用从密码派生的密钥来代替随机生成的密钥。
Halite 包括一个文件加密类,该类利用流 API 允许在可用内存非常少(即小于 8 MB)的系统上对大文件(例如 GB)进行加密。
<?php
use ParagonIE Halite File ;
use ParagonIE Halite KeyFactory ;
$ encryptionKey = KeyFactory:: loadEncryptionKey ( ' /path/outside/webroot/encryption.key ' );
File:: encrypt ( ' input.txt ' , ' output.txt ' , $ encryptionKey );
PHP 致命错误:Uncaught SodiumException:这未实现,因为无法安全地从 PHP 擦除内存
解决方案是确保安装/启用 libsodium。有关更多信息,请参阅本自述文件的上方。
如果您的公司在其产品或服务中使用此库,您可能有兴趣从 Paragon Initiative Enterprises 购买支持合同。