paragonie/sodium_compat
Pure PHP polyfill for libsodium (Sodium crypto), transparently using the PHP extension when installed. Supports PHP 5.2.4+ and 32-bit integers in v1.x, with v2.x targeting PHP 8.1+.
Architecture fit
paragonie/sodium_compat is a pure-PHP polyfill for ext/sodium, not a framework-integrated library. Its design is transparent dispatch: if the native extension is loaded, every sodium_* call defers to it; if not, the library supplies a constant-time PHP implementation. It also exposes ParagonIE_Sodium_Compat::* for environments where the global functions aren't available.ext/sodium has shipped as a core extension since PHP 7.2. So the polyfill's architectural role in a modern Laravel app is fallback insurance, not capability. The realistic value is: (a) unblocking packages that hard-require the sodium_* function surface in environments where the extension has been stripped (slim base images, some shared hosting, hardened php.ini), and (b) giving our own crypto service layer a single API that works across dev containers and production.Illuminate\Encryption\Encrypter remains OpenSSL-based (AES-256-CBC/GCM); Hash:: remains bcrypt/argon2. Sodium Compat is relevant to targeted use cases we'd build ourselves: detached signatures for webhook/service-to-service auth, crypto_secretbox/crypto_box_seal for envelope-encrypted columns, crypto_generichash for keyed digests, crypto_kdf/crypto_kx for key derivation and session key exchange.Integration feasibility
composer require paragonie/sodium_compat, no service provider, no config, no migrations. The package registers an autoloader that conditionally defines the sodium_* functions only when missing.">= 1"; applications should pin the specific version that matters to them. We are an application, so pin.Technical risk
ext-sodium instead).">= 1" will resolve to v2.x on our PHP 8.1+ hosts. If anything in the tree assumed 32-bit support or caps at <2, resolution or runtime behavior changes.ParagonIE_Sodium_Compat::$fastMult is a process-global that toggles constant-time multiplication for speed. In a long-lived Laravel worker (Octane), any code that flips this affects all subsequent requests in that process. The README is explicit that this trades speed for security on processors without constant-time multiply. This should be banned by static analysis.composer why paragonie/sodium_compat against our real composer.lock before treating it as a leaf dependency.Key questions
ext-sodium compiled and enabled in every environment (local, CI, staging, prod FPM, queue workers, Octane workers, cron containers)? Verify with php -m | grep sodium and composer check-platform-reqs, not assumptions.composer why, composer depends, composer show paragonie/sodium_compat)ext-sodium to require."Stack fit
ext-sodium is a core extension, so a healthy deployment already has the native implementation. The polyfill degrades to a near no-op shim. This is the correct and preferred posture: the package provides compatibility headroom; the extension provides the performance.App\Contracts\CryptoSigner / App\Support\SodiumGateway — rather than calling sodium_* or ParagonIE_Sodium_Compat::* inline in controllers or models. This makes the polyfill an implementation detail we can later remove.Crypt facade or Hash facade. Keep it as a separate, explicitly-named service for signing and sealed-box operations.Migration path
composer why paragonie/sodium_compat and composer show to establish whether it's already present transitively and at what version. Confirm with composer.lock and an SBOM pass.extension_loaded('sodium') is true. This is the cheapest, highest-value control in the whole plan."ext-sodium": "*" to composer.json and pin base images / php.ini accordingly), letting sodium_compat remain a transitive-only compatibility artifact, or (b) accept the polyfill path for specific low-frequency, non-request-path jobs, gated by a runtime check.Compatibility
< 2. If a transitive package caps at v1, stay on a pinned v1.x release rather than fighting resolution — v1.x is the actively maintained branch for broad PHP support.">= 1"; for our application, pin a concrete version.libsodium extension path entirely — it is superseded by core ext/sodium for PHP 7.2+.Sequencing
extension_loaded('sodium'), composer check-platform-reqs in the pipeline, lint rule banning $fastMult assignment, base-image pinning.Maintenance
.phar with a published GPG key — good supply-chain hygiene.Support
Scaling
ext-sodium loaded: negligible overhead, C-speed primitives, no scaling concern.ParagonIE_Sodium_Compat::polyfill_is_fast() as a runtime branch and defer crypto work to queues when it returns false, exactly as the README recommends.Failure modes
$fastMult set anywhere affects the whole worker process, including Octane's long-lived state. Treat as a security-relevant lint violation.">= 1" silently pulling a major we didn't test against..env and no rotation path. Pair adoption with an explicit key-handling decision.ext-sodium (libsodium itself is audited) and treating the polyfill purely as a dev/fallback path.Ramp-up
sodium_ → ParagonIE_Sodium_Compat::), and the README points to the Libsodium Quick Reference and Using Libsodium in PHP Projects. A competent PHP developer is productive in roughly a day.crypto_sign_detached or crypto_auth?" and "where does this key live?"How can I help you explore Laravel packages today?