How to Calculate File Checksum in Python

Checksum is finger print of a file. If two files have the same checksum, they are identical files.

There are several types of checksum and the ones we use the most are MD5 and SHA256 based on my experience. Here is the sample code in Python.

import hashlib

class playground(unittest.TestCase):
    def test_sha256(self):
        file_name = 'test.jpg'
        with open(file_name, 'rb') as file:
            bytes = file.read()
            hash = hashlib.sha256(bytes).hexdigest()
            print(hash)

    def test_md5(self):
        file_name = 'test.jpg'
        with open(file_name, 'rb') as file:
            print(hashlib.md5(file.read()).hexdigest())

As a result, the SHA256 of test.jpg is c91834ce2d9e57edb6ccd118c10e5fb3b0eacfb8a8ecda73ae6680ced50009de and MD5 is 61a6ef0dd8c606fc0eb4c676f0e4296a

You don’t need Python to calculate these checksums. Here is the example you can run on your Mac or Linux. I’m sure you can run it on your Linux Subsystem on Windows as well.

❯ md5 test.jpg
MD5 (test.jpg) = 61a6ef0dd8c606fc0eb4c676f0e4296a

❯ openssl dgst -sha256 test.jpg
SHA256(test.jpg)= c91834ce2d9e57edb6ccd118c10e5fb3b0eacfb8a8ecda73ae6680ced50009de

Leave a Reply

Your email address will not be published.