检查pdf是否签名



我想写一个python脚本来检查pdf是否签名。经过相当多的查找,我看到pyPDF2可以帮助从pdf文件中提取文本,但我不确定它是否可以用于提取签名细节,如公钥等。

我确实浏览了一些开源包,如pyhanko和cryptography,但我有点卡在如何做到这一点。

我以前没有使用python进行加密或pdf。你能告诉我最好的执行方法吗?

感谢并致以最良好的问候。拉

我尝试使用加密包,但不太确定如何从pdf中提取签名证书。

Adobe pdf提供PKCS7和CER格式的导出。我想知道如何使用python做到这一点。这是为了对另一个进程有一个验证步骤。

如果有其他简单而聪明的方法来检查pdf是否签名,我也很感激。

免责声明:我是borb的作者,这个库在这个答案中使用

使用borb加载PDF,获取DocumentInfo对象,并调用其has_signatures函数。

from borb.pdf import PDF
from borb.pdf import Document
import typing
# read the PDF
doc: typing.Optional[Document] = None
with open("input.pdf", "rb") as fh:
doc = PDF.loads(fh)
# check whether anything has been read
# this may fail due to IO error
# or a corrupt PDF
assert doc is not None
# check whether signatures are in the PDF
doc.get_document_info().has_signatures()

如果您需要检查pdf是否已签名,只需使用pypdf,您可以尝试:

from pypdf import PdfReader
file = PdfReader(FILE)
sign = file.get_fields()
if sign is None:
print('No signature')
else:
print('Signature detected')

最新更新