如何使用带有etoken(笔驱动器)的Python对PDF文档进行数字签名? [英] How to digitally sign PDF documents using Python with an etoken (pen drive)?

查看:207
本文介绍了如何使用带有etoken(笔驱动器)的Python对PDF文档进行数字签名?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何使用Python对PDF文档进行数字签名?我有一个令牌(在笔式驱动器中)。

How to digitally sign PDF documents using Python? I have an etoken (in pen drive).

此外,我还使用openpyxl创建了一个excel文件,并将其转换为PDF。现在有一个要求,我需要在该PDF文档中添加数字签名。

Additionally, I have created an excel file using openpyxl and converted it into PDF. Now there is a requirement that I need to add digital signature to that PDF document.

有什么方法可以在python中实现吗?

Is there any way I can achieve this in python?

推荐答案

使用为此任务设计的python模块,它对PDF-s进行数字签名。
您应该拥有的所有内容都是带有证书的p12 / pfx文件。

Use python module designed for this task, it signs digitally PDF-s. Everything what You should have is p12/pfx file with certificate.

简单示例如下:
github存储库示例

#!/usr/bin/env python3
# *-* coding: utf-8 *-*
from oscrypto import asymmetric
from endesive import pdf

def main():
    dct = {
        b'sigflags': 3,
        b'contact': b'me@email.com',
        b'location': b'City',
        b'signingdate': b'20180731082642+02\'00\'',
        b'reason': b'Some descriptive message',
    }
    p12 = asymmetric.load_pkcs12(
        open('demo2_user1.p12', 'rb').read(),
        '1234'
    )
    datau = open('pdf.pdf', 'rb').read()
    datas = pdf.cms.sign(datau, dct, p12[0], p12[1], [], 'sha256')
    with open('pdf-signed-cms.pdf', 'wb') as fp:
        fp.write(datau)
        fp.write(datas)

main()

这篇关于如何使用带有etoken(笔驱动器)的Python对PDF文档进行数字签名?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