Home
Python
hashlib Examples (sha256, md5)
This page was last reviewed on Mar 20, 2024.
Dot Net Perls
Hashlib. Suppose we have a file and want a unique value that represents the contents of that file. With hashlib, we can use hash algorithms like SHA256 and MD5 to get hashes.
File
bytes
For hashlib, we are trying to get unique hashes, and these may be slow to compute. Collections like the dictionary use different hashes that are faster to compute.
Dictionary
Example. This Python example program first creates a binary file called "example.txt" that contains some data. It then tries to hash this file's contents.
Version 1 We can compute a hash by creating the required hash object, calling update() with bytes, and then calling hexdigest().
Version 2 If we read in a file as binary (with the "b" flag) we can pass its data to the update() method as well.
Version 3 With file_digest, we have a more efficient way to compute a hash for a file object.
Version 4 If we want to use another hash algorithm like BLAKE2, that is possible by passing in a "blake2s" string to file_digest.
Version 5 The MD5 hash algorithm is not secure, but it is still sometimes used, and we can compute MD5 hashes with file_digest as well.
import hashlib # Create the needed example file. with open("example.txt", "wb") as f: f.write(b"Some example text") # Version 1: get sha256 hash from bytes. m = hashlib.sha256() m.update(b"Some example text") print(m.hexdigest()) # Version 2: get sha256 hash from file using update. with open("example.txt", "rb") as f: m2 = hashlib.sha256() data = f.read() m2.update(data) print(m2.hexdigest()) # Version 3: get sha256 hash from file with file_digest. with open("example.txt", "rb") as f: m3 = hashlib.file_digest(f, "sha256") print(m3.hexdigest()) # Version 4: use blake2s hash. with open("example.txt", "rb") as f: m4 = hashlib.file_digest(f, "blake2s") print(m4.hexdigest()) # Version 5: use md5 hash. with open("example.txt", "rb") as f: m5 = hashlib.file_digest(f, "md5") print(m5.hexdigest())
ba94b1c49abbd67b58019d6295f070913f499a774173c1b951b28525b0fb7193 ba94b1c49abbd67b58019d6295f070913f499a774173c1b951b28525b0fb7193 ba94b1c49abbd67b58019d6295f070913f499a774173c1b951b28525b0fb7193 cd154ac1356589b52ee8ad35899e36129bd62dcbf741dc7c08748fd10900e555 5b3a9b7b92bd8217bf5ffbd301043cea
Hashing the contents of files lets us compute 2 files for equality without knowing their contents. This can be a performance optimization, or help with ensuring data has not been tampered with.
Dot Net Perls is a collection of tested code examples. Pages are continually updated to stay current, with code correctness a top priority.
Sam Allen is passionate about computer languages. In the past, his work has been recommended by Apple and Microsoft and he has studied computers at a selective university in the United States.
This page was last updated on Mar 20, 2024 (new).
Home
Changes
© 2007-2024 Sam Allen.