paragonie/halite
High-level, easy-to-use wrapper around libsodium for secure encryption, decryption, and key management in PHP. Provides modern cryptography primitives with safer APIs, supporting authenticated encryption, password hashing, and secure key storage for applications.
Strengths:
encrypt(), decrypt(), seal(), open()) while handling edge cases (e.g., nonce generation, padding).KeyFactory).seal()/open() enable hybrid encryption (authenticated + encrypted) out-of-the-box, reducing custom implementation risks.Fit for Laravel:
Attribute casting or Eloquent accessors).KeyFactory).Potential Misalignment:
Hash facade.libsodium PHP extension (enabled by default in PHP 8.1+ but may need manual installation on older setups).Laravel Ecosystem Synergy:
EncryptsAttributes trait or CryptManager (though Halite lacks Laravel’s built-in key storage; would need custom integration with config/encryption.php).encrypted:success, decrypted:failure) via Laravel’s event system for logging/monitoring.Cache::put('sensitive_data', Halite::seal($data))).Example Integration Points:
// Encrypting a model attribute
use ParagonIE\Halite\Halite;
use Illuminate\Database\Eloquent\Model;
class User extends Model {
protected $casts = [
'ssn' => EncryptedAttribute::class, // Custom trait wrapping Halite
];
}
// API request encryption
$encryptedPayload = Halite::seal(json_encode($request->all()));
Database Compatibility:
WHERE clauses via application logic, not database-level encryption).Libsodium Availability:
libsodium may not be pre-installed on shared hosting (e.g., older PHP 7.x stacks).Key Management:
config/encryption.php or vaults (e.g., AWS KMS, HashiCorp Vault).KeyFactory with Laravel’s key storage (e.g., config/halite.php).Performance:
memory_cost/time_cost in KeyFactory.Backward Compatibility:
^1.0 in composer.json and test against PHP 8.1+.libsodium is unavailable (e.g., graceful degradation)?PHP/Laravel Alignment:
libsodium).Halite as a singleton or context-bound instance).ext-sodium extension (enabled by default in PHP 8.1+; may need pecl install on older setups).Tooling Compatibility:
Halite for unit tests).composer require paragonie/halite and libsodium checks to pipeline.Assessment Phase:
Crypt::encrypt(), Hash::make()).Incremental Rollout:
Crypt::encrypt()) with Halite’s encrypt().
// Before
$encrypted = Crypt::encrypt($data);
// After
$encrypted = Halite::encrypt($data, $key);
seal() for authenticated + encrypted API payloads).KeyFactory (e.g., for password hashing or token signing).Backward Compatibility:
config/encryption.php for key storage initially, then migrate to KeyFactory.EncryptsAttributes usage.Laravel-Specific Considerations:
EncryptsAttributes trait using Halite:
trait HaliteEncryptsAttributes {
public function getAttribute($key) {
$value = parent::getAttribute($key);
return Halite::unseal($value, $this->getHaliteKey()) ?? $value;
}
}
Cache::put('user_token', Halite::seal($token), now()->addHours(1));
event(new EncryptionAttempted(
Halite::decrypt($data, $key),
$keyId,
$success
));
Third-Party Packages:
spatie/laravel-activitylog to encrypt sensitive log data.Prerequisites:
libsodium.composer.json:
"require": {
"paragonie/halite": "^1.0"
}
config/halite.php).Core Integration:
HaliteServiceProvider to bind Halite to Laravel’s container:
$this->app->singleton(Halite::class, function () {
return new Halite($this->app['config']['halite.key']);
});
HaliteFacade for cleaner syntax:
use Illuminate\Support\Facades\Facade;
class Halite extends Facade {
protected static function getFacadeAccessor() { return 'halite'; }
}
Feature Rollout:
How can I help you explore Laravel packages today?