| Server IP : 138.197.107.151 / Your IP : 216.73.217.10 Web Server : Apache/2.4.58 (Ubuntu) System : Linux BloxBy-Builder 6.8.0-71-generic #71-Ubuntu SMP PREEMPT_DYNAMIC Tue Jul 22 16:52:38 UTC 2025 x86_64 User : wpbetasites_mrakzqskir ( 1022) PHP Version : 8.3.6 Disable Function : NONE MySQL : OFF | cURL : ON | WGET : ON | Perl : ON | Python : OFF | Sudo : ON | Pkexec : OFF Directory : /var/www/shark/app/Helpers/ |
Upload File : |
<?php
namespace App\Helpers;
use RuntimeException;
class EncryptionHelper
{
private const CIPHER = 'aes-256-gcm';
private const TAG_LENGTH = 16; // GCM tag length
public static function secret_encrypt(string $plaintext, string $keyHex): string
{
// Convert hex key into raw 32 bytes
$key = hex2bin($keyHex);
if ($key === false || strlen($key) !== 32) {
throw new RuntimeException('Invalid key: must be 64 hex characters (32 bytes)');
}
$iv = random_bytes(openssl_cipher_iv_length(self::CIPHER));
$tag = '';
$ct = openssl_encrypt(
$plaintext,
self::CIPHER,
$key,
OPENSSL_RAW_DATA,
$iv,
$tag,
'',
self::TAG_LENGTH
);
if ($ct === false) {
throw new RuntimeException('Encrypt failed');
}
// Pack as iv + tag + ciphertext → base64
return base64_encode($iv . $tag . $ct);
}
public static function secret_decrypt(string $b64, string $keyHex): string
{
// Convert hex key into raw 32 bytes
$key = hex2bin($keyHex);
if ($key === false || strlen($key) !== 32) {
throw new RuntimeException('Invalid key: must be 64 hex characters (32 bytes)');
}
$raw = base64_decode($b64, true);
if ($raw === false) {
throw new RuntimeException('Bad base64');
}
$ivlen = openssl_cipher_iv_length(self::CIPHER);
$iv = substr($raw, 0, $ivlen);
$tag = substr($raw, $ivlen, self::TAG_LENGTH);
$ct = substr($raw, $ivlen + self::TAG_LENGTH);
$pt = openssl_decrypt($ct, self::CIPHER, $key, OPENSSL_RAW_DATA, $iv, $tag, '');
if ($pt === false) {
throw new RuntimeException('Decrypt failed');
}
return $pt;
}
}