paragonie/sapient
Sapient is a PHP cryptography library from Paragon Initiative Enterprises offering simple, misuse-resistant APIs for authenticated symmetric encryption, public-key encryption, and digital signatures, built on libsodium with versioned ciphertexts and key-management helpers.
What it is. paragonie/sapient is a framework-agnostic PHP "secure API toolkit" from Paragon Initiative Enterprises. It provides message-level cryptography for machine-to-machine traffic: symmetric (shared-key) encryption, asymmetric (X25519) encryption, sealed messages, signatures, and signed+sealed combinations, plus keyring abstractions that embed a key identifier in a protocol header/footer so receivers can select the right key during rotation. It is built on libsodium primitives and is deliberately not JWT/JOSE — it is a sibling concept to PASETO from the same authors.
Architecture fit. Sapient is a library, not a middleware or service provider. There is no Laravel package, facade, config stub, or HTTP transport opinion. That is both the appeal and the cost: it gives you cryptographic building blocks with a well-defined wire format, and leaves you to own the request/response plumbing. For a Laravel estate it fits best as an internal service-layer concern — a thin application wrapper (service class + Guzzle/Http client middleware + a request-guard middleware) rather than something you sprinkle through controllers.
Integration feasibility. High in a technical sense, medium in effort. ext-sodium is bundled with PHP 7.2+ and is present in every modern Laravel runtime, so no exotic system dependency. The public API surface is small (a handful of static crypto operations plus keyring construction), so a competent PHP developer can produce a working proof of concept in a day. The real work is not the library call — it is key custody, key rotation, and deciding what the signed/encrypted envelope actually represents.
Technical risk.
Key questions for the team. Which call paths genuinely need message-level crypto rather than mTLS plus an OAuth2 client-credentials token? Are all counterparties PHP, or is there a mixed-language boundary? Who owns key material (Vault/KMS vs. Laravel config + env)? What is the rotation cadence and the overlap window? Is there an existing JWT/PASETO investment this would duplicate? And: what does the threat model say we are defending against — a compromised TLS terminator, a curious message broker, or an untrusted intermediary?
Stack fit. Sapient slots in cleanly behind Laravel's existing HTTP layer. Recommended shape:
config/sapient.php holding key IDs and key source (env/KMS reference, never raw keys committed).SapientKeyringFactory singleton bound in a service provider, resolving keys from Vault/AWS Secrets Manager or an encrypted storage/ file.HandlerStack middleware (wired into Illuminate\Support\Facades\Http::withOptions or a dedicated client) that signs and/or seals request bodies and attaches the Sapient header and footer.VerifySapientSignature middleware on the specific route group that owns the protocol, returning a protocol-appropriate 4xx on failure — deliberately not global middleware.SecureMessage) so application code never touches the static crypto API directly. This keeps the blast radius of a future swap to PASETO to one class.Migration path. Do not big-bang. Introduce the protocol alongside existing auth at a versioned boundary (/api/v2/* or an X-Message-Protocol header), because Sapient's header/footer design already anticipates version negotiation. Run dual-stack: new endpoints require Sapient, legacy endpoints keep HMAC/JWT, and the middleware accepts both during the transition. Contract tests on the wire format (golden vectors checked into the repo) are essential — they are the only reliable guard against a silent change in what bytes get signed.
Compatibility. PHP 8.1+ (Laravel 10/11/12 baseline) is fine; ext-sodium must be asserted in CI and in every container image, including queue workers and any Lambda/Octane runtime. Octane and long-running workers are safe because the library is stateless, but the keyring must be rebuilt or cached explicitly on boot rather than resolved per-request. No conflict expected with Sanctum/Passport — they solve user auth; Sapient solves service-to-service message integrity. Keep them conceptually separate.
Sequencing.
Maintenance. Low ongoing code maintenance — the library is small, stable, and on a recent release cadence (last release September 2025). The ongoing cost is operational discipline: key rotation ceremonies, keyring inventory, and keeping the protocol contract tests green. Budget roughly one engineer-week per year for rotation and dependency review, plus a named owner.
Support. Community-only, no SLA. Vendor responsiveness is good for security issues but you cannot escalate an incident. Practical mitigation: forked mirror in your own VCS, pinned version, and an internal note that the fallback path is "reimplement the envelope format with libsodium directly" — a few hundred lines, which is why the MIT license matters here.
Scaling. Effectively free. Sodium's symmetric operations are microseconds; asymmetric operations are tens of microseconds and only matter at very high request volumes. The library is stateless and keyring lookups are cheap, so horizontal scaling and Octane are unaffected. The real scaling constraint is key management: as counterparties multiply, the keyring grows and rotation fan-out becomes the bottleneck, not CPU.
Failure modes to plan for.
ext-sodium in a new container or CI image — assert at boot, fail loudly.Ramp-up. The API is learnable in hours, but correct use is not a junior task. Plan for a half-day onboarding for engineers who will only consume the wrapper, and a two-to-three-day deep dive plus senior security review for whoever owns keys, rotation, and the protocol contract. The most common early mistake is treating Sapient as an auth token library rather than a message-integrity envelope — set that expectation explicitly in the design doc.
How can I help you explore Laravel packages today?