58 lines
2.1 KiB
PHP
58 lines
2.1 KiB
PHP
<?php
|
|
// Generated by setup_mariadb_project.sh — edit as needed.
|
|
define('DB_HOST', '127.0.0.1');
|
|
define('DB_NAME', 'app_30953');
|
|
define('DB_USER', 'app_30953');
|
|
define('DB_PASS', 'e45f2778-db1f-450c-99c6-29efb4601472');
|
|
|
|
// --- Encryption Settings ---
|
|
// WARNING: Changing this key will make all existing encrypted data unreadable.
|
|
// For production, use a key from a secure source like an environment variable.
|
|
define('ENCRYPTION_KEY', 'def0000068fcf8f7483bde1c8a45b53289f734814842116f7238e4375290654f27a845b20d3435324d83a335e86c45000a7649364e4358612743677d6a336e3c');
|
|
define('ENCRYPTION_CIPHER', 'aes-256-cbc');
|
|
|
|
/**
|
|
* Encrypts a string.
|
|
*
|
|
* @param string $plaintext The string to encrypt.
|
|
* @return string The encrypted string (base64 encoded).
|
|
*/
|
|
function encrypt($plaintext) {
|
|
$ivlen = openssl_cipher_iv_length(ENCRYPTION_CIPHER);
|
|
$iv = openssl_random_pseudo_bytes($ivlen);
|
|
$ciphertext_raw = openssl_encrypt($plaintext, ENCRYPTION_CIPHER, ENCRYPTION_KEY, OPENSSL_RAW_DATA, $iv);
|
|
$hmac = hash_hmac('sha256', $ciphertext_raw, ENCRYPTION_KEY, true);
|
|
return base64_encode($iv . $hmac . $ciphertext_raw);
|
|
}
|
|
|
|
/**
|
|
* Decrypts a string.
|
|
*
|
|
* @param string $ciphertext_base64 The base64 encoded ciphertext.
|
|
* @return string|false The decrypted string, or false on failure.
|
|
*/
|
|
function decrypt($ciphertext_base64) {
|
|
$c = base64_decode($ciphertext_base64);
|
|
$ivlen = openssl_cipher_iv_length(ENCRYPTION_CIPHER);
|
|
$iv = substr($c, 0, $ivlen);
|
|
$hmac = substr($c, $ivlen, 32);
|
|
$ciphertext_raw = substr($c, $ivlen + 32);
|
|
$original_plaintext = openssl_decrypt($ciphertext_raw, ENCRYPTION_CIPHER, ENCRYPTION_KEY, OPENSSL_RAW_DATA, $iv);
|
|
$calcmac = hash_hmac('sha256', $ciphertext_raw, ENCRYPTION_KEY, true);
|
|
if (hash_equals($hmac, $calcmac)) {
|
|
return $original_plaintext;
|
|
}
|
|
return false;
|
|
}
|
|
|
|
|
|
function db() {
|
|
static $pdo;
|
|
if (!$pdo) {
|
|
$pdo = new PDO('mysql:host='.DB_HOST.';dbname='.DB_NAME.';charset=utf8mb4', DB_USER, DB_PASS, [
|
|
PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
|
|
PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC,
|
|
]);
|
|
}
|
|
return $pdo;
|
|
} |