@@ -33,7 +33,6 @@ class SHA1Hash:
33
33
"""
34
34
Class to contain the entire pipeline for SHA1 Hashing Algorithm
35
35
"""
36
-
37
36
def __init__ (self , data ):
38
37
"""
39
38
Inititates the variables data and h. h is a list of 5 8-digit Hexadecimal
@@ -68,9 +67,8 @@ def split_blocks(self):
68
67
# @staticmethod
69
68
def expand_block (self , block ):
70
69
"""
71
- Takes block of 64 and returns list of length 80.
72
- It is really a static method but
73
- we need the rotate method inside, so we will have to use self
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
74
72
"""
75
73
w = list (struct .unpack ('>16L' , block )) + [0 ] * 64
76
74
for i in range (16 , 80 ):
@@ -79,7 +77,12 @@ def expand_block(self, block):
79
77
80
78
def final_hash (self ):
81
79
"""
82
- Calls all the other methods to process the input. Returns SHA1 hash
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.
83
86
"""
84
87
self .padded_data = self .padding ()
85
88
self .blocks = self .split_blocks ()
@@ -106,7 +109,6 @@ def final_hash(self):
106
109
self .h [2 ] + c & 0xffffffff ,\
107
110
self .h [3 ] + d & 0xffffffff ,\
108
111
self .h [4 ] + e & 0xffffffff
109
-
110
112
return '%08x%08x%08x%08x%08x' % tuple (self .h )
111
113
112
114
@@ -115,20 +117,17 @@ class SHA1HashTest(unittest.TestCase):
115
117
Test class for the SHA1Hash class. Inherits the TestCase class from unittest
116
118
"""
117
119
def testMatchHashes (self ):
118
- msg = bytes ("Hello World" , 'utf-8' )
120
+ msg = bytes ('Test String' , 'utf-8' )
119
121
self .assertEqual (SHA1Hash (msg ).final_hash (), hashlib .sha1 (msg ).hexdigest ())
120
122
121
- def run_test ():
122
- """
123
- Run the unit test. Pulled this out of main because we probably dont want to run
124
- the test each time.
125
- """
126
- unittest .main ()
127
123
128
124
def main ():
129
125
"""
130
- Provides option string or file to take input and prints the calculated SHA1 hash
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.
131
129
"""
130
+ # unittest.main()
132
131
parser = argparse .ArgumentParser (description = 'Process some strings or files' )
133
132
parser .add_argument ('--string' , dest = 'input_string' ,
134
133
default = 'Hello World!! Welcome to Cryptography' ,
@@ -143,5 +142,6 @@ def main():
143
142
hash_input = bytes (input_string , 'utf-8' )
144
143
print (SHA1Hash (hash_input ).final_hash ())
145
144
145
+
146
146
if __name__ == '__main__' :
147
147
main ()
0 commit comments