PHP code example of spaze / encryption
1. Go to this page and download the library: Download spaze/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/ */
spaze / encryption example snippets
Spaze\Encryption\SymmetricKeyEncryption::__construct(array $keys, string $activeKeyId, string $keyPrefix)
$keys = [
'key1' => 'adek_79e0[...]8a8d',
'key2' => 'adek_d22c[...]cfa3',
];
$activeKeyId = 'key2';
$keyPrefix = 'adek';
$encryption = new Spaze\Encryption\SymmetricKeyEncryption($keys, $activeKeyId, $keyPrefix);
Spaze\Encryption\SymmetricKeyEncryption::encrypt(string $data): string
$encrypted = $encryption->encrypt($addressData);
Spaze\Encryption\SymmetricKeyEncryption::encryptWithAd(string $data, string $additionalData): string
$encrypted = $encryption->encryptWithAd($addressData, $tenantId);
Spaze\Encryption\SymmetricKeyEncryption::decrypt(string $data): string
$decrypted = $encryption->decrypt($encrypted);
Spaze\Encryption\SymmetricKeyEncryption::decryptWithAd(string $data, string $additionalData): string
$decrypted = $encryption->decryptWithAd($encrypted, $tenantId);
Spaze\Encryption\AuthenticatedPublicKeyEncryption::__construct(array $secretKeys, array $publicKeys, string $activeKeyId, string $keyPrefix)
$secretKeys = [
'key1' => 'adek_secret_79e0[...]8a8d',
];
$publicKeys = [
'key1' => 'adek_public_d22c[...]cfa3',
];
$encryption = new Spaze\Encryption\AuthenticatedPublicKeyEncryption($secretKeys, $publicKeys, 'key1', 'adek');
$keyPair = sodium_crypto_box_keypair();
$secretKey = 'adek_secret_' . sodium_bin2hex(sodium_crypto_box_secretkey($keyPair));
$publicKey = 'adek_public_' . sodium_bin2hex(sodium_crypto_box_publickey($keyPair));
Spaze\Encryption\AnonymousPublicKeyEncryption::__construct(array $secretKeys, array $publicKeys, string $activeKeyId, string $keyPrefix)
$encryption = new Spaze\Encryption\AnonymousPublicKeyEncryption([], ['key1' => 'adek_public_d22c[...]cfa3'], 'key1', 'adek');
$encrypted = $encryption->encrypt($addressData); // works
$decrypted = $encryption->decrypt($encrypted); // throws MissingSecretKeyException
$encryption = new Spaze\Encryption\AnonymousPublicKeyEncryption(['key1' => 'adek_secret_79e0[...]8a8d'], [], 'key1', 'adek');
sodium_bin2hex(random_bytes(32))
use Spaze\Encryption\SymmetricKeyEncryption;
class Something
{
public function __construct(
private SymmetricKeyEncryption $emailEncryption,
) {
// ...
}
public function doSomething()
{
// ...
$encryptedEmail = $this->emailEncryption->encrypt($email);
// ...
}
public function doSomethingElse()
{
// ...
$decryptedEmail = $this->emailEncryption->decrypt($email);
// ...
}
}