phpkernel / encryption
AES-256-GCM symmetric encryption and CSRF token utilities for PHP 8.5+
v1.0.0
2026-12-03 19:57 UTC
Requires
- php: >=8.5
- ext-openssl: *
- ext-posix: *
Requires (Dev)
- friendsofphp/php-cs-fixer: ^3.95
- infection/infection: ^0.35.12
- phpstan/phpstan: ^2.3
- phpunit/phpunit: ^13.4
Suggests
None
Provides
None
Conflicts
None
Replaces
None
README
Lightweight symmetric encryption (AES-256-GCM) and CSRF protection library for PHP 8.5+ with zero external runtime dependencies.
Features
- Encryption (EncrypterInterface / Encrypter): Authenticated AES-256-GCM symmetric encryption with 12-byte IVs, 16-byte tags, and strict Base64 validation.
- Key Security: Uses
#[\SensitiveParameter], masks secrets in__debugInfo(), and prevents serialization via__serialize(). - CSRF Tokens (CsrfToken & VerifyToken): Generates cryptographically secure 32-character hex tokens in encrypted cookies (
HttpOnly,Secure,SameSite=Lax) and performs constant-time validation usinghash_equals().
Requirements
- PHP
>= 8.5 ext-opensslext-posix
Installation
composer require phpkernel/encryption
Usage
Symmetric Encryption
use PHPKernel\Encryption\Encrypter;
$key = random_bytes(32); // 32 bytes for AES-256-GCM
$encrypter = new Encrypter($key);
$encrypted = $encrypter->encrypt('sensitive payload');
$decrypted = $encrypter->decrypt($encrypted);
CSRF Token Generation and Verification
use PHPKernel\Encryption\CsrfToken;
use PHPKernel\Encryption\VerifyToken;
$key = random_bytes(32); // 32 bytes for AES-256-GCM
$token = new CsrfToken($key)->csrfToken();
$verifier = new VerifyToken($key);
if ($verifier->verifyToken($_POST['_csrf'] ?? null, $_COOKIE['_csrf'] ?? null)) {
throw new \RuntimeException('Invalid CSRF token.');
}
Code Quality and Testing
Verified with PHP CS Fixer, PHPStan (level: max), PHPUnit 13, and Infection (100% Code Coverage and 100% MSI):
composer check
composer mutate
composer ci
License
This library is open-sourced software licensed under the MIT license.