Install the package via Composer:
composer require novay/kunci
Publish the configuration file (if needed) to customize encryption settings:
php artisan vendor:publish --provider="Novay\Kunci\KunciServiceProvider"
For basic encryption/decryption, use the Kunci facade:
use Novay\Kunci\Facades\Kunci;
// Encrypt
$encrypted = Kunci::encrypt('sensitive-data');
// Decrypt
$decrypted = Kunci::decrypt($encrypted);
First Use Case: Securely encrypting API tokens or user-sensitive data in a database column.
Use deterministic encryption when the same plaintext must always produce the same ciphertext (e.g., indexing, sorting, or comparing encrypted values):
$deterministic = Kunci::deterministic()->encrypt('searchable-data');
Workflow:
email or username deterministically to enable LIKE queries or sorting.if ($encryptedA === $encryptedB)).For unique ciphertexts per encryption (e.g., passwords, tokens):
$randomized = Kunci::encrypt('unique-data');
Schema::table('users', function (Blueprint $table) {
$table->string('encrypted_email')->unique();
});
public function scopeSearchByEmail($query, $email)
{
return $query->where('encrypted_email', Kunci::deterministic()->encrypt($email));
}
protected $casts = [
'encrypted_email' => EncryptedAttribute::make('encrypted_email'),
];
Security Implications:
Key Rotation:
Performance:
deterministic_key in config/kunci.php if using custom keys for deterministic mode.'deterministic' => [
'key' => env('DETERMINISTIC_KEY', 'default-key-here'),
],
Kunci::getAlgorithm() to verify encryption mode (e.g., AES-256-CBC for non-deterministic, AES-256-ECB for deterministic).try {
$decrypted = Kunci::decrypt($encrypted);
} catch (\Novay\Kunci\Exceptions\DecryptException $e) {
Log::error("Decryption failed: " . $e->getMessage());
}
$this->app->bind(\Novay\Kunci\Contracts\Encryptor::class, function () {
return new CustomEncryptor();
});
How can I help you explore Laravel packages today?