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

Pcrypt Laravel Package

opichon/pcrypt

pcrypt is a small PHP/Laravel package that helps you encrypt and decrypt values using password-based cryptography. Useful for securely storing sensitive strings, generating encrypted payloads, and adding an extra layer of protection beyond plain hashing.

View on GitHub
Deep Wiki
Context7

Getting Started

Minimal Setup

  1. Installation

    composer require opichon/pcrypt
    

    No additional configuration is required—pcrypt is a lightweight, dependency-free library.

  2. Basic Usage Encrypt a string:

    use Opichon\Pcrypt\Facades\Pcrypt;
    
    $encrypted = Pcrypt::encrypt('sensitive-data');
    

    Decrypt a string:

    $decrypted = Pcrypt::decrypt($encrypted);
    
  3. First Use Case Securely store API keys or user tokens in a database:

    // Store
    $user->api_token = Pcrypt::encrypt($rawToken);
    $user->save();
    
    // Retrieve
    $rawToken = Pcrypt::decrypt($user->api_token);
    

Implementation Patterns

Common Workflows

  1. Database Storage Encrypt sensitive fields before saving to a database:

    $user = new User();
    $user->password = Pcrypt::encrypt($request->password);
    $user->save();
    
  2. API Requests/Responses Encrypt payloads before sending to untrusted services:

    $encryptedPayload = Pcrypt::encrypt(json_encode($data));
    // Send $encryptedPayload via HTTP
    
  3. Configuration Files Store secrets in config/app.php:

    'services' => [
        'stripe' => [
            'key' => Pcrypt::decrypt(env('STRIPE_KEY_ENCRYPTED')),
        ],
    ],
    

Integration Tips

  • Laravel Events/Listeners Automatically encrypt/decrypt data in observers:

    public function saving(User $user)
    {
        $user->password = Pcrypt::encrypt($user->password);
    }
    
  • Form Request Validation Decrypt input before validation:

    public function rules()
    {
        return [
            'encrypted_token' => 'required|string',
        ];
    }
    
    public function prepareForValidation()
    {
        $this->merge([
            'token' => Pcrypt::decrypt($this->encrypted_token),
        ]);
    }
    
  • API Middleware Decrypt auth tokens in middleware:

    public function handle($request, Closure $next)
    {
        $request->merge([
            'token' => Pcrypt::decrypt($request->bearerToken()),
        ]);
        return $next($request);
    }
    

Gotchas and Tips

Pitfalls

  1. Key Management

    • No built-in key rotation: Manually update keys if compromised.
    • Store keys securely: Use Laravel’s env() or a secrets manager (e.g., AWS Secrets Manager).
  2. Performance

    • Avoid encrypting large payloads (e.g., entire database dumps). Use for small strings (API keys, tokens, passwords).
  3. Error Handling

    • Decryption failures throw exceptions. Wrap calls in try-catch:
      try {
          $data = Pcrypt::decrypt($encrypted);
      } catch (\Opichon\Pcrypt\Exceptions\DecryptException $e) {
          // Log or handle gracefully
      }
      
  4. Compatibility

    • No IV/Nonce handling: If using in multi-threaded environments, ensure unique salts per encryption.

Debugging Tips

  • Verify Encryption/Decryption Compare original and decrypted data:

    $original = 'test';
    $encrypted = Pcrypt::encrypt($original);
    $decrypted = Pcrypt::decrypt($encrypted);
    assert($original === $decrypted);
    
  • Check Key Validity If decryption fails, verify the key matches the encryption key:

    Pcrypt::setKey('correct-key-here');
    

Extension Points

  1. Custom Algorithms Extend the library by implementing Opichon\Pcrypt\Contracts\Encryptor:

    class CustomEncryptor implements Encryptor {
        public function encrypt($data, $key) { ... }
        public function decrypt($data, $key) { ... }
    }
    

    Register via service provider:

    Pcrypt::extend('custom', function () {
        return new CustomEncryptor();
    });
    
  2. Base64 vs. Hex Output Configure output format:

    Pcrypt::setOutputFormat(\Opichon\Pcrypt\Contracts\Pcrypt::BASE64);
    // or
    Pcrypt::setOutputFormat(\Opichon\Pcrypt\Contracts\Pcrypt::HEX);
    
  3. Logging Log encryption/decryption events for auditing:

    Pcrypt::onEncrypting(function ($data, $key) {
        \Log::debug('Encrypted data', ['data' => $data, 'key' => $key]);
    });
    
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.
codifyo/ts-generator-bundle
andydefer/laravel-cluster
testo/fiber
mintobit/jobqueue
a4sex/maintenance-bundle
a4sex/entity-date-update
a4sex/client-identifier
a4sex/base-utilites
a4sex/key-value-storage
a4sex/micro-status
chilldev/dependency-injection-extra
datinglibre/datinglibre-app-api
biberltd/corebundle
bricre/symfony-bundle-test
biberltd/logbundle
dominium/http-adapter-bundle
dominium/google-analytics
a4sex/auto-clean-entity
christhompsontldr/laravel-inky
spatie/mailcoach-vapor