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/ */
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,
];
}
}
// 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);
}
}