Luminova Framework

PHP Luminova: Encryption Configurations for OpenSSL and Sodium

Last updated: 2026-11-28 20:57:12

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


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:

ParameterTypeDescription
$key?stringOptional encryption key or set later (via setKey()) (default null).
$method?stringCipher method. If omitted, it must be set later (via setMethod()) (default null).
$sizeintFallback key size used when the cipher method does not expose a block size (default: 16).

Throws:

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:

ParameterTypeDescription
$key?stringOptional encryption key or set later (via setKey()) (default null).

Throws:

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): self

Parameters:

ParameterTypeDescription
$datastringThe 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): self

Parameters:

ParameterTypeDescription
$aadarray<array-key,mixed>&#124;stringData 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:

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): self

Sodium

public function setKey(string $key, int $length = 32, ?string $salt = null): self

Parameters:

ParameterTypeDescription
$keystringThe cryptography key.
$lengthintDerived key length. OpenSSL defaults to 0 for the digest length. Sodium defaults to 32 bytes.
$salt?stringOpenSSL 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:


setNonce

Set nonce for encryption, if null random nonce will be generated.

public function setNonce(?string $nonce = null): self

Parameters:

ParameterTypeDescription
$nonce?stringOptional 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): self

Sodium

public function setMethod(string $method, int $size = 24): self

Parameters:

ParameterTypeDescription
$methodstringThe encryption cypher method.
$sizeintFallback nonce size if OpenSSL cannot determine the cipher’s IV length. Sodium ignores this parameter.

Return:

self - Return instance of encryption driver class.

Throws:


nonce

Generate a random nonce, or return from a string.

public static function nonce(int $length, ?string $string = null): string

Parameters:

ParameterTypeDescription
$lengthintThe nonce length to generate.
$string?stringThe string to drive the nonce from.

Return:

string - Return the generated encryption nonce string.

Throws:


encrypt

Encrypt data into an encoded message.

This method performs cryptography to generate an encryption hash from a plan text.

public function encrypt(): string

Return:

string - Returns the encoded ciphertext payload, including the nonce used for encryption.

Return the encrypted cipher message data if encryption succeed.

Throws:

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(): string

Return:

string - Return the decrypted plain-text content if decryption succeed.

Throws:


free

Free up cryptography resources.

This will clear:

  • key
  • nonce
  • message
  • addition authentication data
public function free(): bool

Return:

bool - Return true if freed, otherwise false.