PHP Luminova: Encryption Configurations for OpenSSL and Sodium
Secure and flexible encryption system supporting OpenSSL and Sodium drivers in Luminova. Easily encrypt and decrypt data with modern cryptography standards.
The Encryption Driver is the foundation of Luminova’s encryption system.It provides a clean and consistent API that abstracts away the complexity of working with different encryption libraries.
You can use either the OpenSSL or Sodium driver to handle encryption and decryption operations with ease. Both drivers accept a key and manage nonces. OpenSSL takes a cipher method. Sodium selects its cipher through the application configuration.
This makes it ideal for developers who want strong encryption without manually managing ciphers, padding, or key handling details.
For most applications, using the Crypter Class is recommended it automatically uses the encryption driver and method set in the application configuration.
Usages
Encrypt and decrypt with OpenSSL and authenticated associated data. The randomly generated key is kept for the duration of this example.
use Luminova\Security\Encryption\Driver\Openssl;
$key = bin2hex(random_bytes(32));
$aad = 'example-context';
$writer = new Openssl($key, 'aes-128-gcm');
$reader = new Openssl($key, 'aes-128-gcm');
try {
$payload = $writer->setAssociatedData($aad)
->setNonce()
->setData('Plain text')
->encrypt();
echo $reader->setAssociatedData($aad)
->setData($payload)
->decrypt();
} finally {
$writer->free();
$reader->free();
}Recommendation
Use the Crypter Class for automatic driverselection (OpenSSL or Sodium), encryption method resolution, and key configuration.It provides a unified and secure interface without requiring manual setup.
Class Definition
- Openssl namespace:
\Luminova\Security\Encryption\Driver\Openssl - Sodium namespace:
\Luminova\Security\Encryption\Driver\Sodium - Implements:Luminova\Interface\EncryptionInterface
Driver Constructors
OpenSSL Driver Constructor
Initializes a new OpenSSL encryption driver.
A key and cipher method may be provided at construction time, or configured later using setKey() and setMethod() before performing encryption or decryption.
public function __construct(?string $key = null, ?string $method = null, int $size = 16)Parameters:
| Parameter | Type | Description |
|---|---|---|
$key | ?string | Optional encryption key or set later (via setKey()) (default null). |
$method | ?string | Cipher method. If omitted, it must be set later (via setMethod()) (default null). |
$size | int | Fallback key size used when the cipher method does not expose a block size (default: 16). |
Throws:
- Luminova\Exceptions\EncryptionException - When an invalid cipher method or block size is used.
Examples:
Using Openssl encryption handling.
use Luminova\Security\Encryption\Driver\Openssl;
$openssl = new Openssl(
key: bin2hex(random_bytes(32)),
method: 'AES-128-CBC',
size: 16
);Sodium Driver Constructor
Initializes a new Sodium encryption driver.
Initialize the driver, then set the key explicitly before using it.
public function __construct(?string $key = null)Parameters:
| Parameter | Type | Description |
|---|---|---|
$key | ?string | Optional encryption key or set later (via setKey()) (default null). |
Throws:
- Luminova\Exceptions\EncryptionException - When the Sodium extension is unavailable or key setup fails.
Using Sodium encryption handling.
use Luminova\Security\Encryption\Driver\Sodium;
$sodium = new Sodium();
$sodium->setKey(bin2hex(random_bytes(32)), salt: '');Methods
Both drivers implement the same interface. Their key derivation and cipher selection differ.
setData
Set the data to encrypt or decrypt.
This method allows you to specify encoded cipher message to decrypt or a plain text to encrypt.
public function setData(string $data): selfParameters:
| Parameter | Type | Description |
|---|---|---|
$data | string | The cipher message to encrypt or decrypt. |
Return:
self - Return instance of encryption driver class.
setAssociatedData
Sets Additional Authenticated Data (AEAD).
This method allows you to include additional authentication data during encryption or decryption.
public function setAssociatedData(array|string $aad): selfParameters:
| Parameter | Type | Description |
|---|---|---|
$aad | array<array-key,mixed>|string | Data to authenticate with an AEAD cipher. Arrays are JSON-encoded. Supply identical associated data when decrypting. |
Return:
self - Return instance of encryption driver class.
Throws:
- Luminova\Exceptions\EncryptionException - If array and failed to encode.
Note:
Must be identical for encryption and decryption or authentication fails.
setKey
Set the encryption or decryption key.
OpenSSL
public function setKey(string $key, int $length = 0, ?string $salt = null): selfSodium
public function setKey(string $key, int $length = 32, ?string $salt = null): selfParameters:
| Parameter | Type | Description |
|---|---|---|
$key | string | The cryptography key. |
$length | int | Derived key length. OpenSSL defaults to 0 for the digest length. Sodium defaults to 32 bytes. |
$salt | ?string | OpenSSL accepts an optional HKDF salt. For Sodium, pass an empty string to select unkeyed derivation, or supply the same valid hash key for encryption and decryption. |
Return:
self - Return instance of encryption driver class.
Throws:
- Luminova\Exceptions\EncryptionException - If error while generating key.
setNonce
Set nonce for encryption, if null random nonce will be generated.
public function setNonce(?string $nonce = null): selfParameters:
| Parameter | Type | Description |
|---|---|---|
$nonce | ?string | Optional nonce. Leave it as null to generate a fresh nonce before each encryption. |
Return:
self - Return instance of encryption driver class.
setMethod
Set the encryption method and block size for openssl.
This method sets an encryption method and block size for openssl driver only.This method will be ignored on Sodium.
OpenSSL
public function setMethod(string $method, int $size = 16): selfSodium
public function setMethod(string $method, int $size = 24): selfParameters:
| Parameter | Type | Description |
|---|---|---|
$method | string | The encryption cypher method. |
$size | int | Fallback nonce size if OpenSSL cannot determine the cipher’s IV length. Sodium ignores this parameter. |
Return:
self - Return instance of encryption driver class.
Throws:
- Luminova\Exceptions\EncryptionException - If the method or block size is invalid.
nonce
Generate a random nonce, or return from a string.
public static function nonce(int $length, ?string $string = null): stringParameters:
| Parameter | Type | Description |
|---|---|---|
$length | int | The nonce length to generate. |
$string | ?string | The string to drive the nonce from. |
Return:
string - Return the generated encryption nonce string.
Throws:
- Luminova\Exceptions\EncryptionException - If error while generating nonce.
encrypt
Encrypt data into an encoded message.
This method performs cryptography to generate an encryption hash from a plan text.
public function encrypt(): stringReturn:
string - Returns the encoded ciphertext payload, including the nonce used for encryption.
Return the encrypted cipher message data if encryption succeed.
Throws:
- Luminova\Exceptions\EncryptionException - If encryption fails due to invalid parameters.
Note:Encrypted payload is encoded in base64 string.
decrypt
Decrypt an encoded cipher message.
This method performs cryptography to decipher an encrypted hash into a readable plan text.
public function decrypt(): stringReturn:
string - Return the decrypted plain-text content if decryption succeed.
Throws:
- Luminova\Exceptions\EncryptionException - If decryption fails or invalid parameters.
free
Free up cryptography resources.
This will clear:
- key
- nonce
- message
- addition authentication data
public function free(): boolReturn:
bool - Return true if freed, otherwise false.