Luminova Framework

PHP Luminova: Encryption Driver for OpenSSL and Sodium

Last updated: 2025-10-30 18:51:01

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.


Class Definition


Methods

constructor

Creates a new encryption driver instance.

You can optionally pass a key during initialization or call setKey() later 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|nullOptional encryption cypher method or set later (via setMethod()).
$sizeintOptional encryption key size to use if failed to determine size from method.

Throws:

Examples:

Using Openssl encryption handling.

use Luminova\Security\Encryption\Driver\Openssl;

$openssl = new Openssl(
    key: 'mykey',
    method: 'AES-128-CBC',
    size: 16
);

Using Sodium encryption handling.

use Luminova\Security\Encryption\Driver\Sodium;

$sodium = new Sodium(
    key: 'mykey',
    method: 'AES-128-CBC',
    size: 16
);

Programmatic Example:

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';

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

$crypto = new $class(
    $key,
    $method,
    Key::size($method)
);

// 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.


setData

Set the data to encrypt/decrypt.

This method allows you to specify encoded message hash to decrypt or a plain text to encrypt.

public setData(string $data): EncryptionInterface

Parameters:

ParameterTypeDescription
$datastringThe data to encrypt or hash to decrypt.

Return Value:

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


setKey

Set the encryption or decryption key.

public setKey(string $key): EncryptionInterface

Parameters:

ParameterTypeDescription
$keystringThe cryptography key.

Return Value:

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


setNonce

Set nonce for encryption and decryption, 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.

Note:This method is only useful with OpenSSl driver, if set in Sodium, the value will be ignored.


setMethod

Set the encryption method and block size for openssl, 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 nonce(?string $string = null): string

Parameters:

ParameterTypeDescription
$stringstring|nullThe string to extract the nonce from.

Return Value:

string - Return the generated encryption nonce string.


encrypt

Encrypt data into an encoded message.

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

public encrypt(): string|false

Return Value:

string|false Returns the encrypted hash value, or false if encryption fails.

Throws:


decrypt

Decrypt an encoded message.

This method performs cryptography to decipher an encrypted hash into a readable plan text.

public decrypt(): string|false

Return Value:

string|false - Returns the decrypted data, or false if decryption fails.

Throws:


free

Free up encryption resources.

public free(): void