Add test decryption for pg crypto columns

This commit is contained in:
Clemens Schwaighofer
2025-04-14 09:19:58 +09:00
parent 8aee448c59
commit 07e46c91ab
2 changed files with 27 additions and 1 deletions

View File

@@ -15,6 +15,8 @@ ob_start();
define('USE_DATABASE', true); define('USE_DATABASE', true);
// sample config // sample config
require 'config.php'; require 'config.php';
// for testing encryption compare
use OpenPGP\OpenPGP;
// define log file id // define log file id
$LOG_FILE_ID = 'classTest-db-query-encryption'; $LOG_FILE_ID = 'classTest-db-query-encryption';
ob_end_flush(); ob_end_flush();
@@ -50,6 +52,7 @@ print "Secret Key: " . $key . "<br>";
// test text // test text
$text_string = "I a some deep secret"; $text_string = "I a some deep secret";
$text_string = "I a some deep secret ABC";
// //
$crypt = new SymmetricEncryption($key); $crypt = new SymmetricEncryption($key);
$encrypted = $crypt->encrypt($text_string); $encrypted = $crypt->encrypt($text_string);
@@ -134,6 +137,28 @@ if ($res === false) {
print "libsodium and pgcrypto hash hmac match<br>"; print "libsodium and pgcrypto hash hmac match<br>";
} }
} }
$encryptedMessage_template = <<<TEXT
-----BEGIN PGP MESSAGE-----
{BASE64}
-----END PGP MESSAGE-----
TEXT;
$base64_string = base64_encode(hex2bin($res['pg_crypt_text']));
$encryptedMessage = str_replace(
'{BASE64}',
$base64_string,
$encryptedMessage_template
);
try {
$literalMessage = OpenPGP::decryptMessage($encryptedMessage, passwords: [$key]);
$decrypted = $literalMessage->getLiteralData()->getData();
print "Pg decrypted PHP: " . $decrypted . "<br>";
if ($decrypted == $text_string) {
print "Decryption worked<br>";
}
} catch (\Exception $e) {
print "Error decrypting message: " . $e->getMessage() . "<br>";
}
// do compare for PHP and pgcrypto settings // do compare for PHP and pgcrypto settings

View File

@@ -24,6 +24,7 @@
"egrajp/smarty-extended": "^5.4", "egrajp/smarty-extended": "^5.4",
"php": ">=8.1", "php": ">=8.1",
"gullevek/dotenv": "^2.0", "gullevek/dotenv": "^2.0",
"psr/log": "^2.0 || ^3.0" "psr/log": "^2.0 || ^3.0",
"php-privacy/openpgp": "^2.1"
} }
} }