Weave Code
Code Weaver
Helps Laravel developers discover, compare, and choose open-source packages. See popularity, security, maintainers, and scores at a glance to make better decisions.
Feedback
Share your thoughts, report bugs, or suggest improvements.
Subject
Message

Laravel Ciphersweet Laravel Package

spatie/laravel-ciphersweet

Laravel wrapper for Paragonie CipherSweet that adds searchable field-level encryption to Eloquent models. Encrypt/decrypt sensitive attributes and generate blind indexes so you can query encrypted data securely without exposing readable values in your database.

View on GitHub
Deep Wiki
Context7

Technical Evaluation

Architecture Fit

  • Strong Fit for GDPR/Compliance-Centric Applications: The package excels in scenarios requiring field-level encryption (e.g., PII like SSNs, emails, or financial data) while enabling searchability via blind indexes. This aligns with GDPR, HIPAA, or PCI-DSS compliance needs where data must be encrypted at rest but queryable.
  • Eloquent Integration: Leverages Laravel’s Eloquent ORM, making it a natural fit for applications already using Eloquent models. The UsesCipherSweet trait and CipherSweetEncrypted interface provide a declarative way to define encrypted fields, reducing boilerplate.
  • Searchable Encryption: The blind index feature (via addBlindIndex) enables exact-match searches on encrypted data, a critical requirement for user authentication (e.g., email/username lookup) or internal tools (e.g., customer support dashboards).
  • Field-Type Support: Supports text, integers, booleans, floats, JSON, and optional fields (NULL-safe), covering most use cases without forcing a one-size-fits-all approach.

Integration Feasibility

  • Minimal Migration Overhead: Requires:
    1. Publishing migrations (ciphersweet-migrations) to add encrypted_rows and blind_indexes tables.
    2. Updating model fields to text (for encrypted storage).
    3. Running ciphersweet:encrypt to backfill existing data (restartable for large datasets).
  • Key Management: Integrates with Laravel’s .env for key storage (default) or supports file-based or custom providers, allowing alignment with existing secrets management (e.g., AWS KMS, HashiCorp Vault).
  • Validation Support: Includes EncryptedUniqueRule for form validation, reducing custom validation logic for encrypted fields.

Technical Risk

  • Performance Impact:
    • Encryption/Decryption Overhead: Field-level encryption adds latency (~10–50ms per operation, depending on backend). Benchmark with production-like data volumes.
    • Blind Index Searches: Exact-match searches are supported, but partial/fuzzy searches require additional setup (e.g., multiple blind indexes or external search tools like Elasticsearch).
    • Backfill Scalability: Encrypting millions of records may require batch processing or queue-based approaches (e.g., Laravel Queues).
  • Key Rotation Complexity:
    • Rotating keys requires re-encrypting all data (ciphersweet:encrypt), which can be resource-intensive. Plan for downtime or staggered rotation in high-availability systems.
  • Backend Dependencies:
    • Defaults to NaCl (sodium) backend, but FIPS-compliant or custom backends may be needed for regulated industries. Test compatibility with your PHP environment (e.g., OpenSSL extensions).
  • Schema Lock-In:
    • Encrypted fields must be text type, which may conflict with existing schema constraints (e.g., varchar limits). Requires migration planning.
    • Blind indexes add additional database tables, increasing schema complexity.

Key Questions for TPM

  1. Compliance Requirements:
    • Are there specific encryption standards (e.g., AES-256-GCM, FIPS 140-2) that must be enforced? If so, does the nacl backend suffice, or is a custom backend needed?
    • Does the application require audit logs for encryption/decryption events? The package does not natively support this.
  2. Performance Trade-offs:
    • What is the acceptable latency for encrypted field access? Test with your expected query patterns (e.g., read-heavy vs. write-heavy).
    • Will blind indexes be used for high-cardinality fields (e.g., emails)? Large indexes may impact database performance.
  3. Key Management:
    • How will encryption keys be rotated in production? Will automated tools (e.g., AWS Secrets Manager) trigger re-encryption, or will it be manual?
    • Is multi-region key replication needed for disaster recovery? The package does not support distributed key storage.
  4. Data Migration:
    • What is the size of the existing dataset? For >1M records, consider a staged migration (e.g., encrypt new records first, then backfill).
    • Are there legacy systems that read raw data? Encryption may break integrations unless they are updated.
  5. Search Requirements:
    • Are partial/fuzzy searches required? If so, will you use multiple blind indexes or an external search layer (e.g., Elasticsearch)?
    • How will case sensitivity or normalization (e.g., trimming whitespace) be handled in searches?
  6. Monitoring and Alerts:
    • How will failed decryption attempts (e.g., corrupted data or key errors) be monitored? The package lacks built-in error tracking.
    • Are there SLA requirements for encrypted field access? Define metrics for latency and failure rates.
  7. Team Expertise:
    • Does the team have experience with field-level encryption or blind indexes? If not, allocate time for training or proof-of-concept testing.
    • Is there DevOps support for managing encryption keys and migrations?

