zendframework/zend-crypt
Zend\Crypt provides secure PHP cryptography utilities including encryption/decryption, hashing, HMAC, key generation, and password adapters. Designed for Zend Framework apps but usable standalone, with pluggable algorithms and safer defaults for common crypto tasks.
Start by installing the package via Composer (though note it’s archived and unmaintained—see Gotchas). For new projects, consider migrating to paragonie/whirlpool, defuse/php-encryption, or Laravel’s native Hash/Crypt facades. If constrained to use zend-crypt (e.g., legacy code), install with:
composer require zendframework/zend-crypt
First use case: securely hashing passwords using Bcrypt:
use Zend\Crypt\Password\Bcrypt;
$bcrypt = new Bcrypt(['cost' => 12]);
$hashed = $bcrypt->create('user_password');
$valid = $bcrypt->verify('user_password', $hashed);
Check src/Password/ and src/Symmetric/ in the repo for core classes—start with PasswordFactory or BlockCipher for basic workflows.
Zend\Crypt\Password\Bcrypt (or Scrypt/Pbkdf2 if required). Avoid rolling custom hashers—validate against stored hashes via verify() with constant-time comparison.BlockCipher with modern algorithms (e.g., AES-256-CBC). Generate keys with Zend\Crypt\Key\Derivation\PBKDF2 (never hardcode secrets):
use Zend\Crypt\BlockCipher;
use Zend\Crypt\Key\Derivation\PBKDF2;
$cipher = BlockCipher::factory('openssl', ['algo' => 'aes-256-cbc']);
$key = PBKDF2::derive('master_secret', 'salt123', 100000, 32);
$cipher->setKey($key);
$encrypted = $cipher->encrypt(['data' => 'sensitive_value']);
$decrypted = $cipher->decrypt($encrypted);
Zend\Crypt\Random for tokens (e.g., CSRF):
$token = bin2hex(Random::getString(32)); // or ->getRandomBytes(32)
For integrity:
use Zend\Crypt\Hash;
$hash = Hash::compute('sha256', 'secret_key', $message);
Integrate into Laravel via a custom Encrypter wrapper or store sensitive config values encrypted in .env (derive key per-environment).
⚠️ Critical: This package is archived (since 2019) and unmaintained. Prefer Laravel’s built-in Crypt (which uses OpenSSL directly) or modern standalone libraries like defuse/php-encryption. Using zend-crypt in new code introduces unpatched security risks.
BlockCipher defaults to openssl and mcrypt—but mcrypt is removed in PHP 7.2+. Ensure openssl is used explicitly.BlockCipher manages IVs automatically (appends to ciphertext), but ensure storage/transmission preserves the full payload. Never reuse keys across environments without re-derivation.verify()—not ===—to prevent timing attacks.Zend\Crypt\Password\PasswordAdapterFactory requires strict config format (['algorithm' => 'bcrypt', 'cost' => 13]). Misformatted configs silently fallback or throw.Zend\Crypt\Password\PasswordInterface for custom schemes (e.g., Argon2 via password_hash() wrapper), but validate thoroughly.zend-crypt’s internal exception logging by wrapping operations in try/catch—errors like invalid IVs or bad keys throw Exception\RuntimeException.How can I help you explore Laravel packages today?