PHP Classes

How to Use a PHP Cryptography Library to Encrypt and Decrypt Data Using HKDF to Create Stronger Derived KeysCrypto: Encrypt and decrypt data using rotating keys

Recommend this page to a friend!
  Info   Documentation   View files Files   Install with Composer Install with Composer   Download Download   Reputation   Support forum   Blog    
Last Updated Ratings Unique User Downloads Download Rankings
2026-07-22 (4 months ago) RSS 2.0 feedNot yet rated by the usersTotal: Not yet counted Not yet ranked
Version License PHP version Categories
crypto 1.0MIT/X Consortium ...8.1Cryptography, PHP 8
Description 

Author

This package can encrypt and decrypt data using rotating keys.

It provides a set of modules to implement cryptographic functions:

Currently it provides modules that implement the functions:

- Password hashing and verification

- Rotate cryptographic keys

- Create derived keys following the HKDF standard

Picture of Mohamed Abdulalim
  Performance   Level  
Name: Mohamed Abdulalim <contact>
Classes: 2 packages by
Country: Egypt Egypt
Age: 46
All time rank: Not yet ranked
Week rank: Not yet ranked
Innovation award
Innovation award
Nominee: 1x

Instructions

Please read this document to learn how to use this package encrypt and decrypt data using .

Documentation

Maatify Crypto

Latest Version PHP Version License

PHPStan

Changelog Security

Monthly Downloads Total Downloads

Security Audit

Security First Maatify Ecosystem

Install

Installation

composer require maatify/crypto

Quick Example

use Maatify\Crypto\DX\CryptoProvider;
use Maatify\Crypto\Reversible\DTO\ReversibleCryptoMetadataDTO;

$crypto = $container->get(CryptoProvider::class);

$service = $crypto->context("user:email:v1");

$encrypted = $service->encrypt("hello world");

$metadata = new ReversibleCryptoMetadataDTO(
    $encrypted['result']->iv,
    $encrypted['result']->tag
);

$plain = $service->decrypt(
    $encrypted['result']->cipher,
    $encrypted['key_id'],
    $encrypted['algorithm'],
    $metadata
);

Documentation

For full documentation and detailed usage guides, see the official documentation book:

?? Documentation Book

The documentation includes:

  • Architecture overview
  • Key rotation lifecycle
  • Context-based encryption
  • Password hashing pipeline
  • Security design guarantees
  • Integration examples

1. Overview

This library is a Security-First cryptographic system providing a set of strict, isolated, and frozen cryptographic primitives. It is designed to be extracted as a standalone library, independent of any specific application framework.

The system enforces a strict separation of concerns between key lifecycle management, key derivation, reversible encryption, and password hashing. It prioritizes safety, deterministic behavior, and fail-closed security over developer convenience.

All cryptographic primitives within this library are frozen, meaning their core logic, algorithms, and security boundaries are immutable. Any significant change to these primitives requires a formal architectural review.

Cryptographic Design

The library follows modern cryptographic best practices:

  • AES-256-GCM authenticated encryption
  • HKDF domain separation (RFC 5869)
  • Argon2id password hashing
  • Secure randomness via `random_bytes()`
  • Strict key rotation lifecycle (Active / Inactive / Retired)
  • Fail-closed cryptographic operations

2. Library Breakdown

The cryptographic system is composed of five distinct, decoupled components:

1. Password Component (Password/)

Purpose: Secure, irreversible password hashing and verification.

What it DOES

Implements a strict pipeline of:

HMAC-SHA256 (Pepper) ? Argon2id

Handles:

  • hashing
  • verification
  • rehash checks

What it DOES NOT do

  • manage storage of hashes
  • retrieve pepper secrets

Security Boundary

Operates in total isolation from encryption keys and relies on:

PasswordPepperProviderInterface

2. KeyRotation Module (KeyRotation/)

Purpose: Management of cryptographic key lifecycles and rotation policies.

What it DOES

Enforces strict invariants:

exactly one ACTIVE key at any time

Manages key states:

  • ACTIVE
  • INACTIVE
  • RETIRED

This allows seamless key rotation without downtime.

What it DOES NOT do

  • perform encryption or decryption
  • load keys from storage

Security Boundary

Acts as the sole authority on which key may encrypt new data.

3. HKDF Module (HKDF/)

Purpose: Key Derivation Function implementing RFC 5869.

What it DOES

Derives independent keys from a root key using explicit contexts.

Example contexts:

notification:email:v1
auth:session:v1
payment:token:v1

What it DOES NOT do

  • generate root secrets
  • perform encryption

Security Boundary

Ensures compromise of one domain cannot affect another.

4. Reversible Module (Reversible/)

Purpose: Symmetric encryption and decryption.

Algorithm:

