Encryption, decryption, and key handling utilities for Switon Framework.
composer require switon/cryptoRequirements: PHP 8.3+, ext-openssl, ext-mbstring
use Switon\Core\Attribute\Autowired;
use Switon\Crypto\CipherInterface;
class UserProfileService
{
#[Autowired] protected CipherInterface $cipher;
public function encryptProfile(int $userId, array $profile): string
{
return $this->cipher->encrypt(
json_encode($profile, JSON_THROW_ON_ERROR),
"user:{$userId}"
);
}
public function decryptProfile(int $userId, string $encrypted): array
{
$json = $this->cipher->decrypt($encrypted, "user:{$userId}", 'json');
return json_decode($json, true, flags: JSON_THROW_ON_ERROR);
}
}Docs: https://docs.switon.dev/latest/crypto
MIT.