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 ): stringSymmetricCrypto::encryptWithAD ( HiddenString , EncryptionKey , string ): stringSymmetricCrypto::decrypt ( string , EncryptionKey ): HiddenStringSymmetricCrypto::decryptWithAD ( string , EncryptionKey , string ): HiddenStringAsymmetricCrypto::seal ( HiddenString , EncryptionPublicKey ): stringAsymmetricCrypto::unseal ( string , EncryptionSecretKey ): HiddenStringAsymmetricCrypto::encrypt ( HiddenString 、 EncryptionSecretKey 、 EncryptionPublicKey ): stringAsymmetricCrypto::encryptWithAD ( HiddenString , EncryptionSecretKey , EncryptionPublicKey , string ): stringAsymmetricCrypto::decrypt ( string , EncryptionSecretKey , EncryptionPublicKey ): HiddenStringAsymmetricCrypto::decryptWithAD ( string , EncryptionSecretKey , EncryptionPublicKey , string ): HiddenStringSymmetricCrypto::authenticate ( string , AuthenticationKey ): stringSymmetricCrypto::verify ( string , AuthenticationKey , string ): boolAsymmetricCrypto::sign ( string , SignatureSecretKey ): stringAsymmetricCrypto::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 購買支援合約。