PHP code example of mrfrkayvaz / privata

1. Go to this page and download the library: Download mrfrkayvaz/privata 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/ */

    

mrfrkayvaz / privata example snippets


'providers' => [
    // ...
    Privata\PrivataServiceProvider::class,
],

'aliases' => [
    // ...
    'Privata' => Privata\Facades\Privata::class,
],

use Privata\Facades\Privata;

// Encrypt data
$encrypted = Privata::encrypt('sensitive data');

// Decrypt data
$decrypted = Privata::decrypt($encrypted);



namespace App\Models;

use Illuminate\Database\Eloquent\Model;
use Privata\Traits\Encryptable;
use Privata\Masks\EmailMask;
use Privata\Masks\PhoneMask;

class User extends Model
{
    use Encryptable;

    protected $fillable = ['name', 'email', 'phone'];

    protected function encrypted(): array
    {
        return ['email', 'phone'];
    }

    protected function encryptionMasks(): array
    {
        return [
            'email' => EmailMask::class,
            'phone' => PhoneMask::class,
        ];
    }
}

Schema::create('users', function (Blueprint $table) {
    $table->id();
    $table->string('name');
    
    // Encrypted fields
    $table->text('email_encrypted')->nullable();
    $table->timestamp('email_encrypted_at')->nullable();
    
    $table->text('phone_encrypted')->nullable();
    $table->timestamp('phone_encrypted_at')->nullable();
    
    $table->timestamps();
});

// Create a user with encrypted data
$user = User::create([
    'name' => 'John Doe',
    'email' => '[email protected]',
    'phone' => '+1234567890'
]);

// The email and phone are automatically encrypted and stored
// Access the decrypted values
echo $user->email; // [email protected]
echo $user->phone; // +1234567890

// Access masked values
echo $user->email_masked; // j*******@e*******.com
echo $user->phone_masked; // +1*******90

class User extends Model
{
    use Encryptable;

    protected function canDecrypt(): bool
    {
        // Only decrypt for authenticated users
        return auth()->check();
    }
}

use Privata\Masks\EmailMask;

$masked = EmailMask::mask('[email protected]');
// Result: j*******@e*******.com

use Privata\Masks\PhoneMask;

$masked = PhoneMask::mask('+1234567890');
// Result: +1*******90

use Privata\Masks\StringMask;

$masked = StringMask::mask('sensitive data');
// Result: se**********



namespace App\Masks;

use Privata\Contracts\Mask;

class CustomMask implements Mask
{
    public static function mask(string $data, string $maskingCharacter = '*'): string
    {
        // Your custom masking logic
        return substr($data, 0, 2) . str_repeat($maskingCharacter, strlen($data) - 2);
    }
}

// config/privata.php
'encryption' => [
    'driver' => 'aes',
    'masking_character' => '*',
],

'drivers' => [
    'aes' => [
        'key' => env('PRIVATA_ENCRYPTION_KEY'),
        'cipher' => env('PRIVATA_ENCRYPTION_CIPHER', 'aes-256-cbc'),
    ]
],

'database' => [
    'encrypted_data_suffix' => '_encrypted',
    'encrypted_timestamp_suffix' => '_encrypted_at',
    'encrypted_masked_suffix' => '_masked',
    'add_masked_value' => true,
],
bash
php artisan vendor:publish --provider="Privata\PrivataServiceProvider" --tag="privata-config"
bash
php artisan tinker
>>> base64_encode(random_bytes(32))