|
| 1 | +""" |
| 2 | +Demonstrates implementation of SHA1 Hash function in a Python class and gives utilities |
| 3 | +to find hash of string or hash of text from a file. |
| 4 | +Usage: python sha1.py --string "Hello World!!" |
| 5 | + pyhton sha1.py --file "hello_world.txt" |
| 6 | + When run without any arguments, it prints the hash of the string "Hello World!! Welcome to Cryptography" |
| 7 | +Also contains a Test class to verify that the generated Hash is same as that |
| 8 | +returned by the hashlib library |
| 9 | +
|
| 10 | +SHA1 hash or SHA1 sum of a string is a crytpographic function which means it is easy |
| 11 | +to calculate forwards but extemely difficult to calculate backwards. What this means |
| 12 | +is, you can easily calculate the hash of a string, but it is extremely difficult to |
| 13 | +know the original string if you have its hash. This property is useful to communicate |
| 14 | +securely, send encrypted messages and is very useful in payment systems, blockchain |
| 15 | +and cryptocurrency etc. |
| 16 | +The Algorithm as described in the reference: |
| 17 | +First we start with a message. The message is padded and the length of the message |
| 18 | +is added to the end. It is then split into blocks of 512 bits or 64 bytes. The blocks |
| 19 | +are then processed one at a time. Each block must be expanded and compressed. |
| 20 | +The value after each compression is added to a 160bit buffer called the current hash |
| 21 | +state. After the last block is processed the current hash state is returned as |
| 22 | +the final hash. |
| 23 | +Reference: https://deadhacker.com/2006/02/21/sha-1-illustrated/ |
| 24 | +""" |
| 25 | + |
| 26 | +import argparse |
| 27 | +import struct |
| 28 | +import hashlib #hashlib is only used inside the Test class |
| 29 | +import unittest |
| 30 | + |
| 31 | + |
| 32 | +class SHA1Hash: |
| 33 | + """ |
| 34 | + Class to contain the entire pipeline for SHA1 Hashing Algorithm |
| 35 | + """ |
| 36 | + def __init__(self, data): |
| 37 | + """ |
| 38 | + Inititates the variables data and h. h is a list of 5 8-digit Hexadecimal |
| 39 | + numbers corresponding to (1732584193, 4023233417, 2562383102, 271733878, 3285377520) |
| 40 | + respectively. We will start with this as a message digest. 0x is how you write |
| 41 | + Hexadecimal numbers in Python |
| 42 | + """ |
| 43 | + self.data = data |
| 44 | + self.h = [0x67452301, 0xEFCDAB89, 0x98BADCFE, 0x10325476, 0xC3D2E1F0] |
| 45 | + |
| 46 | + @staticmethod |
| 47 | + def rotate(n, b): |
| 48 | + """ |
| 49 | + Static method to be used inside other methods. Left rotates n by b. |
| 50 | + """ |
| 51 | + return ((n << b) | (n >> (32 - b))) & 0xffffffff |
| 52 | + |
| 53 | + def padding(self): |
| 54 | + """ |
| 55 | + Pads the input message with zeros so that padded_data has 64 bytes or 512 bits |
| 56 | + """ |
| 57 | + padding = b'\x80' + b'\x00'*(63 - (len(self.data) + 8) % 64) |
| 58 | + padded_data = self.data + padding + struct.pack('>Q', 8 * len(self.data)) |
| 59 | + return padded_data |
| 60 | + |
| 61 | + def split_blocks(self): |
| 62 | + """ |
| 63 | + Returns a list of bytestrings each of length 64 |
| 64 | + """ |
| 65 | + return [self.padded_data[i:i+64] for i in range(0, len(self.padded_data), 64)] |
| 66 | + |
| 67 | + # @staticmethod |
| 68 | + def expand_block(self, block): |
| 69 | + """ |
| 70 | + Takes a bytestring-block of length 64, unpacks it to a list of integers and returns a |
| 71 | + list of 80 integers pafter some bit operations |
| 72 | + """ |
| 73 | + w = list(struct.unpack('>16L', block)) + [0] * 64 |
| 74 | + for i in range(16, 80): |
| 75 | + w[i] = self.rotate((w[i-3] ^ w[i-8] ^ w[i-14] ^ w[i-16]), 1) |
| 76 | + return w |
| 77 | + |
| 78 | + def final_hash(self): |
| 79 | + """ |
| 80 | + Calls all the other methods to process the input. Pads the data, then splits into |
| 81 | + blocks and then does a series of operations for each block (including expansion). |
| 82 | + For each block, the variable h that was initialized is copied to a,b,c,d,e |
| 83 | + and these 5 variables a,b,c,d,e undergo several changes. After all the blocks are |
| 84 | + processed, these 5 variables are pairwise added to h ie a to h[0], b to h[1] and so on. |
| 85 | + This h becomes our final hash which is returned. |
| 86 | + """ |
| 87 | + self.padded_data = self.padding() |
| 88 | + self.blocks = self.split_blocks() |
| 89 | + for block in self.blocks: |
| 90 | + expanded_block = self.expand_block(block) |
| 91 | + a, b, c, d, e = self.h |
| 92 | + for i in range(0, 80): |
| 93 | + if 0 <= i < 20: |
| 94 | + f = (b & c) | ((~b) & d) |
| 95 | + k = 0x5A827999 |
| 96 | + elif 20 <= i < 40: |
| 97 | + f = b ^ c ^ d |
| 98 | + k = 0x6ED9EBA1 |
| 99 | + elif 40 <= i < 60: |
| 100 | + f = (b & c) | (b & d) | (c & d) |
| 101 | + k = 0x8F1BBCDC |
| 102 | + elif 60 <= i < 80: |
| 103 | + f = b ^ c ^ d |
| 104 | + k = 0xCA62C1D6 |
| 105 | + a, b, c, d, e = self.rotate(a, 5) + f + e + k + expanded_block[i] & 0xffffffff,\ |
| 106 | + a, self.rotate(b, 30), c, d |
| 107 | + self.h = self.h[0] + a & 0xffffffff,\ |
| 108 | + self.h[1] + b & 0xffffffff,\ |
| 109 | + self.h[2] + c & 0xffffffff,\ |
| 110 | + self.h[3] + d & 0xffffffff,\ |
| 111 | + self.h[4] + e & 0xffffffff |
| 112 | + return '%08x%08x%08x%08x%08x' %tuple(self.h) |
| 113 | + |
| 114 | + |
| 115 | +class SHA1HashTest(unittest.TestCase): |
| 116 | + """ |
| 117 | + Test class for the SHA1Hash class. Inherits the TestCase class from unittest |
| 118 | + """ |
| 119 | + def testMatchHashes(self): |
| 120 | + msg = bytes('Test String', 'utf-8') |
| 121 | + self.assertEqual(SHA1Hash(msg).final_hash(), hashlib.sha1(msg).hexdigest()) |
| 122 | + |
| 123 | + |
| 124 | +def main(): |
| 125 | + """ |
| 126 | + Provides option 'string' or 'file' to take input and prints the calculated SHA1 hash. |
| 127 | + unittest.main() has been commented because we probably dont want to run |
| 128 | + the test each time. |
| 129 | + """ |
| 130 | + # unittest.main() |
| 131 | + parser = argparse.ArgumentParser(description='Process some strings or files') |
| 132 | + parser.add_argument('--string', dest='input_string', |
| 133 | + default='Hello World!! Welcome to Cryptography', |
| 134 | + help='Hash the string') |
| 135 | + parser.add_argument('--file', dest='input_file', help='Hash contents of a file') |
| 136 | + args = parser.parse_args() |
| 137 | + input_string = args.input_string |
| 138 | + #In any case hash input should be a bytestring |
| 139 | + if args.input_file: |
| 140 | + hash_input = open(args.input_file, 'rb').read() |
| 141 | + else: |
| 142 | + hash_input = bytes(input_string, 'utf-8') |
| 143 | + print(SHA1Hash(hash_input).final_hash()) |
| 144 | + |
| 145 | + |
| 146 | +if __name__ == '__main__': |
| 147 | + main() |
0 commit comments