1. Go to this page and download the library: Download phpnomad/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/ */
phpnomad / encryption example snippets
use PHPNomad\Encryption\Providers\ArrayKeyProvider;
use PHPNomad\Encryption\Models\EncryptedValue;
use PHPNomad\Sodium\EncryptionIntegration\Strategies\SodiumEncryptionStrategy;
// A key ring holding one 32-byte key at version 1.
$keys = new ArrayKeyProvider([1 => sodium_crypto_aead_xchacha20poly1305_ietf_keygen()]);
// The cipher comes from the integration package; everything else is this one.
$encryption = new SodiumEncryptionStrategy($keys);
$sealed = $encryption->encrypt('sk-live-super-secret');
// Persist it however your storage layer prefers — the value is pure data, so the
// shape is yours. For example, spread across columns (base64 for text columns):
$row = [
'ciphertext' => base64_encode($sealed->getCiphertext()),
'nonce' => base64_encode($sealed->getNonce()),
'key_version' => $sealed->getKeyVersion(),
'cipher' => $sealed->getCipher(),
];
// Later — rebuild the value from your stored fields and decrypt:
$restored = new EncryptedValue(
base64_decode($row['ciphertext']),
base64_decode($row['nonce']),
(int) $row['key_version'],
$row['cipher'],
);
$plaintext = $encryption->decrypt($restored);
// => "sk-live-super-secret"
// v1 was current when old values were sealed. Now add v2 and make it current.
$keys = new ArrayKeyProvider([
1 => $oldKey, // retained so old ciphertext still decrypts
2 => $newKey,
], currentVersion: 2);
$encryption = new SodiumEncryptionStrategy($keys);
$encryption->encrypt('x'); // sealed under v2
$encryption->decrypt($oldValue); // still decrypts against v1
use PHPNomad\Encryption\Interfaces\EncryptionStrategy;
use PHPNomad\Encryption\Models\EncryptedValue;
final class MyCipherStrategy implements EncryptionStrategy
{
public const CIPHER = 'my-cipher-v1';
public function encrypt(string $plaintext, string $context = ''): EncryptedValue { /* ...return new EncryptedValue($ct, $nonce, $version, self::CIPHER) */ }
public function decrypt(EncryptedValue $value, string $context = ''): string { /* ... */ }
}