Luminova Framework

PHP Luminova: Encryption Driver for OpenSSL and Sodium

Last updated: 2026-02-15 10:27:04

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 support setting custom keys, methods, and key sizes, while following modern cryptographic best practices.

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 selects and configures the best available encryption driver and method.


Usages

Initialize based on application encryption configuration.

use Luminova\Security\Encryption\Driver\{Openssl, Sodium};
use Luminova\Security\Encryption\Key;

$handler = Key::handler(true);
$method  = Key::method();
$key     = 'mykey';

$crypto = match ($handler) {
  'openssl' => new Openssl($key, $method, Key::size($method)),
  'sodium'  => new Sodium($key),
  default   => throw new Error('Encryption handler not supported.'),
};

// Encrypt
$crypto->setNonce($nonce); // Optional
$crypto->setData('Plain text');
$hash = $crypto->encrypt();

// Decrypt
echo $crypto->setData($hash)->decrypt();

Recommendation

Use the Crypter Class for automatic driver selection (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 __construct(?string $key = null, ?string $method = null, int $size = 16): mixed

Parameters:

ParameterTypeDescription
$keystring|nullOptional encryption key or set later (via setKey()).
$methodstring|nullCipher method. If omitted, it must be set later (via setMethod()).
$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: 'mykey',
  method: 'AES-128-CBC',
  size: 16
);

Sodium Driver Constructor

Initializes a new Sodium encryption driver.

A key may be provided at construction time, or set later using setKey() before performing encryption or decryption.

public __construct(?string $key = null): mixed

Parameters:

ParameterTypeDescription
$keystring|nullOptional encryption key or set later (via setKey()).

Throws:

Using Sodium encryption handling.

use Luminova\Security\Encryption\Driver\Sodium;

$sodium = new Sodium(
  key: 'mykey'
);

Methods

Both drivers share methods and implementation logic.

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 setData(string $data): EncryptionInterface

Parameters:

ParameterTypeDescription
$datastringThe cipher message to encrypt or decrypt.

Return Value:

Luminova\Interface\EncryptionInterface - 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 setAssociatedData(array|string $aad): EncryptionInterface

Parameters:

ParameterTypeDescription
$aadstringThe additional data to authenticate (not encrypted).

Return Value:

Luminova\Interface\EncryptionInterface - 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.

public setKey(string $key, int $length = 0, ?string $salt = null): EncryptionInterface

Parameters:

ParameterTypeDescription
$keystringThe cryptography key.
$lengthintOptional key length (default: 0).
$saltstring|nullOptional random salt to use during HKDF key derivation (default: null).

Return Value:

Luminova\Interface\EncryptionInterface - Return instance of encryption driver class.

Throws:


setNonce

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

public setNonce(?string $nonce = null): EncryptionInterface

Parameters:

ParameterTypeDescription
$noncestring|nullOptional cryptography nonce for encryption.

Return Value:

Luminova\Interface\EncryptionInterface - 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.

public setMethod(string $method, int $size = 16): EncryptionInterface

Parameters:

ParameterTypeDescription
$methodstringThe encryption cypher method.
$modeintOptional encryption key size to use if failed to determine size from method.

Return Value:

Luminova\Interface\EncryptionInterface - Return instance of encryption driver class.

Throws:


nonce

Generate a random nonce, or return from a string.

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

Parameters:

ParameterTypeDescription
$lengthintThe nonce length to generate.
$stringstring|nullThe string to drive the nonce from.

Return Value:

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

Return Value:

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

Return Value:

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 free(): bool

Return Value:

bool - Return true if freed, otherwise false.