Protect your data and secure every field in your entities with this tiny encryption library!
This is a open-source library. Please consider a link to this repository when you're actively using it.
This small encryption/decryption library can be required by using composer. Please use the following command:
composer require internetpixels/sodium-encryption
You have to set the secret
and public
tokens once. Those keys are not allowed to change overtime!
<?php
// Update the keys (create a new unique keypair)!
\InternetPixels\SodiumEncryption\EncryptionManager::setKeys(
'1d17336ba7b2cec7dc8ec788e78ebf835d9f85cfc414275e92fd8e3ae5d6d2b6',
'b88fc95850eec82492e9f0616cfeb69b9205735e34f5ce5e83d681eb38147d57'
);
We advise you to create a unique nonce per entity (use the EncryptionManager::generateNonce()
method). You'll have to save the nonce with your data too, because it will be used when you want to decrypt the data again.
<?php
$string = 'This is my default text string with 88 numbers!';
$nonce = EncryptionManager::generateNonce();
$encrypted = EncryptionManager::encrypt($string, $nonce);
In order to decrypt a field, you'll need the encrypted string and the nonce.
<?php
$string = EncryptionManager::decrypt($encrypted, $nonce);