33 lines
765 B
PHP
33 lines
765 B
PHP
<?php
|
|
|
|
function encryptText(string $text, string $key): array
|
|
{
|
|
$iv = random_bytes(16);
|
|
$cipher = openssl_encrypt($text, 'AES-256-CBC', $key, 0, $iv);
|
|
if ($cipher === false) {
|
|
throw new RuntimeException('Encryption failed.');
|
|
}
|
|
return [
|
|
'cipher' => $cipher,
|
|
'iv' => base64_encode($iv),
|
|
];
|
|
}
|
|
|
|
function decryptText(string $cipher, string $iv, string $key): string|false
|
|
{
|
|
return openssl_decrypt($cipher, 'AES-256-CBC', $key, 0, base64_decode($iv));
|
|
}
|
|
|
|
function generateId(): string
|
|
{
|
|
return bin2hex(random_bytes(16));
|
|
}
|
|
|
|
function jsonResponse(array $data, int $status = 200): void
|
|
{
|
|
http_response_code($status);
|
|
header('Content-Type: application/json');
|
|
echo json_encode($data);
|
|
exit;
|
|
}
|