简体   繁体   English

检查 pdf 是否已签名

[英]Check if a pdf is signed or not

I would like to write a python script to check if a pdf is signed or not.我想写一个 python 脚本来检查 pdf 是否被签名。 After quite a bit of looking around, I saw that pyPDF2 helps extract text from pdf files, but I am not sure if it can be used to extract the signature details such as Public Key etc.环顾四周后,我看到 pyPDF2 有助于从 pdf 个文件中提取文本,但我不确定它是否可用于提取签名详细信息,如公钥等。

I did go through some of the open source packages like pyhanko and cryptography but I am a bit stuck as to how to do it.我通过一些开源包(如 pyhanko 和 cryptography)完成了 go,但我对如何去做有点困惑。

I have not worked on encryptions or PDFs using python before.我之前没有使用 python 处理过加密或 PDF。 Could you please guide me on the best way possible to execute this?您能指导我执行此操作的最佳方法吗?

Thanks and best regards, Raghu谢谢并致以最诚挚的问候, Raghu

I tried using cryptography package but was not quite sure how to extract the signature certificate from the pdf.我尝试使用密码学 package 但不太确定如何从 pdf 中提取签名证书。

Adobe pdf offers export in PKCS7 and CER format.I would like to know how to do this using python. This is inorder to have a validation step for another process. Adobe pdf 提供 PKCS7 和 CER 格式的导出。我想知道如何使用 python 执行此操作。这是为了对另一个过程进行验证步骤。

Also appreciate if there are other easy and smart ways to check if the pdf is signed or not.还要感谢是否有其他简单而聪明的方法来检查 pdf 是否已签名。

disclaimer: I am the author of borb the library used in this answer免责声明:我是本答案中使用的库borb的作者

Simply load the PDF using borb , get the DocumentInfo object, and call its has_signatures function.只需使用borb ,获取DocumentInfo object,并调用其has_signatures function。

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()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM