Search by

phpkernel / encryption

phpkernel

AES-256-GCM symmetric encryption and CSRF token utilities for PHP 8.5+

Package info

codeberg.org/phpkernel/encryption

Issues

pkg:composer/phpkernel/encryption

Statistics

Installs: 0

Dependents: 0

Suggesters: 0

v1.0.0 2026-12-03 19:57 UTC

This package is auto-updated.

Last update: 2026-12-03 17:00:38 UTC


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 using hash_equals().

Requirements

  • PHP >= 8.5
  • ext-openssl
  • ext-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.