AES-256-GCM

What it DOES

  • encrypt data
  • decrypt data

What it DOES NOT do

  • manage keys
  • derive keys

Security Boundary

Fail-closed behavior:

  • invalid tag ? exception
  • corrupted ciphertext ? exception
  • unsupported algorithm ? exception

Weak algorithms like ECB are explicitly forbidden.

5. DX Module (DX/)

Purpose: Developer Experience orchestration layer.

Provides the CryptoProvider facade.

Example:

$crypto->context("user:email:v1");

What it DOES

Connects primitives into usable pipelines.

What it DOES NOT do

Contains no cryptographic logic.

All security guarantees exist in the lower layers.

3. Architectural Flow

The library supports three secure pipelines.

A. Password Hashing Pipeline

Used for authentication.

Input Password
   ?
HMAC-SHA256 (Pepper)
   ?
Argon2id (Salted)
   ?
Password Hash

B. Context-Based Encryption Pipeline (Recommended)

Used for sensitive application data.

Root Keys (KeyRotation)
       ?
HKDF (Context)
       ?
Derived Keys
       ?
AES-256-GCM Encryption

C. Direct Encryption Pipeline (Advanced)

Used only for internal system secrets.

Root Keys (KeyRotation)
       ?
AES-256-GCM

Key Hierarchy

Root Keys

Managed by KeyRotation.

Never used directly for domain data in the standard pipeline.

Derived Keys

Generated via HKDF.

Used for actual encryption.

Password Secrets

Pepper is fully isolated from encryption keys.

4. Design Principles

Fail-Closed Behavior

All modules throw exceptions on failure.

No silent failures. No fallback algorithms.

Explicit Versioning

Contexts must be versioned.

Example:

auth:token:v1

Domain Separation

Different data domains must use different contexts.

Key Rotation Safety

Old keys remain valid for decryption while new data always uses the active key.

No Implicit Defaults

Algorithms, keys, and contexts must always be explicit.

No Hidden State

Modules are stateless.

They do not cache secrets or persist runtime state.

5. What This Library Is NOT

Not a Framework

No dependency on Laravel, Symfony, etc.

Not a Key Management System

Does not store keys.

Keys must be injected by the host application.

Not a Secrets Loader

Does not read .env.

Secret management belongs to the host application.

Not a Generic Crypto DSL

The library intentionally exposes specific approved primitives only:

  • AES-256-GCM
  • Argon2id
  • HKDF

No driver abstraction.

6. Stability & Extraction Readiness

This library is production-ready.

It was designed from the start to be extractable as a standalone package.

Frozen Components

  • Password
  • KeyRotation
  • HKDF
  • Reversible

Their security contracts are locked.

Optional Components

DX layer is optional.

Consumers may wire primitives manually.

Stability

This library is production-ready and follows Semantic Versioning.


  Files folder image Files (99)  
File Role Description
Files folder image.github (1 directory)
Files folder imagedocs (1 directory)
Files folder imagesrc (7 directories)
Files folder imagetests (6 directories)
Accessible without login Plain text file .php-cs-fixer.php Example Example script
Accessible without login Plain text file CHANGELOG.md Doc. Documentation
Accessible without login Plain text file composer.json Data Auxiliary data
Accessible without login Plain text file HOW_TO_USE.md Doc. Auxiliary data
Accessible without login Plain text file LICENSE Lic. License text
Accessible without login Plain text file phpstan.neon Data Auxiliary data
Accessible without login Plain text file phpunit.xml Data Auxiliary data
Accessible without login Plain text file README.md Doc. Documentation
Accessible without login Plain text file SECURITY.md Doc. Auxiliary data
Accessible without login Plain text file SECURITY_AUDIT.md Doc. Auxiliary data
Accessible without login Plain text file VERSION Data Auxiliary data

  Files folder image Files (99)  /  .github  
File Role Description
Files folder imageworkflows (1 file)

  Files folder image Files (99)  /  .github  /  workflows  
File Role Description
  Accessible without login Plain text file ci.yml Data Auxiliary data

  Files folder image Files (99)  /  docs  
File Role Description
Files folder imagebook (7 files)

  Files folder image Files (99)  /  docs  /  book  
File Role Description
  Accessible without login Plain text file architecture.md Doc. Documentation
  Accessible without login Plain text file hkdf.md Doc. Documentation
  Accessible without login Plain text file index.md Doc. Documentation
  Accessible without login Plain text file key_rotation.md Doc. Documentation
  Accessible without login Plain text file password_hashing.md Doc. Documentation
  Accessible without login Plain text file reversible_encryption.md Doc. Documentation
  Accessible without login Plain text file security_model.md Doc. Documentation

  Files folder image Files (99)  /  src  
