|
| 1 | +# Author: M. Yathurshan |
| 2 | +# Black Formatter: True |
| 3 | + |
| 4 | +""" |
| 5 | +Implementation of SHA256 Hash function in a Python class and provides utilities |
| 6 | +to find hash of string or hash of text from a file. |
| 7 | +
|
| 8 | +Usage: python sha256.py --string "Hello World!!" |
| 9 | + python sha256.py --file "hello_world.txt" |
| 10 | + When run without any arguments, |
| 11 | + it prints the hash of the string "Hello World!! Welcome to Cryptography" |
| 12 | +
|
| 13 | +References: |
| 14 | +https://qvault.io/cryptography/how-sha-2-works-step-by-step-sha-256/ |
| 15 | +https://en.wikipedia.org/wiki/SHA-2 |
| 16 | +""" |
| 17 | + |
| 18 | +import argparse |
| 19 | +import struct |
| 20 | +import unittest |
| 21 | + |
| 22 | + |
| 23 | +class SHA256: |
| 24 | + """ |
| 25 | + Class to contain the entire pipeline for SHA1 Hashing Algorithm |
| 26 | +
|
| 27 | + >>> SHA256(b'Python').hash |
| 28 | + '18885f27b5af9012df19e496460f9294d5ab76128824c6f993787004f6d9a7db' |
| 29 | +
|
| 30 | + >>> SHA256(b'hello world').hash |
| 31 | + 'b94d27b9934d3e08a52e52d7da7dabfac484efe37a5380ee9088f7ace2efcde9' |
| 32 | + """ |
| 33 | + |
| 34 | + def __init__(self, data: bytes) -> None: |
| 35 | + self.data = data |
| 36 | + |
| 37 | + # Initialize hash values |
| 38 | + self.hashes = [ |
| 39 | + 0x6A09E667, |
| 40 | + 0xBB67AE85, |
| 41 | + 0x3C6EF372, |
| 42 | + 0xA54FF53A, |
| 43 | + 0x510E527F, |
| 44 | + 0x9B05688C, |
| 45 | + 0x1F83D9AB, |
| 46 | + 0x5BE0CD19, |
| 47 | + ] |
| 48 | + |
| 49 | + # Initialize round constants |
| 50 | + self.round_constants = [ |
| 51 | + 0x428A2F98, |
| 52 | + 0x71374491, |
| 53 | + 0xB5C0FBCF, |
| 54 | + 0xE9B5DBA5, |
| 55 | + 0x3956C25B, |
| 56 | + 0x59F111F1, |
| 57 | + 0x923F82A4, |
| 58 | + 0xAB1C5ED5, |
| 59 | + 0xD807AA98, |
| 60 | + 0x12835B01, |
| 61 | + 0x243185BE, |
| 62 | + 0x550C7DC3, |
| 63 | + 0x72BE5D74, |
| 64 | + 0x80DEB1FE, |
| 65 | + 0x9BDC06A7, |
| 66 | + 0xC19BF174, |
| 67 | + 0xE49B69C1, |
| 68 | + 0xEFBE4786, |
| 69 | + 0x0FC19DC6, |
| 70 | + 0x240CA1CC, |
| 71 | + 0x2DE92C6F, |
| 72 | + 0x4A7484AA, |
| 73 | + 0x5CB0A9DC, |
| 74 | + 0x76F988DA, |
| 75 | + 0x983E5152, |
| 76 | + 0xA831C66D, |
| 77 | + 0xB00327C8, |
| 78 | + 0xBF597FC7, |
| 79 | + 0xC6E00BF3, |
| 80 | + 0xD5A79147, |
| 81 | + 0x06CA6351, |
| 82 | + 0x14292967, |
| 83 | + 0x27B70A85, |
| 84 | + 0x2E1B2138, |
| 85 | + 0x4D2C6DFC, |
| 86 | + 0x53380D13, |
| 87 | + 0x650A7354, |
| 88 | + 0x766A0ABB, |
| 89 | + 0x81C2C92E, |
| 90 | + 0x92722C85, |
| 91 | + 0xA2BFE8A1, |
| 92 | + 0xA81A664B, |
| 93 | + 0xC24B8B70, |
| 94 | + 0xC76C51A3, |
| 95 | + 0xD192E819, |
| 96 | + 0xD6990624, |
| 97 | + 0xF40E3585, |
| 98 | + 0x106AA070, |
| 99 | + 0x19A4C116, |
| 100 | + 0x1E376C08, |
| 101 | + 0x2748774C, |
| 102 | + 0x34B0BCB5, |
| 103 | + 0x391C0CB3, |
| 104 | + 0x4ED8AA4A, |
| 105 | + 0x5B9CCA4F, |
| 106 | + 0x682E6FF3, |
| 107 | + 0x748F82EE, |
| 108 | + 0x78A5636F, |
| 109 | + 0x84C87814, |
| 110 | + 0x8CC70208, |
| 111 | + 0x90BEFFFA, |
| 112 | + 0xA4506CEB, |
| 113 | + 0xBEF9A3F7, |
| 114 | + 0xC67178F2, |
| 115 | + ] |
| 116 | + |
| 117 | + self.preprocessed_data = self.preprocessing(self.data) |
| 118 | + self.final_hash() |
| 119 | + |
| 120 | + @staticmethod |
| 121 | + def preprocessing(data: bytes) -> bytes: |
| 122 | + padding = b"\x80" + (b"\x00" * (63 - (len(data) + 8) % 64)) |
| 123 | + big_endian_integer = struct.pack(">Q", (len(data) * 8)) |
| 124 | + return data + padding + big_endian_integer |
| 125 | + |
| 126 | + def final_hash(self) -> None: |
| 127 | + # Convert into blocks of 64 bytes |
| 128 | + self.blocks = [ |
| 129 | + self.preprocessed_data[x : x + 64] |
| 130 | + for x in range(0, len(self.preprocessed_data), 64) |
| 131 | + ] |
| 132 | + |
| 133 | + for block in self.blocks: |
| 134 | + # Convert the given block into a list of 4 byte integers |
| 135 | + words = list(struct.unpack(">16L", block)) |
| 136 | + # add 48 0-ed integers |
| 137 | + words += [0] * 48 |
| 138 | + |
| 139 | + a, b, c, d, e, f, g, h = self.hashes |
| 140 | + |
| 141 | + for index in range(0, 64): |
| 142 | + if index > 15: |
| 143 | + # modify the zero-ed indexes at the end of the array |
| 144 | + s0 = ( |
| 145 | + self.ror(words[index - 15], 7) |
| 146 | + ^ self.ror(words[index - 15], 18) |
| 147 | + ^ (words[index - 15] >> 3) |
| 148 | + ) |
| 149 | + s1 = ( |
| 150 | + self.ror(words[index - 2], 17) |
| 151 | + ^ self.ror(words[index - 2], 19) |
| 152 | + ^ (words[index - 2] >> 10) |
| 153 | + ) |
| 154 | + |
| 155 | + words[index] = ( |
| 156 | + words[index - 16] + s0 + words[index - 7] + s1 |
| 157 | + ) % 0x100000000 |
| 158 | + |
| 159 | + # Compression |
| 160 | + S1 = self.ror(e, 6) ^ self.ror(e, 11) ^ self.ror(e, 25) |
| 161 | + ch = (e & f) ^ ((~e & (0xFFFFFFFF)) & g) |
| 162 | + temp1 = ( |
| 163 | + h + S1 + ch + self.round_constants[index] + words[index] |
| 164 | + ) % 0x100000000 |
| 165 | + S0 = self.ror(a, 2) ^ self.ror(a, 13) ^ self.ror(a, 22) |
| 166 | + maj = (a & b) ^ (a & c) ^ (b & c) |
| 167 | + temp2 = (S0 + maj) % 0x100000000 |
| 168 | + |
| 169 | + h, g, f, e, d, c, b, a = ( |
| 170 | + g, |
| 171 | + f, |
| 172 | + e, |
| 173 | + ((d + temp1) % 0x100000000), |
| 174 | + c, |
| 175 | + b, |
| 176 | + a, |
| 177 | + ((temp1 + temp2) % 0x100000000), |
| 178 | + ) |
| 179 | + |
| 180 | + mutated_hash_values = [a, b, c, d, e, f, g, h] |
| 181 | + |
| 182 | + # Modify final values |
| 183 | + self.hashes = [ |
| 184 | + ((element + mutated_hash_values[index]) % 0x100000000) |
| 185 | + for index, element in enumerate(self.hashes) |
| 186 | + ] |
| 187 | + |
| 188 | + self.hash = "".join([hex(value)[2:].zfill(8) for value in self.hashes]) |
| 189 | + |
| 190 | + def ror(self, value: int, rotations: int) -> int: |
| 191 | + """ |
| 192 | + Right rotate a given unsigned number by a certain amount of rotations |
| 193 | + """ |
| 194 | + return 0xFFFFFFFF & (value << (32 - rotations)) | (value >> rotations) |
| 195 | + |
| 196 | + |
| 197 | +class SHA256HashTest(unittest.TestCase): |
| 198 | + """ |
| 199 | + Test class for the SHA256 class. Inherits the TestCase class from unittest |
| 200 | + """ |
| 201 | + |
| 202 | + def test_match_hashes(self) -> None: |
| 203 | + import hashlib |
| 204 | + |
| 205 | + msg = bytes("Test String", "utf-8") |
| 206 | + self.assertEqual(SHA256(msg).hash, hashlib.sha256(msg).hexdigest()) |
| 207 | + |
| 208 | + |
| 209 | +def main() -> None: |
| 210 | + """ |
| 211 | + Provides option 'string' or 'file' to take input |
| 212 | + and prints the calculated SHA-256 hash |
| 213 | + """ |
| 214 | + |
| 215 | + # unittest.main() |
| 216 | + |
| 217 | + import doctest |
| 218 | + |
| 219 | + doctest.testmod() |
| 220 | + |
| 221 | + parser = argparse.ArgumentParser() |
| 222 | + parser.add_argument( |
| 223 | + "-s", |
| 224 | + "--string", |
| 225 | + dest="input_string", |
| 226 | + default="Hello World!! Welcome to Cryptography", |
| 227 | + help="Hash the string", |
| 228 | + ) |
| 229 | + parser.add_argument( |
| 230 | + "-f", "--file", dest="input_file", help="Hash contents of a file" |
| 231 | + ) |
| 232 | + |
| 233 | + args = parser.parse_args() |
| 234 | + |
| 235 | + input_string = args.input_string |
| 236 | + |
| 237 | + # hash input should be a bytestring |
| 238 | + if args.input_file: |
| 239 | + with open(args.input_file, "rb") as f: |
| 240 | + hash_input = f.read() |
| 241 | + else: |
| 242 | + hash_input = bytes(input_string, "utf-8") |
| 243 | + |
| 244 | + print(SHA256(hash_input).hash) |
| 245 | + |
| 246 | + |
| 247 | +if __name__ == "__main__": |
| 248 | + main() |
0 commit comments