PHP 如何检查 PDF 是否经过数字签名
PHP How to check if PDF is digitally signed
我的 PHP 程序使用 TCPDF
生成 PDF,然后:
- 用户下载PDF;
- 用户对 PDF 进行数字签名;
- 用户上传签名的PDF;
对于第 3 步,我想检查上传的文件是否经过数字签名。
更好的方法是检查上传的文件是否与程序在步骤 1 中生成的文件相同。
试试这个:
<?php
//from:
function isStringInFile($file,$string){
$handle = fopen($file, 'r');
$valid = false; // init as false
while (($buffer = fgets($handle)) !== false) {
if (strpos($buffer, $string) !== false) {
$valid = TRUE;
break; // Once you find the string, you should break out the loop.
}
}
fclose($handle);
return $valid;
}
搜索“adbe.pkcs7.detached”:
//Signed?
echo isStringInFile('mypdf.pdf', 'adbe.pkcs7.detached');
要检查是否是相同的 pdf,您可以使用 TCPDF's setKeyWords() to put some unique keys and check with the Smalot PDF Parser:
<?php
$parser = new \Smalot\PdfParser\Parser();
$pdf = $parser->parseFile('signed_pdf.pdf'); //com keywords
$details = $pdf->getDetails();
我的 PHP 程序使用 TCPDF
生成 PDF,然后:
- 用户下载PDF;
- 用户对 PDF 进行数字签名;
- 用户上传签名的PDF;
对于第 3 步,我想检查上传的文件是否经过数字签名。
更好的方法是检查上传的文件是否与程序在步骤 1 中生成的文件相同。
试试这个:
<?php
//from:
function isStringInFile($file,$string){
$handle = fopen($file, 'r');
$valid = false; // init as false
while (($buffer = fgets($handle)) !== false) {
if (strpos($buffer, $string) !== false) {
$valid = TRUE;
break; // Once you find the string, you should break out the loop.
}
}
fclose($handle);
return $valid;
}
搜索“adbe.pkcs7.detached”:
//Signed?
echo isStringInFile('mypdf.pdf', 'adbe.pkcs7.detached');
要检查是否是相同的 pdf,您可以使用 TCPDF's setKeyWords() to put some unique keys and check with the Smalot PDF Parser:
<?php
$parser = new \Smalot\PdfParser\Parser();
$pdf = $parser->parseFile('signed_pdf.pdf'); //com keywords
$details = $pdf->getDetails();