taphra / crypt
Authenticated encryption for PHP: XSalsa20-Poly1305 via libsodium. Zero dependencies.
Requires
- php: ^8.2
- ext-sodium: *
Requires (Dev)
None
Suggests
None
Provides
None
Conflicts
None
Replaces
None
README
Documentation: https://taphra.dev/docs/packages/crypt/. This package is a read-only copy of a folder in taphra/taphra; send issues and pull requests there.
Authenticated encryption for PHP with libsodium secretbox (XSalsa20-Poly1305). One class, no Composer dependencies; it needs only the sodium extension, which PHP bundles.
Requirements
- PHP 8.2+
ext-sodium
Installation
composer require taphra/crypt
Encrypting with a secret key
Use encrypt() and decrypt() with a random key of at least 32 bytes, kept out of your code (for example in an environment variable). Generate one with:
php -r 'echo bin2hex(random_bytes(32)), "\n";'
Inside a Taphra app, php taphra key:generate writes such a key to .env as APP_SECRET_KEY.
use Taphra\Crypt; $key = getenv('APP_SECRET_KEY'); // 64 hex characters $token = Crypt::encrypt('sensitive data', $key); // base64 text, different on every call $plain = Crypt::decrypt($token, $key); if ($plain === false) { // wrong key, truncated or tampered data }
Crypt::encrypt(string $plaintext, string $key): string
Returns base64 text holding a format marker, a random 16-byte salt, a random 24-byte nonce and the ciphertext with its Poly1305 tag. The salt derives a fresh key for each message from yours (keyed BLAKE2b). The output is 60 bytes longer than the plaintext before base64 encoding.
A key shorter than 32 bytes, including an empty one, throws an InvalidArgumentException. The length check cannot tell a random key from a long guessable one: use random bytes.
Crypt::decrypt(string $ciphertext, string $key): string|false
Returns the plaintext, or false when the key is wrong or the data was truncated or changed. It never returns partly decrypted data.
decrypt() also reads ciphertexts written by earlier versions of this package, which used an unsalted BLAKE2b hash of the key and accepted keys of any length. Decrypt such data with its old key and encrypt it again with a strong one:
$plain = Crypt::decrypt($oldToken, $oldShortKey); $token = Crypt::encrypt($plain, $newKey);
Encrypting with a passphrase
When the secret is something a person types, use the passphrase methods. They derive the key with Argon2id (sodium_crypto_pwhash) and a random salt stored in the output, which makes guessing the passphrase slow.
$token = Crypt::encryptWithPassphrase('notes', 'correct horse battery staple'); $plain = Crypt::decryptWithPassphrase($token, 'correct horse battery staple'); // "notes" Crypt::decryptWithPassphrase($token, 'wrong guess'); // false
Each call uses about 64 MB of memory and takes up to about a tenth of a second, so use them for a few values, not for bulk data. An empty passphrase throws an InvalidArgumentException. decrypt() and decryptWithPassphrase() never accept each other's output.
Security properties
| Property | Detail |
|---|---|
| Cipher | XSalsa20-Poly1305 (sodium_crypto_secretbox) |
| Key from a secret key | keyed BLAKE2b over a random 16-byte salt per message; key at least 32 bytes |
| Key from a passphrase | Argon2id, libsodium "interactive" cost, random 16-byte salt per message |
| Nonce | 24 random bytes per message |
| Integrity | Poly1305 tag: changed data returns false |
| Memory | derived keys are wiped with sodium_memzero() after use |
Package layout
packages/crypt/
composer.json
src/
Crypt.php # Taphra\Crypt, static methods only