moved constants inside the class

This commit is contained in:
piyush-kgp 2018-09-23 19:44:37 +05:30
parent 4d255f4e9f
commit 7f3895cdaf

View File

@ -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" 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 Also contains a Test class to verify that the generated Hash is same as that
returned by the hashlib library returned by the hashlib library
Reference: https://deadhacker.com/2006/02/21/sha-1-illustrated/
The Algorithm as described in the reference: The Algorithm as described in the reference:
First we start with a message. The message is padded and the length of the message 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 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 state. After the last block is processed the current hash state is returned as
the final hash. the final hash.
Reference: https://deadhacker.com/2006/02/21/sha-1-illustrated/
""" """
import argparse import argparse
import hashlib #hashlib is only used inside the Test class import hashlib #hashlib is only used inside the Test class
class SHA1:
class SHA1Hash:
""" """
Class to contain the entire pipeline for SHA1 Hashing Algorithm Class to contain the entire pipeline for SHA1 Hashing Algorithm
""" """
H0 - 01100111010001010010001100000001
H1 - 11101111110011011010101110001001
H2 - 10011000101110101101110011111110
H3 - 00010000001100100101010001110110
H4 - 11000011110100101110000111110000
def __init__(self, data): def __init__(self, data):
self.data = data self.data = data
self.current_hash = '' self.current_hash = ''
@ -41,11 +50,9 @@ class SHA1:
return return
def final_hash(self): def final_hash(self):
assert True #everything done till now return 'This is in my To Do list'
# return self.current_hash
return hashlib.sha1(bytes(self.data, 'utf-8')).hexdigest()
class SHA1Test: class SHA1HashTest:
""" """
Test class for the SHA1 class Test class for the SHA1 class
""" """
@ -53,7 +60,7 @@ class SHA1Test:
self.data = data self.data = data
def calculated_hash(self): def calculated_hash(self):
return SHA1(self.data).final_hash() return SHA1Hash(self.data).final_hash()
def hashlib_hash(self): def hashlib_hash(self):
return hashlib.sha1(self.data.byte_encode()).hexdigest() return hashlib.sha1(self.data.byte_encode()).hexdigest()
@ -67,10 +74,10 @@ class SHA1Test:
def run_test_case(hash_input = 'Hello World'): 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. each time we want to calculate hash.
""" """
print(SHA1Test(hash_input).match_hashes()) print(SHA1HashTest(hash_input).match_hashes())
def main(): def main():
@ -84,7 +91,7 @@ def main():
hash_input = open(args.input_file, 'r').read() hash_input = open(args.input_file, 'r').read()
else: else:
hash_input = input_string hash_input = input_string
print(SHA1(hash_input).final_hash()) print(SHA1Hash(hash_input).final_hash())
if __name__ == '__main__': if __name__ == '__main__':
main() main()