File Role Description
Files folder imageBootstrap (1 file)
Files folder imageContract (1 file)
Files folder imageDX (4 files, 1 directory)
Files folder imageHKDF (6 files, 2 directories)
Files folder imageKeyRotation (6 files, 5 directories)
Files folder imagePassword (4 files, 4 directories)
Files folder imageReversible (4 files, 5 directories)

  Files folder image Files (99)  /  src  /  Bootstrap  
File Role Description
  Plain text file CryptoModuleBindingsExample.php Class Class source

  Files folder image Files (99)  /  src  /  Contract  
File Role Description
  Plain text file CryptoContextProviderInterface.php Class Class source

  Files folder image Files (99)  /  src  /  DX  
File Role Description
Files folder imagedocs (2 files)
  Plain text file CryptoContextFactory.php Class Class source
  Plain text file CryptoDirectFactory.php Class Class source
  Plain text file CryptoProvider.php Class Class source
  Accessible without login Plain text file README.md Doc. Documentation

  Files folder image Files (99)  /  src  /  DX  /  docs  
File Role Description
  Accessible without login Plain text file ADR-005-Crypto-DX-Layer.md Doc. Documentation
  Accessible without login Plain text file HOW_TO_USE.md Doc. Documentation

  Files folder image Files (99)  /  src  /  HKDF  
File Role Description
Files folder imagedocs (1 file)
Files folder imageExceptions (4 files)
  Plain text file HKDFContext.php Class Class source
  Plain text file HKDFKeyDeriver.php Class Class source
  Plain text file HKDFPolicy.php Class Class source
  Plain text file HKDFService.php Class Class source
  Accessible without login Plain text file HOW_TO_USE.md Doc. Documentation
  Accessible without login Plain text file README.md Doc. Documentation

  Files folder image Files (99)  /  src  /  HKDF  /  docs  
File Role Description
  Accessible without login Plain text file ADR-003-HKDF.md Doc. Documentation

  Files folder image Files (99)  /  src  /  HKDF  /  Exceptions  
File Role Description
  Plain text file HKDFException.php Class Class source
  Plain text file InvalidContextException.php Class Class source
  Plain text file InvalidOutputLengthException.php Class Class source
  Plain text file InvalidRootKeyException.php Class Class source

  Files folder image Files (99)  /  src  /  KeyRotation  
File Role Description
Files folder imagedocs (2 files)
Files folder imageDTO (4 files)
Files folder imageExceptions (5 files)
Files folder imagePolicy (1 file)
Files folder imageProviders (1 file)
  Plain text file CryptoKeyInterface.php Class Class source
  Plain text file KeyProviderInterface.php Class Class source
  Plain text file KeyRotationPolicyInterface.php Class Class source
  Plain text file KeyRotationService.php Class Class source
  Accessible without login Plain text file KeyStatusEnum.php Aux. Configuration script
  Accessible without login Plain text file README.md Doc. Documentation

  Files folder image Files (99)  /  src  /  KeyRotation  /  docs  
File Role Description
  Accessible without login Plain text file ADR-002-Key-Rotation-Architecture.md Doc. Documentation
  Accessible without login Plain text file HOW_TO_USE.md Doc. Documentation

  Files folder image Files (99)  /  src  /  KeyRotation  /  DTO  
File Role Description
  Plain text file CryptoKeyDTO.php Class Class source
  Plain text file KeyRotationDecisionDTO.php Class Class source
  Plain text file KeyRotationStateDTO.php Class Class source
  Plain text file KeyRotationValidationResultDTO.php Class Class source

  Files folder image Files (99)  /  src  /  KeyRotation  /  Exceptions  
File Role Description
  Plain text file DecryptionKeyNotAllowedException.php Class Class source
  Plain text file KeyNotFoundException.php Class Class source
  Plain text file KeyRotationException.php Class Class source
  Plain text file MultipleActiveKeysException.php Class Class source
  Plain text file NoActiveKeyException.php Class Class source

  Files folder image Files (99)  /  src  /  KeyRotation  /  Policy  
File Role Description
  Plain text file StrictSingleActiveKeyPolicy.php Class Class source

  Files folder image Files (99)  /  src  /  KeyRotation  /  Providers  
File Role Description
  Plain text file InMemoryKeyProvider.php Class Class source

  Files folder image Files (99)  /  src  /  Password  
File Role Description
Files folder imagedocs (1 file)
Files folder imageDTO (1 file)
Files folder imageException (4 files)
Files folder imagePepper (1 file)
  Accessible without login Plain text file HOW_TO_USE.md Doc. Documentation
  Plain text file PasswordHasher.php Class Class source
  Plain text file PasswordHasherInterface.php Class Class source
  Accessible without login Plain text file README.md Doc. Documentation

  Files folder image Files (99)  /  src  /  Password  /  docs  
