Files
myep/src/Security/Crypt.php
T

51 lines
1.2 KiB
PHP

<?php
declare(strict_types=1);
namespace App\Security;
use Spatie\Crypto\Rsa\PrivateKey;
use Spatie\Crypto\Rsa\PublicKey;
/**
* RSA encryption/decryption utilities for secure credential storage.
*
* Uses asymmetric encryption to store BPN API passwords securely. Private key
* encrypts data for storage, public key decrypts for API calls. Also provides
* signing/verification for message integrity.
*/
class Crypt
{
public function __construct(private readonly string $path)
{
}
public function encrypt(string $message): string
{
$privateKey = PrivateKey::fromFile($this->path.'/private.key');
return $privateKey->encrypt($message);
}
public function decrypt(string $message): string
{
$publicKey = PublicKey::fromFile($this->path.'/public.key');
return $publicKey->decrypt($message);
}
public function sign(string $message): string
{
$privateKey = PrivateKey::fromFile($this->path.'/private.key');
return $privateKey->sign($message);
}
public function verify(string $message, string $signature): bool
{
$publicKey = PublicKey::fromFile($this->path.'/public.key');
return $publicKey->verify($message, $signature);
}
}