Skip to content
github-actions[bot] edited this page Nov 22, 2026 · 3 revisions

InitPHP Encryption — Documentation

Welcome to the official documentation for initphp/encryption — a small, opinionated symmetric encryption library for PHP 8.2+ built on ext-openssl and ext-sodium.

The package wraps PHP's two production-grade cryptographic extensions behind a single, hard-to-misuse API. Pick a handler, pass a key, encrypt anything — the library handles key derivation, authentication, format versioning and safe serialization for you.

composer require initphp/encryption
use InitPHP\Encryption\Encrypt;
use InitPHP\Encryption\Sodium;

$handler = Encrypt::use(Sodium::class, ['key' => getenv('APP_KEY')]);

$ciphertext = $handler->encrypt(['user_id' => 42, 'role' => 'admin']);
// → "020047ae3f8c..." — hex, safe for cookies / DBs / URLs

$payload = $handler->decrypt($ciphertext);
// → ['user_id' => 42, 'role' => 'admin']

The package ships two handlers and one extension point:

Class Purpose
OpenSSL AES-family ciphers with HMAC authentication; configurable cipher and hash.
Sodium libsodium's XSalsa20-Poly1305 AEAD; no tunables to get wrong.
BaseHandler Abstract base for building your own handler around any primitive.

Both built-in handlers implement the same HandlerInterface, so swapping between them is a constructor change.

Start Here

Navigation

Getting Started

Handlers

Reference

Practical Guides

Other

At a Glance — Handler Comparison

Capability OpenSSL Sodium
Authenticated encryption ✅ (HMAC, encrypt-then-MAC) ✅ (Poly1305 AEAD)
Random IV / nonce per call ✅ (random_bytes) ✅ (random_bytes)
Accepts any-length user key ✅ (HKDF) ✅ (BLAKE2b)
Configurable cipher ✅ —
Configurable hash ✅ —
Length-hiding padding — ✅ (sodium_pad)
Self-describing format header ✅ (version + serializer flag) ✅ (version + serializer flag)
JSON serializer (default) ✅ ✅
PHP serializer (opt-in, safe) ✅ ✅
Single exception class for all errors ✅ ✅

If you have no opinion, use the Sodium handler. It is harder to misuse, and the defaults are correct.

What You Get

  • Authenticated by default. No "encrypt without authenticate" mode — tampered ciphertexts always raise an exception, never decrypt to garbage.
  • Versioned wire format. Every ciphertext begins with a magic byte (0x02); future format changes will bump it and reject older ciphertexts with a clear error instead of silent corruption.
  • One exception type. Every failure raises InitPHP\Encryption\Exceptions\EncryptionException. One catch covers everything — see Error Handling.
  • Safe-by-default serialization. JSON is the default, so the historical unserialize()-on-attacker-bytes pitfall is closed without you having to think about it. PHP serialize stays available as an opt-in for binary payloads, with allowed_classes: false always set.
  • Free key flexibility. Pass any non-empty string for key; the handler derives a key of the correct length internally.

Conventions in This Documentation

  • "Ciphertext" always means the hex string returned by encrypt(). The underlying bytes are described as "binary".
  • The 2-byte header on every ciphertext is the format header; byte 0 is the version byte (0x02 in this release), byte 1 is the serializer flag (0x00 = JSON, 0x01 = php_serialize). See Ciphertext Format.
  • "User key" is whatever string you pass in the key option. "Derived key" is what the handler feeds to the cryptographic primitive after key derivation (HKDF for OpenSSL, BLAKE2b for Sodium).

Package Metadata

If something in this documentation is unclear, wrong, or out of date, open an issue — doc fixes are merged eagerly.

Clone this wiki locally