PHP code example of switon / crypto

1. Go to this page and download the library: Download switon/crypto library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

switon / crypto example snippets


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);
    }
}