paragonie/halite
High-level, easy-to-use wrapper around libsodium for secure encryption, decryption, and key management in PHP. Provides modern cryptography primitives with safer APIs, supporting authenticated encryption, password hashing, and secure key storage for applications.
Installation
composer require paragonie/halite
Halite is a drop-in replacement for PHP’s openssl_* functions, so no additional configuration is needed.
First Use Case: Encrypting a Message
use ParagonIE\Halite\Halite;
// Generate a new key pair (or load existing)
$keyPair = Halite::generateKeyPair();
// Encrypt a message
$encrypted = Halite::encrypt($keyPair->publicKey, 'My secret message');
// Decrypt the message
$decrypted = Halite::decrypt($keyPair->privateKey, $encrypted);
Where to Look First
Halite::encrypt(), Halite::decrypt(), and Halite::generateKeyPair() are the core methods.Halite::encrypt($publicKey, $data) and Halite::decrypt($privateKey, $encryptedData).Key Management
Halite::generateKeyPair() for new keys or Halite::importKeyPair() for existing keys.$keyPair = Halite::importKeyPair(
'private_key_base64',
'public_key_base64'
);
Symmetric Encryption (Shared Secrets)
Halite::encrypt($sharedKey, $data) and Halite::decrypt($sharedKey, $encryptedData).Asymmetric Encryption (Public/Private Keys)
$encrypted = Halite::encrypt($recipientPublicKey, 'Confidential data');
$decrypted = Halite::decrypt($recipientPrivateKey, $encrypted);
Signing and Verification
$signature = Halite::sign($privateKey, 'Data to sign');
$isValid = Halite::verify($publicKey, $signature, 'Data to sign');
Integration with Laravel
.env or a secure vault (e.g., AWS KMS, HashiCorp Vault).config() to centralize key paths:
config(['halite.private_key' => env('HALITE_PRIVATE_KEY')]);
class CryptoService {
public function encrypt(string $data): string {
return Halite::encrypt(
config('halite.public_key'),
$data
);
}
}
Batch Operations
$encryptedBatch = Halite::encryptBatch($publicKey, ['msg1', 'msg2']);
$decryptedBatch = Halite::decryptBatch($privateKey, $encryptedBatch);
Key Exposure
.gitignore).Key Rotation
Data Size Limits
Time Synchronization for Signatures
Base64 vs. Raw Keys
Invalid Key Errors
Halite::isValidKeyPair() or Halite::isValidPublicKey().Decryption Failures
Halite::decrypt($wrongKey, $data) will fail silently).Performance
Use Halite::generateNewKey() for Ephemeral Keys
$ephemeralKey = Halite::generateNewKey();
Combine with Laravel’s Encryption
encrypt() for lower-risk data.Custom Key Storage
class KeyRepository {
public function getPrivateKey(): string {
return cache()->remember('private_key', 3600, fn() => env('HALITE_PRIVATE_KEY'));
}
}
Error Handling
Halite\Exception\CryptoException):
try {
$decrypted = Halite::decrypt($key, $data);
} catch (Exception $e) {
Log::error("Decryption failed: " . $e->getMessage());
throw new \RuntimeException("Failed to decrypt data.");
}
Testing
Halite::generateKeyPair() in tests to avoid hardcoding keys:
public function testEncryption() {
$keyPair = Halite::generateKeyPair();
$encrypted = Halite::encrypt($keyPair->publicKey, 'test');
$this->assertEquals('test', Halite::decrypt($keyPair->privateKey, $encrypted));
}
Libsodium Compatibility
sudo apt-get install libsodium-dev on Ubuntu). Halite will throw an error if missing.How can I help you explore Laravel packages today?