File Role Description
  Accessible without login Plain text file ADR-004-Password-Hashing-Architecture.md Doc. Documentation

  Files folder image Files (99)  /  src  /  Password  /  DTO  
File Role Description
  Plain text file ArgonPolicyDTO.php Class Class source

  Files folder image Files (99)  /  src  /  Password  /  Exception  
File Role Description
  Plain text file HashingFailedException.php Class Class source
  Plain text file InvalidArgonPolicyException.php Class Class source
  Plain text file PasswordCryptoException.php Class Class source
  Plain text file PepperUnavailableException.php Class Class source

  Files folder image Files (99)  /  src  /  Password  /  Pepper  
File Role Description
  Plain text file PasswordPepperProviderInterface.php Class Class source

  Files folder image Files (99)  /  src  /  Reversible  
File Role Description
Files folder imageAlgorithms (1 file)
Files folder imagedocs (2 files)
Files folder imageDTO (2 files)
Files folder imageExceptions (3 files)
Files folder imageRegistry (1 file)
  Accessible without login Plain text file README.md Doc. Documentation
  Accessible without login Plain text file ReversibleCryptoAlgorithmEnum.php Aux. Configuration script
  Plain text file ReversibleCryptoAlgorithmInterface.php Class Class source
  Plain text file ReversibleCryptoService.php Class Class source

  Files folder image Files (99)  /  src  /  Reversible  /  Algorithms  
File Role Description
  Plain text file Aes256GcmAlgorithm.php Class Class source

  Files folder image Files (99)  /  src  /  Reversible  /  docs  
File Role Description
  Accessible without login Plain text file ADR-001-Reversible-Crypto-Design.md Doc. Documentation
  Accessible without login Plain text file HOW_TO_USE.md Doc. Documentation

  Files folder image Files (99)  /  src  /  Reversible  /  DTO  
File Role Description
  Plain text file ReversibleCryptoEncryptionResultDTO.php Class Class source
  Plain text file ReversibleCryptoMetadataDTO.php Class Class source

  Files folder image Files (99)  /  src  /  Reversible  /  Exceptions  
File Role Description
  Plain text file CryptoAlgorithmNotSupportedException.php Class Class source
  Plain text file CryptoDecryptionFailedException.php Class Class source
  Plain text file CryptoKeyNotFoundException.php Class Class source

  Files folder image Files (99)  /  src  /  Reversible  /  Registry  
File Role Description
  Plain text file ReversibleCryptoAlgorithmRegistry.php Class Class source

  Files folder image Files (99)  /  tests  
File Role Description
Files folder imageDX (3 files)
Files folder imageHKDF (4 files)
Files folder imageIntegration (3 files)
Files folder imageKeyRotation (3 files)
Files folder imagePassword (2 files)
Files folder imageReversible (3 files)

  Files folder image Files (99)  /  tests  /  DX  
File Role Description
  Plain text file CryptoContextFactoryTest.php Class Class source
  Plain text file CryptoDirectFactoryTest.php Class Class source
  Plain text file CryptoProviderTest.php Class Class source

  Files folder image Files (99)  /  tests  /  HKDF  
File Role Description
  Plain text file HKDFContextTest.php Class Class source
  Plain text file HKDFKeyDeriverTest.php Class Class source
  Plain text file HKDFPolicyTest.php Class Class source
  Plain text file HKDFServiceTest.php Class Class source

  Files folder image Files (99)  /  tests  /  Integration  
File Role Description
  Plain text file ContextEncryptionPipelineTest.php Class Class source
  Plain text file DirectEncryptionPipelineTest.php Class Class source
  Plain text file PasswordPipelineTest.php Class Class source

  Files folder image Files (99)  /  tests  /  KeyRotation  
File Role Description
  Plain text file InMemoryKeyProviderTest.php Class Class source
  Plain text file KeyRotationServiceTest.php Class Class source
  Plain text file StrictSingleActiveKeyPolicyTest.php Class Class source

  Files folder image Files (99)  /  tests  /  Password  
File Role Description
  Plain text file ArgonPolicyDTOTest.php Class Class source
  Plain text file PasswordHasherTest.php Class Class source

  Files folder image Files (99)  /  tests  /  Reversible  
File Role Description
  Plain text file Aes256GcmAlgorithmTest.php Class Class source
  Plain text file ReversibleCryptoAl...thmRegistryTest.php Class Class source
  Plain text file ReversibleCryptoServiceTest.php Class Class source

The PHP Classes site has supported package installation using the Composer tool since 2013, as you may verify by reading this instructions page.
Install with Composer Install with Composer
 Version Control Unique User Downloads  
 100%
Total:0
This week:0