Help me write code for computing md5 checksum
- User
Help me write code for computing md5 checksum
Assistant
Here is some Python code to compute the MD5 checksum of a file:
import hashlib def get_md5(filename): hash_md5 = hashlib.md5() with open(filename, "rb") as f: for chunk in iter(lambda: f.read(4096), b""): hash_md5.update(chunk) return hash_md5.hexdigest() # Example usage filename = 'example.txt' md5_sum = get_md5(filename) print(md5_sum)To break this down:
- We import the hashlib module which contains the md5() function
- We create a md5 object to hold the state of the computation
- We open the file in binary read mode and iterate through it in 4KB chunks
- We update the md5 object with each chunk
- Finally we return the hex digest computed from the final md5 state
So this allows us to efficiently compute the MD5 checksum for files of any size.
Let me know if you need any clarification or have additional questions!
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.