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 latter is more idiomatic and makes use of the built-in function iter(callable, sentinel) (documentation here). By default the algorithm used in order to do the hash is SHA1, but another one can be easily used just passing a new instance of it to the hasher_alg parameter.
Basic hash utils file
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 | |
Idiomatic hash utils file
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 | |
I’ve based this post mainly from these answers given on StackOverflow:
And that’s it! Happy hashing!