6
6
Without any arguments prints the hash of the string "Hello World"
7
7
Also contains a Test class to verify that the generated Hash is same as that
8
8
returned by the hashlib library
9
- Reference: https://deadhacker.com/2006/02/21/sha-1-illustrated/
10
9
11
10
The Algorithm as described in the reference:
12
11
First we start with a message. The message is padded and the length of the message
15
14
The value after each compression is added to a 160bit buffer called the current hash
16
15
state. After the last block is processed the current hash state is returned as
17
16
the final hash.
17
+ Reference: https://deadhacker.com/2006/02/21/sha-1-illustrated/
18
18
"""
19
19
20
20
import argparse
21
21
import hashlib #hashlib is only used inside the Test class
22
22
23
- class SHA1 :
23
+
24
+
25
+ class SHA1Hash :
24
26
"""
25
27
Class to contain the entire pipeline for SHA1 Hashing Algorithm
26
28
"""
29
+
30
+ H0 - 01100111010001010010001100000001
31
+ H1 - 11101111110011011010101110001001
32
+ H2 - 10011000101110101101110011111110
33
+ H3 - 00010000001100100101010001110110
34
+ H4 - 11000011110100101110000111110000
35
+
27
36
def __init__ (self , data ):
28
37
self .data = data
29
38
self .current_hash = ''
@@ -41,19 +50,17 @@ def compress_block(self):
41
50
return
42
51
43
52
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'
47
54
48
- class SHA1Test :
55
+ class SHA1HashTest :
49
56
"""
50
57
Test class for the SHA1 class
51
58
"""
52
59
def __init__ (self , data ):
53
60
self .data = data
54
61
55
62
def calculated_hash (self ):
56
- return SHA1 (self .data ).final_hash ()
63
+ return SHA1Hash (self .data ).final_hash ()
57
64
58
65
def hashlib_hash (self ):
59
66
return hashlib .sha1 (self .data .byte_encode ()).hexdigest ()
@@ -67,10 +74,10 @@ def match_hashes(self):
67
74
68
75
def run_test_case (hash_input = 'Hello World' ):
69
76
"""
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
71
78
each time we want to calculate hash.
72
79
"""
73
- print (SHA1Test (hash_input ).match_hashes ())
80
+ print (SHA1HashTest (hash_input ).match_hashes ())
74
81
75
82
76
83
def main ():
@@ -84,7 +91,7 @@ def main():
84
91
hash_input = open (args .input_file , 'r' ).read ()
85
92
else :
86
93
hash_input = input_string
87
- print (SHA1 (hash_input ).final_hash ())
94
+ print (SHA1Hash (hash_input ).final_hash ())
88
95
89
96
if __name__ == '__main__' :
90
97
main ()
0 commit comments