paragonie/sodium_compat
Pure-PHP polyfill for PHP’s Sodium (libsodium) cryptography API. Transparently uses the native sodium extension when available; otherwise falls back to a compatible implementation. v1 supports PHP 5.2+ incl. 32-bit; v2 targets PHP 8.1+ only.
composer require paragonie/sodium_compat. No configuration needed—just require_once 'vendor/autoload.php';.sodium_crypto_pwhash() (Argon2id), even on shared hosting where ext/sodium isn’t available:
$passwordHash = sodium_crypto_pwhash(
64, // hash length
'user_password_here',
sodium_randombytes_buf(SODIUM_CRYPTO_PWHASH_SALTBYTES),
SODIUM_CRYPTO_PWHASH_OPSLIMIT_INTERACTIVE,
SODIUM_CRYPTO_PWHASH_MEMLIMIT_INTERACTIVE,
SODIUM_CRYPTO_PWHASH_ALG_ARGON2ID13
);
Sodium\Library::versionString() to confirm the polyfill is active. No native extension required.sodium_*() functions throughout your codebase—code works identically in environments with or without ext/sodium.$key = sodium_crypto_secretbox_keygen();
$nonce = random_bytes(SODIUM_CRYPTO_SECRETBOX_NONCEBYTES);
$ciphertext = sodium_crypto_secretbox($plaintext, $nonce, $key);
$derivedKey = sodium_crypto_pwhash(
32,
$userPassword,
$salt,
SODIUM_CRYPTO_PWHASH_OPSLIMIT_MODERATE,
SODIUM_CRYPTO_PWHASH_MEMLIMIT_MODERATE
);
sodium_crypto_secretbox() for your payload sizes—avoid on high-throughput crypto-heavy endpoints.SODIUM_CRYPTO_* constants are auto-exported—no need to use them. Avoid hardcoding magic numbers (e.g., 32 for key length); prefer SODIUM_CRYPTO_SECRETBOX_KEYBYTES.if (!extension_loaded('sodium')) { sodium_compat_init(); } in test bootstrap to force polyfill consistency.function_exists('sodium_crypto_box_keypair').composer.json— Laravel’s auto-discovery and native bindings will take over seamlessly.random_bytes() (not mt_rand()) for nonces/secrets. The polyfill adds a CSPRNG fallback if unavailable.How can I help you explore Laravel packages today?