0

I am using python 3. I coded a program that creates some text files daily. I want to sign these files and verify when necessary that these files haven not been changed and stayed original from signing. So, can I create a timestamp prove? How to verify a signed file in python I found only that topic. How can I do that? What modules should I use and how?

curveball
  • 4,320
  • 15
  • 39
  • 49

1 Answers1

-1

You can create a hash of the file using hashlib package, like this:

import hashlib

hasher = hashlib.md5()
with open('file.txt', 'rb') as f:
    buf = f.read()
    hasher.update(buf)
print(hasher.hexdigest())

this snippet calculates the MD5 hash of the given file, you can use another hash function (MD6, SHA-1, SHA-256...) and store the hash somewhere like on a database or just rename the text file with the calculated hash if the name of the file dont matter, later you can use the same code to calculate the hash of the same file and compare the new result with the old one.

hope this helps!

  • 1
    This is nice for verification purposes, but it doesn't constitute a signature in the cryptographic sense because you're not using a signing key. – chicks Jun 29 '19 at 11:09