Neuron-PHP

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

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
throws
Exception

If decryption fails or authentication fails

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
throws
Exception

If encryption fails

Return values
string

JSON-encoded encrypted payload

generateKey()

Generate a cryptographically secure random key

public generateKey() : string
Tags
throws
Exception

If key generation fails

Return values
string

A 32-byte (256-bit) key encoded as hex

getCipher()

Get the cipher algorithm name

public getCipher() : string
Return values
string

isValidKey()

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


        
On this page

Search results