Encryption Helpers
The package exposes two global helpers.
crypto_encode()
function crypto_encode($data, string $type = CryptorType::SYMMETRIC): string
Behavior:
- serializes the input with PHP
serialize(...) - resolves a cryptor through
CryptorFactory::get($type) - calls
encrypt(...)on the resolved cryptor
Implications
- scalar values, arrays, and objects all go through serialization first
- the output format is helper-level ciphertext, not the raw representation of the original value
- the default cryptor type is symmetric
crypto_decode()
function crypto_decode(string $encryptedData, string $type = CryptorType::SYMMETRIC)
Behavior:
- resolves a cryptor through
CryptorFactory::get($type) - decrypts the string
- runs
@unserialize(...)on the decrypted result - returns the unserialized value when that result is not
false - otherwise returns the decrypted string
Type and lifecycle caveats
Asymmetric helper calls depend on shared instance reuse
Because the asymmetric adapter generates a key pair at construction time, this works only while the same factory-managed adapter instance is reused:
$encrypted = crypto_encode('secret', CryptorType::ASYMMETRIC);
$value = crypto_decode($encrypted, CryptorType::ASYMMETRIC);
Within one live DI container, CryptorFactory memoization preserves that key pair.
Across a container reset, process restart, or any new asymmetric adapter instance, the same ciphertext is no longer expected to decrypt correctly.
false does not round-trip cleanly
As noted in the package contract, helper decoding cannot distinguish:
- "unserialize failed"
- "unserialize succeeded and the actual value is boolean false"
So helper consumers should not rely on exact false round-tripping.
Where helpers are used in core
Current framework code uses these helpers in at least two places:
- Cookie storage encrypts values before writing and decrypts on read
- Session storage encrypts values before storing and decrypts on read
That makes symmetric mode part of the framework's default data-at-rest behavior for those packages.