Python hashlib

From wikinotes

hash functions are used to map data of a variable size into data of a fixed size. hashing functions generally accommodate for collisions. hashing is not the same as checksumming, which is used to verify data integrity. (although I am not entirely clear on the distinction - they seem to share algorithms).


Hashing Files

## Simple HASH of a file's contents.
import hashlib

hash_md5 = hashlib.md5( open('myfile.txt', 'rb').read() )
hash     = hash.hexdigest()
## Scalable method of getting a HASH of
## a file's contents (if cannot fit file into memory)
import hashlib

hash_md5 = hashlib.md5()

with open(fname, "rb") as f:
	for chunk in iter(lambda: f.read(4096), b""):
		hash_md5.update(chunk)

hash = hash_md5.hexdigest()