Python Script to Calculate Checksum of File

I was in desperate need of a utility to calculate checksums of certain files. I was running Windows, and did not want to install a third-party tool to do this for me. I did have Python3 installed and decided to write a script myself to give me checksum for a file. But the script grew bigger as I wanted more and more features. Eventually, the script does the following: (1) calculate and display checksum (MD5, SHA1, SHA224, SHA256, SHA384, SHA512) for a file; (2) calculate and display size (bytes, KB, MB, GB) in terms of storage (1024 bytes = 1 kilo byte or KB); and (3) calculate and display size (bits, Kb, Mb, Gb) in terms of data transfer over network. The script is as below. Actually, if you don’t want to know the internals of the script, just use it as a utility. You’ll need Python 3 and make sure you have argparse module installed (Debian: sudo aptitude install python-argparse).

# This script reads a file and displays its size and checksums
def get_custom_checksum(input_file_name):
    from datetime import datetime
    starttime =
	# START: Actual checksum calculation
    from hashlib import md5, sha1, sha224, sha384, sha256, sha512
    #chunk_size = 1 # 1 byte -- NOT RECOMENDED -- USE AT LEAST 1KB. When 1KB takes 1 min to run, 1B takes 19 minutes to run
    #chunk_size = 1024 # 1 KB
    chunk_size = 1048576 # 1024 B * 1024 B = 1048576 B = 1 MB
    file_md5_checksum = md5()
    file_sha1_checksum = sha1()
    file_sha224_checksum = sha224()
    file_sha256_checksum = sha256()
    file_sha384_checksum = sha384()
    file_sha512_checksum = sha512()
        with open(input_file_name, "rb") as f:
            byte =
            previous_byte = byte
            byte_size = len(byte)
            file_read_iterations = 1
            while byte:
                previous_byte = byte
                byte =
                byte_size += len(byte)
                file_read_iterations += 1
    except IOError:
        print ('File could not be opened: %s' % (input_file_name))
	# END: Actual checksum calculation
    # For storage purposes, 1024 bytes = 1 kilobyte
    # For data transfer purposes, 1000 bits = 1 kilobit
    kilo_byte_size = byte_size/1024
    mega_byte_size = kilo_byte_size/1024
    giga_byte_size = mega_byte_size/1024
    bit_size = byte_size*8
    kilo_bit_size = bit_size/1000
    mega_bit_size = kilo_bit_size/1000
    giga_bit_size = mega_bit_size/1000
    last_chunk_size = len(previous_byte)
    stoptime =
    processtime = stoptime-starttime
    custom_checksum_profile = {
        'starttime': starttime,
        'byte_size': byte_size,
        'kilo_byte_size': kilo_byte_size,
        'mega_byte_size': mega_byte_size,
        'giga_byte_size': giga_byte_size,
        'bit_size': bit_size,
        'kilo_bit_size': kilo_bit_size,
        'mega_bit_size': mega_bit_size,
        'giga_bit_size': giga_bit_size,
        'file_read_iterations': file_read_iterations,
        'last_chunk_size': last_chunk_size,
        'md5_checksum': file_md5_checksum.hexdigest(),
        'sha1_checksum': file_sha1_checksum.hexdigest(),
        'sha224_checksum': file_sha224_checksum.hexdigest(),
        'sha256_checksum': file_sha256_checksum.hexdigest(),
        'sha384_checksum': file_sha384_checksum.hexdigest(),
        'sha512_checksum': file_sha512_checksum.hexdigest(),
        'stoptime': stoptime,
        'processtime': processtime,
    return custom_checksum_profile

    # Thanks to the following resources for helping with this function

def print_custom_checksum(input_file_name):
    custom_checksum_profile = get_custom_checksum(input_file_name)
        print ('Start Time            :', custom_checksum_profile['starttime'])
        print ('File Size (bytes)     :', custom_checksum_profile['byte_size'])
        print ('File Size (kilobytes) :', custom_checksum_profile['kilo_byte_size'])
        print ('File Size (megabytes) :', custom_checksum_profile['mega_byte_size'])
        print ('File Size (gigabytes) :', custom_checksum_profile['giga_byte_size'])
        print ('File Size (bits)      :', custom_checksum_profile['bit_size'])
        print ('File Size (kilobits)  :', custom_checksum_profile['kilo_bit_size'])
        print ('File Size (megabits)  :', custom_checksum_profile['mega_bit_size'])
        print ('File Size (gigabits)  :', custom_checksum_profile['giga_bit_size'])
        print ('File Read Iterations  :', custom_checksum_profile['file_read_iterations'])
        print ('Last Chunk (bytes)    :', custom_checksum_profile['last_chunk_size'])
        print ('MD5                   :', custom_checksum_profile['md5_checksum'])
        print ('SHA1                  :', custom_checksum_profile['sha1_checksum'])
        print ('SHA224                :', custom_checksum_profile['sha224_checksum'])
        print ('SHA256                :', custom_checksum_profile['sha256_checksum'])
        print ('SHA384                :', custom_checksum_profile['sha384_checksum'])
        print ('SHA512                :', custom_checksum_profile['sha512_checksum'])
        print ('Stop Time             :', custom_checksum_profile['stoptime'])
        print ('Processing Time       :', custom_checksum_profile['processtime'])
    except TypeError: #  'NoneType' object is not subscriptable --- basically this should happen when the input file could not be opened
    # csv output

import argparse
parser = argparse.ArgumentParser(description='Determine and print various checksums of an input file and its size. Supported checksums are MD5, SHA1, SHA224, SHA256, SHA384, and SHA512.', version=script_version)
parser.add_argument('-f', '--file', metavar='in-file', action='store', dest='file_name', type=str, required=True, help='Name of file for which the checksum needs to be calculated')
args = parser.parse_args()
print ('Processing File       :', args.file_name)

# Thanks to the following resources for helping with argparse

4 Responses to Python Script to Calculate Checksum of File

  1. alex says:

    This is a very nice job.
    Thank you…

  2. Anonymous says:

    ./ line 2: syntax error near unexpected token `(‘
    ./ line 2: `def get_custom_checksum(input_file_name):’

  3. TechKarma says:

    Nice, useful script. Thanks for sharing it.

  4. Pingback: Confluence: Harman Cloud Platform

%d bloggers like this: