mmeyer2k/secretbox
最新稳定版本:v0.0.0
Composer 安装命令:
composer require mmeyer2k/secretbox
包简介
A minimalist wrapper for libsodium secretbox
README 文档
README
A minimalist libsodium secretbox implementation for PHP, supporting key rotation.
Features
- Encrypt and decrypt messages using libsodium's secretbox
- Support for key rotation (multiple keys for decryption)
- Simple API for secure key management
Installation
Install via Composer:
composer require mmeyer2k/secretbox
Requires PHP 8.2+ and the Sodium extension.
Usage
Basic encryption and decryption:
use \Mmeyer2k\SecretBox\SecretBox; $key = random_bytes(32); // 32 bytes required $ciphertext = SecretBox::encrypt('secret message', $key); $plaintext = SecretBox::decrypt($ciphertext, $key);
Key Management
Creating a Key
Generate a secure 32-byte key:
head -c 32 /dev/urandom | base64 -w 0
Storing a Key
Store keys in environment variables or configuration files as base64 strings. Decode before use:
$key = base64_decode('[your base64 key]');
Key Rotation
Support multiple keys for seamless rotation:
$plaintext = SecretBox::decrypt($ciphertext, [ $oldKey, $newKey, ]);
Decryption will succeed with any valid key in the array.
Error Handling
If decryption fails (e.g., no matching key), a \SodiumException is thrown:
try { $plaintext = SecretBox::decrypt($ciphertext, $key); } catch (\SodiumException $e) { // Handle decryption failure }
License
MIT
统计信息
- 总下载量: 85
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 0
- 点击次数: 0
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2025-01-23