Easily hashing content of any size in Python
Sometimes need arises in order to check if some data has been tampered or not by chance or because a bilious user. Imagine downloading a file from the internet where the owner of the file has placed another file with the hash signature of the former. You want to check if the file you downloaded has the same signature as the one the author claims to have. If so, then you can be pretty sure it hasn’t been tampered.
Here is an implementation in Python 3.x to hash files and strings of any given size. The first implementation is probably more understandable by anyone having some programming experience. The second one is more python-idiomatic and makes use of the built-in function
iter(callable, sentinel). SHA1 is by default the algorithm used in order to do the hash, but another one can be easily used just passing a new instance of it to the
Basic hash utils file
Idiomatic hash utils file
I’ve based this post mainly from these answers given on StackOverflow:
And that’s it! Happy hashing!