OpenSSLEncryptor
in package
implements
IEncryptor
OpenSSL-based encryption implementation
Provides secure encryption/decryption using OpenSSL with AES-256-CBC cipher. Includes HMAC authentication to ensure data integrity and authenticity.
Table of Contents
Interfaces
- IEncryptor
- Interface for encryption/decryption implementations
Constants
- CIPHER = 'aes-256-cbc'
- KEY_LENGTH = 32
Methods
- decrypt() : string
- Decrypt data encrypted with encrypt()
- encrypt() : string
- Encrypt data using OpenSSL with AES-256-CBC
- generateKey() : string
- Generate a cryptographically secure random key
- getCipher() : string
- Get the cipher algorithm name
- isValidKey() : bool
- Validate that a key meets the requirements
- generateMac() : string
- Generate HMAC for payload authentication
- verifyMac() : bool
- Verify HMAC for payload authentication
Constants
CIPHER
private
mixed
CIPHER
= 'aes-256-cbc'
KEY_LENGTH
private
mixed
KEY_LENGTH
= 32
Methods
decrypt()
Decrypt data encrypted with encrypt()
public
decrypt(string $encryptedData, string $key) : string
Parameters
- $encryptedData : string
-
JSON-encoded encrypted payload
- $key : string
-
The decryption key
Tags
Return values
string —The decrypted plaintext data
encrypt()
Encrypt data using OpenSSL with AES-256-CBC
public
encrypt(string $data, string $key) : string
The encrypted payload includes:
- Base64 encoded encrypted data
- Base64 encoded initialization vector (IV)
- HMAC for authentication
- Metadata (cipher type)
Parameters
- $data : string
-
The plaintext data to encrypt
- $key : string
-
The encryption key
Tags
Return values
string —JSON-encoded encrypted payload
generateKey()
Generate a cryptographically secure random key
public
generateKey() : string
Tags
Return values
string —A 32-byte (256-bit) key encoded as hex
getCipher()
Get the cipher algorithm name
public
getCipher() : string
Return values
stringisValidKey()
Validate that a key meets the requirements
public
isValidKey(string $key) : bool
Parameters
- $key : string
-
The key to validate (hex encoded or raw binary)
Return values
bool —True if the key is valid
generateMac()
Generate HMAC for payload authentication
private
generateMac(array<string|int, mixed> $payload, string $key) : string
Parameters
- $payload : array<string|int, mixed>
-
The payload to authenticate
- $key : string
-
The key for HMAC
Return values
string —The HMAC hash
verifyMac()
Verify HMAC for payload authentication
private
verifyMac(array<string|int, mixed> $payload, string $key) : bool
Parameters
- $payload : array<string|int, mixed>
-
The payload to verify
- $key : string
-
The key for HMAC
Return values
bool —True if MAC is valid