Skip to content

Commit 59027e4

Browse files
committed
Improved code documentation, removed uncalled function
1 parent 965fdee commit 59027e4

File tree

1 file changed

+14
-14
lines changed

1 file changed

+14
-14
lines changed

hashes/sha1.py

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,6 @@ class SHA1Hash:
3333
"""
3434
Class to contain the entire pipeline for SHA1 Hashing Algorithm
3535
"""
36-
3736
def __init__(self, data):
3837
"""
3938
Inititates the variables data and h. h is a list of 5 8-digit Hexadecimal
@@ -68,9 +67,8 @@ def split_blocks(self):
6867
# @staticmethod
6968
def expand_block(self, block):
7069
"""
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
7472
"""
7573
w = list(struct.unpack('>16L', block)) + [0] * 64
7674
for i in range(16, 80):
@@ -79,7 +77,12 @@ def expand_block(self, block):
7977

8078
def final_hash(self):
8179
"""
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.
8386
"""
8487
self.padded_data = self.padding()
8588
self.blocks = self.split_blocks()
@@ -106,7 +109,6 @@ def final_hash(self):
106109
self.h[2] + c & 0xffffffff,\
107110
self.h[3] + d & 0xffffffff,\
108111
self.h[4] + e & 0xffffffff
109-
110112
return '%08x%08x%08x%08x%08x' %tuple(self.h)
111113

112114

@@ -115,20 +117,17 @@ class SHA1HashTest(unittest.TestCase):
115117
Test class for the SHA1Hash class. Inherits the TestCase class from unittest
116118
"""
117119
def testMatchHashes(self):
118-
msg = bytes("Hello World", 'utf-8')
120+
msg = bytes('Test String', 'utf-8')
119121
self.assertEqual(SHA1Hash(msg).final_hash(), hashlib.sha1(msg).hexdigest())
120122

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()
127123

128124
def main():
129125
"""
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.
131129
"""
130+
# unittest.main()
132131
parser = argparse.ArgumentParser(description='Process some strings or files')
133132
parser.add_argument('--string', dest='input_string',
134133
default='Hello World!! Welcome to Cryptography',
@@ -143,5 +142,6 @@ def main():
143142
hash_input = bytes(input_string, 'utf-8')
144143
print(SHA1Hash(hash_input).final_hash())
145144

145+
146146
if __name__ == '__main__':
147147
main()

0 commit comments

Comments
 (0)