Integration Approach

Stack Fit

  • Laravel Ecosystem: Optimized for Laravel 8+ with Eloquent, making it a zero-friction addition for existing Laravel applications. Works seamlessly with:
    • Laravel Scout (for advanced search, though blind indexes may still be needed for encrypted data).
    • Laravel Nova/Panel (for admin interfaces accessing encrypted data).
    • Laravel Queues (for batch encryption/decryption).
  • Database Compatibility:
    • Supports MySQL, PostgreSQL, SQLite (via CipherSweet’s PHP library). Test with your specific DB version for any quirks (e.g., text field limits).
    • NoSQL: Not supported; this is a relational-database-focused solution.
  • PHP Requirements:
    • Requires PHP 8.0+ (due to CipherSweet’s dependencies). Ensure your environment meets this.
    • Extensions: sodium (for NaCl backend) or openssl (for FIPS) must be enabled.

Migration Path

  1. Assessment Phase:
    • Audit sensitive fields in existing models to identify candidates for encryption.
    • Document search requirements (exact matches vs. partial searches).
    • Plan key management (e.g., .env, AWS KMS, or custom provider).
  2. Development Phase:
    • Step 1: Add Package and Migrations
      composer require spatie/laravel-ciphersweet
      php artisan vendor:publish --tag="ciphersweet-migrations"
      php artisan migrate
      
    • Step 2: Configure Models
      • Implement CipherSweetEncrypted and UsesCipherSweet traits.
      • Define encrypted fields in configureCipherSweet() (e.g., email, ssn).
      • Add blind indexes for searchable fields.
    • Step 3: Generate and Store Key
      php artisan ciphersweet:generate-key
      
      Store the key in .env (CIPHERSWEET_KEY) or a secure vault.
    • Step 4: Backfill Data
      php artisan ciphersweet:encrypt App\User <key>
      
      For large datasets, use queues or batch processing:
      php artisan queue:work --queue=ciphersweet
      
  3. Testing Phase:
    • Unit Tests: Verify encryption/decryption of model attributes.
    • Integration Tests: Test blind index searches and validation rules.
    • Performance Tests: Measure latency for encrypted field access and searches.
    • Key Rotation Test: Simulate key rotation with a subset of data.
  4. Deployment Phase:
    • Staged Rollout: Encrypt new records first, then backfill existing data.
    • Monitoring: Track decryption failures, latency, and search performance.
    • Rollback Plan: Document steps to revert to unencrypted fields if issues arise.

Compatibility

  • Existing Code:
    • No Breaking Changes: Encrypted fields are accessed like normal attributes (e.g., $user->email still works).
    • Validation: Replace Rule::unique() with EncryptedUniqueRule for encrypted fields.
    • Serializers: Ensure JSON/API responses include encrypted fields (they will be decrypted automatically).
  • Third-Party Packages:
    • Laravel Scout: Blind indexes can complement Scout for encrypted search, but Scout’s built-in search may not work on encrypted fields.
    • Laravel Cashier/Billing: If storing PII (e.g., customer IDs), ensure encrypted fields are handled in webhooks/payments.
    • Laravel Medialibrary: If encrypting file metadata, ensure the encrypted fields are included in queries.

**Sequencing

Weaver

How can I help you explore Laravel packages today?

Conversation history is not saved when not logged in.
Prompt
Add packages to context
No packages found.
codraw/framework-extra-bundle
codraw/messenger
codraw/security
codraw/mailer
codraw/contracts
codraw/profiling
codraw/dependency-injection
codraw/tester
codraw/core
nexmo/api-specification
capell-app/block-library
axium/identity
cetria/laravel-dummy-models
cetria/reflection-helper
agropredict/sso-auth-bundle
evolvestudio/spam-protection
datacore/hub-sdk
develia/commons
cuci/prototurk-sdk
cuci/prototurk-sdk-symfony