1
0
Fork 0
mirror of https://github.com/24eme/signaturepdf synced 2024-06-03 14:32:13 +02:00

Outsourcing of the cryptography functions

This commit is contained in:
tale-fau 2023-11-03 15:13:09 +01:00
parent aa951558fb
commit 579379318f
2 changed files with 42 additions and 32 deletions

40
app.php
View file

@ -190,6 +190,8 @@ $f3->route('POST /sign',
} }
); );
require_once 'lib/cryptography.class.php';
$f3->route('POST /share', $f3->route('POST /share',
function($f3) { function($f3) {
$hash = substr(hash('sha512', uniqid().rand()), 0, 20); $hash = substr(hash('sha512', uniqid().rand()), 0, 20);
@ -237,17 +239,9 @@ $f3->route('POST /share',
if(!$f3->get('DEBUG')) { if(!$f3->get('DEBUG')) {
array_map('unlink', glob($tmpfile."*.svg")); array_map('unlink', glob($tmpfile."*.svg"));
} }
$key = "test";
foreach (glob("/tmp/".$hash.'/*.pdf') as $file) { $encryptor = new CryptographyClass();
$outputFile = $file.".gpg"; $encryptor->encrypt($hash);
$command = "echo '$key' | gpg --batch --passphrase-fd 0 --symmetric --cipher-algo AES256 -o $outputFile $file";
$result = shell_exec($command);
if ($result === false) {
echo "Cypher failure.";
exit;
}
unlink($file);
}
$f3->reroute($f3->get('REVERSE_PROXY_URL').'/signature/'.$hash."#informations"); $f3->reroute($f3->get('REVERSE_PROXY_URL').'/signature/'.$hash."#informations");
} }
@ -259,17 +253,8 @@ $f3->route('GET /signature/@hash/pdf',
$hash = Web::instance()->slug($f3->get('PARAMS.hash')); $hash = Web::instance()->slug($f3->get('PARAMS.hash'));
$sharingFolder = $f3->get('PDF_STORAGE_PATH').$hash; $sharingFolder = $f3->get('PDF_STORAGE_PATH').$hash;
$key = "test"; $cryptor = new CryptographyClass();
foreach (glob("/tmp/".$hash.'/*.gpg') as $file) { $cryptor->decrypt($hash);
$outputFile = str_replace(".gpg", "", $file);
$command = "echo '$key' | gpg --batch --passphrase-fd 0 --decrypt -o $outputFile $file";
$result = shell_exec($command);
if ($result === false) {
echo "Decypher failure.";
exit;
}
unlink($file);
}
$files = scandir($sharingFolder); $files = scandir($sharingFolder);
$originalFile = $sharingFolder.'/original.pdf'; $originalFile = $sharingFolder.'/original.pdf';
@ -296,16 +281,7 @@ $f3->route('GET /signature/@hash/pdf',
} }
Web::instance()->send($finalFile, null, 0, TRUE, $filename); Web::instance()->send($finalFile, null, 0, TRUE, $filename);
foreach (glob("/tmp/".$hash.'/*.pdf') as $file) { $cryptor->encrypt($hash);
$outputFile = $file.".gpg";
$command = "echo '$key' | gpg --batch --passphrase-fd 0 --symmetric --cipher-algo AES256 -o $outputFile $file";
$result = shell_exec($command);
if ($result === false) {
echo "Cypher failure.";
exit;
}
unlink($file);
}
if($f3->get('DEBUG')) { if($f3->get('DEBUG')) {
return; return;

View file

@ -0,0 +1,34 @@
<?php
class CryptographyClass
{
public function encrypt($hash) {
$key = "test";
foreach (glob("/tmp/".$hash.'/*.pdf') as $file) {
$outputFile = $file.".gpg";
$command = "echo '$key' | gpg --batch --passphrase-fd 0 --symmetric --cipher-algo AES256 -o $outputFile $file";
$result = shell_exec($command);
if ($result === false) {
echo "Cypher failure";
exit;
}
unlink($file);
}
}
public function decrypt($hash) {
$key = "test";
foreach (glob("/tmp/".$hash.'/*.gpg') as $file) {
$outputFile = str_replace(".gpg", "", $file);
$command = "echo '$key' | gpg --batch --passphrase-fd 0 --decrypt -o $outputFile $file";
$result = shell_exec($command);
if ($result === false) {
echo "Decypher failure";
exit;
}
unlink($file);
}
}
}
?>