paragonie/halite
Halite is a high-level PHP cryptography library from Paragon Initiative Enterprises, built on libsodium. It provides safer, opinionated APIs for encryption, authentication, key management, and password hashing, helping you use modern crypto correctly with minimal foot-guns.
Strengths (Updated/Reinforced):
final modifiers on private methods (PR #204) allows for deeper Laravel-specific subclassing (e.g., custom Halite wrappers for audit logging or key rotation hooks).Fit for Laravel (Reaffirmed):
deriveKey() to integrate with config/encryption.php).Halite::encrypt()).Potential Misalignment (Unchanged):
Laravel Ecosystem Synergy (Updated):
// Example PHPStan rule for Halite keys
rules:
- methodcall:
method: ParagonIE\Halite\Halite::encrypt
args:
- type: string
- type: string
message: "Halite keys must be base64-encoded."
HaliteEncryptsAttributes trait).Example Integration (Updated):
// Laravel-specific Halite wrapper with static analysis support
class LaravelHalite extends Halite {
public function encryptAttribute(string $value, string $key): string {
return parent::encrypt($value, $this->normalizeKey($key));
}
private function normalizeKey(string $key): string {
// Custom logic to fetch from Laravel's config/encryption.php
return base64_encode($key);
}
}
Static Analysis Overhead:
phpstan.neon:
includes:
- vendor/paragonie/halite/extension.neon
Backward Compatibility (Reaffirmed):
seal()/open()).Performance (Unchanged):
config/encryption.php keys be normalized for Halite (e.g., base64 encoding)?pest.php)?final modifiers enable custom key rotation logic in Laravel?PHP/Laravel Alignment:
Halite::encrypt()).Halite in unit tests).ext-sodium (unchanged).composer.json for static analysis:
"require-dev": {
"phpstan/phpstan": "^1.10",
"vimeo/psalm": "^6.0"
}
Tooling Compatibility (Updated):
# .github/workflows/laravel.yml
- name: Static Analysis
run: |
vendor/bin/phpstan analyse --level=5 app/
vendor/bin/psalm --init
phpstan.neon for Halite).Assessment Phase (Reinforced):
vendor/bin/phpstan analyse --level=5 app/Http/Controllers/ --focus-on-tests
Incremental Rollout (Updated):
Crypt::encrypt() with Halite::encrypt() and validate with PHPStan.LaravelHalite wrapper (as shown above) and test with Psalm.// tests/Feature/HaliteTest.php
use ParagonIE\Halite\Tests\HaliteTest as BaseHaliteTest;
class HaliteTest extends BaseHaliteTest {
protected function setUp(): void {
$this->halite = new LaravelHalite();
}
}
Backward Compatibility (Reinforced):
EncryptsAttributes:
trait HaliteEncryptsAttributes {
public function getAttribute($key) {
$value = parent::getAttribute($key);
return LaravelHalite::instance()->unseal($value) ?? $value;
}
}
Laravel-Specific Considerations:
# phpstan.neon
ignoreErrors:
- "Method ParagonIE\Halite\Halite::privateMethod() is not public."
// Example test for LaravelHalite
public function testLaravelHaliteEncryption() {
$halite = new LaravelHalite();
$this->assertTrue(HaliteTest::testEncryptDecrypt($halite));
}
KeyFactory for Laravel’s config/encryption.php:
class LaravelKeyFactory extends KeyFactory {
public function getMasterKey(): string {
return base64_encode(config('encryption.key'));
}
}
Third-Party Packages (Unchanged):
Prerequisites (Updated):
composer require --dev phpstan/phpstan vimeo/psalm
vendor/bin/phpstan analyse --init
LaravelHalite to config/app.php services:
'halite' => LaravelHalite::class,
Core Integration (Updated):
LaravelHalite with static analysis support:
class LaravelHalite extends Halite {
public function __construct() {
parent::__construct(
$this->app['config']['halite.key']
);
}
}
HaliteServiceProvider with PHPStan-validated methods:
public function register() {
$this->app->singleton(Halite::class, function () {
return new LaravelHalite();
});
}
Feature Rollout (Updated):
Crypt::encrypt() and validate with PHPStan.HaliteEncryptsAttributes trait and test with Psalm.How can I help you explore Laravel packages today?