petrknap / crypto-sodium
加密钠
v1.3.0
2024-04-28 10:43 UTC
Requires
- php: >=8.1
- ext-mbstring: *
- ext-sodium: *
- petrknap/binary: ^4.0
- petrknap/shorts: ^2.1
Requires (Dev)
- nunomaduro/phpinsights: ^2.11
- phpstan/phpstan: ^1.10
- phpunit/phpunit: ^10.5
- squizlabs/php_codesniffer: ^3.7
This package is auto-updated.
Last update: 2024-09-04 09:25:40 UTC
README
这是一个简单的库,将 功能性的 sodium_crypt_*
封装成对象。
输入和输出都是二进制数据,大胆使用 petrknap/binary
。
示例
对称块加密
use PetrKnap\CryptoSodium\SecretBox; $secretBox = new SecretBox(); $message = 'Hello World!'; $key = $secretBox->generateKey(); $ciphertext = $secretBox->encrypt($message, $key); echo $secretBox->decrypt($ciphertext, $key); $secretBox->eraseData($key);
非对称块加密
use PetrKnap\CryptoSodium\Box; $box = new Box(); $message = 'Hello World!'; $keyPair = $box->generateKeyPair(); $ciphertext = $box->encrypt($message, $keyPair); echo $box->decrypt($ciphertext, $keyPair); $box->eraseData($keyPair);
对称流加密
use PetrKnap\CryptoSodium\SecretStream\XChaCha20Poly1305; $xChaCha20Poly1305 = new XChaCha20Poly1305(); $messageChunk1 = 'Hello '; $messageChunk2 = 'World!'; $key = $xChaCha20Poly1305->generateKey(); $pushStream = $xChaCha20Poly1305->initPush($key); $ciphertextHeader = $pushStream->header; $ciphertextChunk1 = $pushStream->push($messageChunk1); $ciphertextChunk2 = $pushStream->push($messageChunk2, tag: XChaCha20Poly1305::TAG_FINAL); $pullStream = $xChaCha20Poly1305->initPull($ciphertextHeader, $key); echo $pullStream->pull($ciphertextChunk1); echo $pullStream->pull($ciphertextChunk2); $xChaCha20Poly1305->eraseData($key);
带有附加数据的对称块加密
use PetrKnap\CryptoSodium\Aead\Aes256Gcm; $aes256Gcm = new Aes256Gcm(); $message = 'Hello World!'; $purpose = 'example'; $key = $aes256Gcm->generateKey(); $ciphertext = $aes256Gcm->encrypt($message, $key, additionalData: $purpose); echo $aes256Gcm->decrypt($ciphertext, $key, additionalData: $purpose); $aes256Gcm->eraseData($key);
运行 composer require petrknap/crypto-sodium
安装它。您可以通过 捐赠 支持此项目。该项目遵循 LGPL-3.0-or-later
的条款。