Skip to content
TheChatArchiveThe Chat Archive
Claudeclaude-2.1· AnthropicHugging Face Arena Human Preference 55K

Help me write code for computing md5 checksum

From a Chatbot Arena battleThe same person sent this prompt to two anonymous models at once. Compare it with gpt-3.5-turbo-1106’s answer.
  1. User

    Help me write code for computing md5 checksum

  2. Claude 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!

Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.