PHP Luminova: Data Encryption and Decryption Drivers Interface
The Encryption Driver allows you to secure sensitive data in your applications. By implementing either OpenSSL or Sodium encryption methods, with flexibility in choosing your encryption approach.
The Encryption Driver provides a flexible solution for data encryption within the Luminova Framework. It serves as an abstraction layer that supports multiple encryption algorithms, including OpenSSL and Sodium, ensuring robust security measures while offering developers the flexibility to choose the encryption method that best fits their needs.
- Class interface:
\Luminova\Interface\EncryptionInterface
constructor
Constructor, optional pass a blank key string and setKey() later before encrypt/decrypt.
OpenSSL Class namespace: \Luminova\Security\Encryption\OpenSSL
new OpenSSL(?string $key = null, ?string $method = null, int $size = 16);Sodium Class namespace: \Luminova\Security\Encryption\Sodium
new Sodium(?string $key = null, ?string $method = null, int $size = 16);Parameters:
| Parameter | Type | Description |
|---|---|---|
$key | string|null | The encryption key. |
$method | string|null | Optional encryption cypher method.. |
$size | int | Optional key size for encryption.. |
Throws:
- \Luminova\Exceptions\InvalidException - If the method or block size is invalid while using openssl.
Methods
setData
Set the data to encrypt/decrypt.
public setData(string $data): voidParameters:
| Parameter | Type | Description |
|---|---|---|
$data | string | The data to encrypt/decrypt. |
setKey
Set the encryption key.
public setKey(string $key): voidParameters:
| Parameter | Type | Description |
|---|---|---|
$key | string | The encryption key. |
setNonce
Set nonce for encryption and decryption, if null random nonce will be generated.
public setNonce(string|null $nonce = null): voidParameters:
| Parameter | Type | Description |
|---|---|---|
$nonce | string|null | The nonce for encryption. |
setMethod
Set the encryption method and block size for openssl, this method will be ignored on Sodium.
public setMethod(string $method, int $size = 16): voidParameters:
| Parameter | Type | Description |
|---|---|---|
$method | string | The encryption cypher method. |
$mode | int | Optional Key size for encryption.. |
Throws:
- \Luminova\Exceptions\InvalidException - If the method or block size is invalid.
nonce
Generate a random nonce, or return from a string.
public nonce(string|null $string = null): stringParameters:
| Parameter | Type | Description |
|---|---|---|
$string | string|null | The string to extract the nonce from. |
Return Value:
The nonce string.
encrypt
Encrypt data.
public encrypt(): string|boolReturn Value:
The encrypted data, or false if encryption fails.
Throws:
- \Luminova\Exceptions\InvalidException - If encryption fails due to invalid parameters.
decrypt
Decrypt data.
public decrypt(): string|boolReturn Value:
The decrypted data, or false if decryption fails.
Throws:
- \Luminova\Exceptions\InvalidException - If decryption fails due to invalid parameters.
free
Free up resources.
public free(): void