Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit 7f3895c

Browse files
committed
moved constants inside the class
1 parent 4d255f4 commit 7f3895c

File tree

1 file changed

+17
-10
lines changed

1 file changed

+17
-10
lines changed

hashes/sha1.py

Lines changed: 17 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
Without any arguments prints the hash of the string "Hello World"
77
Also contains a Test class to verify that the generated Hash is same as that
88
returned by the hashlib library
9-
Reference: https://deadhacker.com/2006/02/21/sha-1-illustrated/
109
1110
The Algorithm as described in the reference:
1211
First we start with a message. The message is padded and the length of the message
@@ -15,15 +14,25 @@
1514
The value after each compression is added to a 160bit buffer called the current hash
1615
state. After the last block is processed the current hash state is returned as
1716
the final hash.
17+
Reference: https://deadhacker.com/2006/02/21/sha-1-illustrated/
1818
"""
1919

2020
import argparse
2121
import hashlib #hashlib is only used inside the Test class
2222

23-
class SHA1:
23+
24+
25+
class SHA1Hash:
2426
"""
2527
Class to contain the entire pipeline for SHA1 Hashing Algorithm
2628
"""
29+
30+
H0 - 01100111010001010010001100000001
31+
H1 - 11101111110011011010101110001001
32+
H2 - 10011000101110101101110011111110
33+
H3 - 00010000001100100101010001110110
34+
H4 - 11000011110100101110000111110000
35+
2736
def __init__(self, data):
2837
self.data = data
2938
self.current_hash = ''
@@ -41,19 +50,17 @@ def compress_block(self):
4150
return
4251

4352
def final_hash(self):
44-
assert True #everything done till now
45-
# return self.current_hash
46-
return hashlib.sha1(bytes(self.data, 'utf-8')).hexdigest()
53+
return 'This is in my To Do list'
4754

48-
class SHA1Test:
55+
class SHA1HashTest:
4956
"""
5057
Test class for the SHA1 class
5158
"""
5259
def __init__(self, data):
5360
self.data = data
5461

5562
def calculated_hash(self):
56-
return SHA1(self.data).final_hash()
63+
return SHA1Hash(self.data).final_hash()
5764

5865
def hashlib_hash(self):
5966
return hashlib.sha1(self.data.byte_encode()).hexdigest()
@@ -67,10 +74,10 @@ def match_hashes(self):
6774

6875
def run_test_case(hash_input = 'Hello World'):
6976
"""
70-
Pulled this out of main because we probably dont want to run the Unit Test
77+
Pulled this out of main because we probably dont want to run the Test
7178
each time we want to calculate hash.
7279
"""
73-
print(SHA1Test(hash_input).match_hashes())
80+
print(SHA1HashTest(hash_input).match_hashes())
7481

7582

7683
def main():
@@ -84,7 +91,7 @@ def main():
8491
hash_input = open(args.input_file, 'r').read()
8592
else:
8693
hash_input = input_string
87-
print(SHA1(hash_input).final_hash())
94+
print(SHA1Hash(hash_input).final_hash())
8895

8996
if __name__ == '__main__':
9097
main()

0 commit comments

Comments
 (0)