简体   繁体   中英

How to extract digital signature from pdf using python?

I am using python 3.7 and i want to extract digital signature from an invoice(pdf) and then verify the digital signature . How do i go about it...Please help I do not have any idea regarding this digital signature extraction and need to start from scratch.

Thank You

Lets start with what a DSA is and the difference between that and a MAC or hash . A digital signature algorithm provides integrity, authentication, and non-repudiation, whereas a Message Authentication Code provides integrity and authentication, and a hash only provides integrity.

Since you are starting from scratch, I will include some informal definitions:
Integrity - integrity is ensured when you can definitely say there have been no modifications.
Authentication - Authentication is ensured when you can verify that the data came from the intended source. Ex. I can prove that Tom sent me the PDF.
Non-repudiation - Non-repudiation is ensured when a third-party can verify both the data's integrity and authentication given only the data and the relevant info (public key typically). Ex. John can verify that I sent you the pdf.

So depending on which type of algorithm you need, here are a few examples:

Hash

import hashlib
print(hashlib.sha256(open('file.pdf', 'r').read().encode()).hexdigest())


MAC - HMAC, keyed hash

import hashlib, hmac
print(hmac.digest(b"key", b"message", digest=hashlib.sha256))


DSA - using the cryptography package Taken fromhere

>>> from cryptography.hazmat.backends import default_backend
>>> from cryptography.hazmat.primitives import hashes
>>> from cryptography.hazmat.primitives.asymmetric import dsa
>>> private_key = dsa.generate_private_key(
...     key_size=1024,
...     backend=default_backend()
... )
>>> data = b"this is some data I'd like to sign"
>>> signature = private_key.sign(
...     data,
...     hashes.SHA256()
... )

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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