From 4d255f4e9fbdbb07a1a4544b6bfc4148b2e49779 Mon Sep 17 00:00:00 2001 From: piyush-kgp Date: Sun, 23 Sep 2018 05:25:07 +0530 Subject: [PATCH 1/4] SHA1 skeleton code --- hashes/sha1.py | 90 ++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 90 insertions(+) create mode 100644 hashes/sha1.py diff --git a/hashes/sha1.py b/hashes/sha1.py new file mode 100644 index 000000000..009cba1a6 --- /dev/null +++ b/hashes/sha1.py @@ -0,0 +1,90 @@ +""" +Demonstrates implementation of SHA1 Hash function in a Python class and gives utilities +to find hash of string or hash of text from a file. +Usage: python sha1.py --string "Hello World Welcome to Cryptography" + pyhton sha1.py --file "hello_world.txt" + Without any arguments prints the hash of the string "Hello World" +Also contains a Test class to verify that the generated Hash is same as that +returned by the hashlib library +Reference: https://deadhacker.com/2006/02/21/sha-1-illustrated/ + +The Algorithm as described in the reference: +First we start with a message. The message is padded and the length of the message +is added to the end. It is then split into blocks of 512 bits. The blocks are then +processed one at a time. Each block must be expanded and compressed. +The value after each compression is added to a 160bit buffer called the current hash +state. After the last block is processed the current hash state is returned as +the final hash. +""" + +import argparse +import hashlib #hashlib is only used inside the Test class + +class SHA1: + """ + Class to contain the entire pipeline for SHA1 Hashing Algorithm + """ + def __init__(self, data): + self.data = data + self.current_hash = '' + + def padding(self): + return + + def split_block(self): + return + + def expand_block(self): + return + + def compress_block(self): + return + + def final_hash(self): + assert True #everything done till now + # return self.current_hash + return hashlib.sha1(bytes(self.data, 'utf-8')).hexdigest() + +class SHA1Test: + """ + Test class for the SHA1 class + """ + def __init__(self, data): + self.data = data + + def calculated_hash(self): + return SHA1(self.data).final_hash() + + def hashlib_hash(self): + return hashlib.sha1(self.data.byte_encode()).hexdigest() + + def byte_encode(self): + return bytes(self.data, 'utf-8') + + def match_hashes(self): + # self.assertEqual(self.calculated_hash(), self.hashlib_hash()) + return self.calculated_hash() == self.hashlib_hash() + +def run_test_case(hash_input = 'Hello World'): + """ + Pulled this out of main because we probably dont want to run the Unit Test + each time we want to calculate hash. + """ + print(SHA1Test(hash_input).match_hashes()) + + +def main(): + parser = argparse.ArgumentParser(description='Process some strings or files') + parser.add_argument('--string', dest='input_string', default='Hello World', + help='Hash the string') + parser.add_argument('--file', dest='input_file', help='Hash contents of a file') + args = parser.parse_args() + input_string = args.input_string + if args.input_file: + hash_input = open(args.input_file, 'r').read() + else: + hash_input = input_string + print(SHA1(hash_input).final_hash()) + +if __name__ == '__main__': + main() From 7f3895cdafa91330b618018b6cb09564b87f5b57 Mon Sep 17 00:00:00 2001 From: piyush-kgp Date: Sun, 23 Sep 2018 19:44:37 +0530 Subject: [PATCH 2/4] moved constants inside the class --- hashes/sha1.py | 27 +++++++++++++++++---------- 1 file changed, 17 insertions(+), 10 deletions(-) diff --git a/hashes/sha1.py b/hashes/sha1.py index 009cba1a6..7f87fe243 100644 --- a/hashes/sha1.py +++ b/hashes/sha1.py @@ -6,7 +6,6 @@ Usage: python sha1.py --string "Hello World Welcome to Cryptography" Without any arguments prints the hash of the string "Hello World" Also contains a Test class to verify that the generated Hash is same as that returned by the hashlib library -Reference: https://deadhacker.com/2006/02/21/sha-1-illustrated/ The Algorithm as described in the reference: First we start with a message. The message is padded and the length of the message @@ -15,15 +14,25 @@ processed one at a time. Each block must be expanded and compressed. The value after each compression is added to a 160bit buffer called the current hash state. After the last block is processed the current hash state is returned as the final hash. +Reference: https://deadhacker.com/2006/02/21/sha-1-illustrated/ """ import argparse import hashlib #hashlib is only used inside the Test class -class SHA1: + + +class SHA1Hash: """ Class to contain the entire pipeline for SHA1 Hashing Algorithm """ + + H0 - 01100111010001010010001100000001 + H1 - 11101111110011011010101110001001 + H2 - 10011000101110101101110011111110 + H3 - 00010000001100100101010001110110 + H4 - 11000011110100101110000111110000 + def __init__(self, data): self.data = data self.current_hash = '' @@ -41,11 +50,9 @@ class SHA1: return def final_hash(self): - assert True #everything done till now - # return self.current_hash - return hashlib.sha1(bytes(self.data, 'utf-8')).hexdigest() + return 'This is in my To Do list' -class SHA1Test: +class SHA1HashTest: """ Test class for the SHA1 class """ @@ -53,7 +60,7 @@ class SHA1Test: self.data = data def calculated_hash(self): - return SHA1(self.data).final_hash() + return SHA1Hash(self.data).final_hash() def hashlib_hash(self): return hashlib.sha1(self.data.byte_encode()).hexdigest() @@ -67,10 +74,10 @@ class SHA1Test: def run_test_case(hash_input = 'Hello World'): """ - Pulled this out of main because we probably dont want to run the Unit Test + Pulled this out of main because we probably dont want to run the Test each time we want to calculate hash. """ - print(SHA1Test(hash_input).match_hashes()) + print(SHA1HashTest(hash_input).match_hashes()) def main(): @@ -84,7 +91,7 @@ def main(): hash_input = open(args.input_file, 'r').read() else: hash_input = input_string - print(SHA1(hash_input).final_hash()) + print(SHA1Hash(hash_input).final_hash()) if __name__ == '__main__': main() From 965fdee22d93e06a4449bce8f6ec65e35f90d9ce Mon Sep 17 00:00:00 2001 From: piyush-kgp Date: Tue, 25 Sep 2018 19:21:29 +0530 Subject: [PATCH 3/4] SHA1 class completed, Test passed --- hashes/sha1.py | 142 +++++++++++++++++++++++++++++++++---------------- 1 file changed, 96 insertions(+), 46 deletions(-) diff --git a/hashes/sha1.py b/hashes/sha1.py index 7f87fe243..077433019 100644 --- a/hashes/sha1.py +++ b/hashes/sha1.py @@ -1,16 +1,22 @@ """ Demonstrates implementation of SHA1 Hash function in a Python class and gives utilities to find hash of string or hash of text from a file. -Usage: python sha1.py --string "Hello World Welcome to Cryptography" +Usage: python sha1.py --string "Hello World!!" pyhton sha1.py --file "hello_world.txt" - Without any arguments prints the hash of the string "Hello World" + When run without any arguments, it prints the hash of the string "Hello World!! Welcome to Cryptography" Also contains a Test class to verify that the generated Hash is same as that returned by the hashlib library +SHA1 hash or SHA1 sum of a string is a crytpographic function which means it is easy +to calculate forwards but extemely difficult to calculate backwards. What this means +is, you can easily calculate the hash of a string, but it is extremely difficult to +know the original string if you have its hash. This property is useful to communicate +securely, send encrypted messages and is very useful in payment systems, blockchain +and cryptocurrency etc. The Algorithm as described in the reference: First we start with a message. The message is padded and the length of the message -is added to the end. It is then split into blocks of 512 bits. The blocks are then -processed one at a time. Each block must be expanded and compressed. +is added to the end. It is then split into blocks of 512 bits or 64 bytes. The blocks +are then processed one at a time. Each block must be expanded and compressed. The value after each compression is added to a 160bit buffer called the current hash state. After the last block is processed the current hash state is returned as the final hash. @@ -18,79 +24,123 @@ Reference: https://deadhacker.com/2006/02/21/sha-1-illustrated/ """ import argparse +import struct import hashlib #hashlib is only used inside the Test class - +import unittest class SHA1Hash: """ Class to contain the entire pipeline for SHA1 Hashing Algorithm """ - - H0 - 01100111010001010010001100000001 - H1 - 11101111110011011010101110001001 - H2 - 10011000101110101101110011111110 - H3 - 00010000001100100101010001110110 - H4 - 11000011110100101110000111110000 def __init__(self, data): + """ + Inititates the variables data and h. h is a list of 5 8-digit Hexadecimal + numbers corresponding to (1732584193, 4023233417, 2562383102, 271733878, 3285377520) + respectively. We will start with this as a message digest. 0x is how you write + Hexadecimal numbers in Python + """ self.data = data - self.current_hash = '' + self.h = [0x67452301, 0xEFCDAB89, 0x98BADCFE, 0x10325476, 0xC3D2E1F0] + + @staticmethod + def rotate(n, b): + """ + Static method to be used inside other methods. Left rotates n by b. + """ + return ((n << b) | (n >> (32 - b))) & 0xffffffff def padding(self): - return + """ + Pads the input message with zeros so that padded_data has 64 bytes or 512 bits + """ + padding = b'\x80' + b'\x00'*(63 - (len(self.data) + 8) % 64) + padded_data = self.data + padding + struct.pack('>Q', 8 * len(self.data)) + return padded_data - def split_block(self): - return + def split_blocks(self): + """ + Returns a list of bytestrings each of length 64 + """ + return [self.padded_data[i:i+64] for i in range(0, len(self.padded_data), 64)] - def expand_block(self): - return - - def compress_block(self): - return + # @staticmethod + def expand_block(self, block): + """ + Takes block of 64 and returns list of length 80. + It is really a static method but + we need the rotate method inside, so we will have to use self + """ + w = list(struct.unpack('>16L', block)) + [0] * 64 + for i in range(16, 80): + w[i] = self.rotate((w[i-3] ^ w[i-8] ^ w[i-14] ^ w[i-16]), 1) + return w def final_hash(self): - return 'This is in my To Do list' + """ + Calls all the other methods to process the input. Returns SHA1 hash + """ + self.padded_data = self.padding() + self.blocks = self.split_blocks() + for block in self.blocks: + expanded_block = self.expand_block(block) + a, b, c, d, e = self.h + for i in range(0, 80): + if 0 <= i < 20: + f = (b & c) | ((~b) & d) + k = 0x5A827999 + elif 20 <= i < 40: + f = b ^ c ^ d + k = 0x6ED9EBA1 + elif 40 <= i < 60: + f = (b & c) | (b & d) | (c & d) + k = 0x8F1BBCDC + elif 60 <= i < 80: + f = b ^ c ^ d + k = 0xCA62C1D6 + a, b, c, d, e = self.rotate(a, 5) + f + e + k + expanded_block[i] & 0xffffffff,\ + a, self.rotate(b, 30), c, d + self.h = self.h[0] + a & 0xffffffff,\ + self.h[1] + b & 0xffffffff,\ + self.h[2] + c & 0xffffffff,\ + self.h[3] + d & 0xffffffff,\ + self.h[4] + e & 0xffffffff -class SHA1HashTest: + return '%08x%08x%08x%08x%08x' %tuple(self.h) + + +class SHA1HashTest(unittest.TestCase): """ - Test class for the SHA1 class + Test class for the SHA1Hash class. Inherits the TestCase class from unittest """ - def __init__(self, data): - self.data = data + def testMatchHashes(self): + msg = bytes("Hello World", 'utf-8') + self.assertEqual(SHA1Hash(msg).final_hash(), hashlib.sha1(msg).hexdigest()) - def calculated_hash(self): - return SHA1Hash(self.data).final_hash() - - def hashlib_hash(self): - return hashlib.sha1(self.data.byte_encode()).hexdigest() - - def byte_encode(self): - return bytes(self.data, 'utf-8') - - def match_hashes(self): - # self.assertEqual(self.calculated_hash(), self.hashlib_hash()) - return self.calculated_hash() == self.hashlib_hash() - -def run_test_case(hash_input = 'Hello World'): +def run_test(): """ - Pulled this out of main because we probably dont want to run the Test - each time we want to calculate hash. + Run the unit test. Pulled this out of main because we probably dont want to run + the test each time. """ - print(SHA1HashTest(hash_input).match_hashes()) - + unittest.main() def main(): + """ + Provides option string or file to take input and prints the calculated SHA1 hash + """ parser = argparse.ArgumentParser(description='Process some strings or files') - parser.add_argument('--string', dest='input_string', default='Hello World', + parser.add_argument('--string', dest='input_string', + default='Hello World!! Welcome to Cryptography', help='Hash the string') parser.add_argument('--file', dest='input_file', help='Hash contents of a file') args = parser.parse_args() input_string = args.input_string + #In any case hash input should be a bytestring if args.input_file: - hash_input = open(args.input_file, 'r').read() + hash_input = open(args.input_file, 'rb').read() else: - hash_input = input_string + hash_input = bytes(input_string, 'utf-8') print(SHA1Hash(hash_input).final_hash()) if __name__ == '__main__': From 59027e4bd54c9c46d531077b977fe1de434797bc Mon Sep 17 00:00:00 2001 From: piyush-kgp Date: Tue, 25 Sep 2018 22:56:24 +0530 Subject: [PATCH 4/4] Improved code documentation, removed uncalled function --- hashes/sha1.py | 28 ++++++++++++++-------------- 1 file changed, 14 insertions(+), 14 deletions(-) diff --git a/hashes/sha1.py b/hashes/sha1.py index 077433019..e34f87a76 100644 --- a/hashes/sha1.py +++ b/hashes/sha1.py @@ -33,7 +33,6 @@ class SHA1Hash: """ Class to contain the entire pipeline for SHA1 Hashing Algorithm """ - def __init__(self, data): """ Inititates the variables data and h. h is a list of 5 8-digit Hexadecimal @@ -68,9 +67,8 @@ class SHA1Hash: # @staticmethod def expand_block(self, block): """ - Takes block of 64 and returns list of length 80. - It is really a static method but - we need the rotate method inside, so we will have to use self + Takes a bytestring-block of length 64, unpacks it to a list of integers and returns a + list of 80 integers pafter some bit operations """ w = list(struct.unpack('>16L', block)) + [0] * 64 for i in range(16, 80): @@ -79,7 +77,12 @@ class SHA1Hash: def final_hash(self): """ - Calls all the other methods to process the input. Returns SHA1 hash + Calls all the other methods to process the input. Pads the data, then splits into + blocks and then does a series of operations for each block (including expansion). + For each block, the variable h that was initialized is copied to a,b,c,d,e + and these 5 variables a,b,c,d,e undergo several changes. After all the blocks are + processed, these 5 variables are pairwise added to h ie a to h[0], b to h[1] and so on. + This h becomes our final hash which is returned. """ self.padded_data = self.padding() self.blocks = self.split_blocks() @@ -106,7 +109,6 @@ class SHA1Hash: self.h[2] + c & 0xffffffff,\ self.h[3] + d & 0xffffffff,\ self.h[4] + e & 0xffffffff - return '%08x%08x%08x%08x%08x' %tuple(self.h) @@ -115,20 +117,17 @@ class SHA1HashTest(unittest.TestCase): Test class for the SHA1Hash class. Inherits the TestCase class from unittest """ def testMatchHashes(self): - msg = bytes("Hello World", 'utf-8') + msg = bytes('Test String', 'utf-8') self.assertEqual(SHA1Hash(msg).final_hash(), hashlib.sha1(msg).hexdigest()) -def run_test(): - """ - Run the unit test. Pulled this out of main because we probably dont want to run - the test each time. - """ - unittest.main() def main(): """ - Provides option string or file to take input and prints the calculated SHA1 hash + Provides option 'string' or 'file' to take input and prints the calculated SHA1 hash. + unittest.main() has been commented because we probably dont want to run + the test each time. """ + # unittest.main() parser = argparse.ArgumentParser(description='Process some strings or files') parser.add_argument('--string', dest='input_string', default='Hello World!! Welcome to Cryptography', @@ -143,5 +142,6 @@ def main(): hash_input = bytes(input_string, 'utf-8') print(SHA1Hash(hash_input).final_hash()) + if __name__ == '__main__': main()