PHP code example of sneakyx / laravel-dynamic-encryption
1. Go to this page and download the library: Download sneakyx/laravel-dynamic-encryption library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
sneakyx / laravel-dynamic-encryption example snippets
use Illuminate\Support\Facades\Crypt;
$payload = Crypt::encryptString('secret');
$plain = Crypt::decryptString($payload);
use Sneakyx\LaravelDynamicEncryption\Casts\EncryptedNullableCast;
use Sneakyx\LaravelDynamicEncryption\Casts\EncryptedNullableJsonCast;
class User extends Model
{
protected function casts(): array
{
return [
'secret_field' => EncryptedNullableCast::class,
'secret_array' => EncryptedNullableJsonCast::class,
];
}
}
// OLD (deprecated)
use Sneakyx\LaravelDynamicEncryption\Traits\DynamicEncryptable;
class User extends Model {
use DynamicEncryptable;
protected array $encryptable = ['secret_field'];
}
// NEW
use Sneakyx\LaravelDynamicEncryption\Casts\EncryptedNullableCast;
class User extends Model {
protected function casts(): array {
return ['secret_field' => EncryptedNullableCast::class];
}
}
use Illuminate\Database\Eloquent\Model;
use Sneakyx\LaravelDynamicEncryption\Casts\EncryptedNullableCast;
class UserSecret extends Model
{
protected $casts = [
'iban' => EncryptedNullableCast::class,
];
}
use Sneakyx\LaravelDynamicEncryption\Values\LockedEncryptedValue;
if ($userSecret->iban instanceof LockedEncryptedValue) {
// Show banner/modal: "Please enter password to view"
} else {
echo $userSecret->iban; // Decrypted plaintext
}
namespace Tests;
use Illuminate\Foundation\Testing\TestCase as BaseTestCase;
use Sneakyx\LaravelDynamicEncryption\Testing\DynamicEncryptionTestLoader;
abstract class TestCase extends BaseTestCase
{
use CreatesApplication;
use DynamicEncryptionTestLoader;
protected function setUp(): void
{
parent::setUp();
// Initializes a random key and writes it to the configured cache store
$this->initializeDynamicEncryptionKeyForTests();
}
}