2
2
Demonstrates implementation of SHA1 Hash function in a Python class and gives utilities
3
3
to find hash of string or hash of text from a file.
4
4
Usage: python sha1.py --string "Hello World!!"
5
- pyhton sha1.py --file "hello_world.txt"
5
+ python sha1.py --file "hello_world.txt"
6
6
When run without any arguments, it prints the hash of the string "Hello World!! Welcome to Cryptography"
7
7
Also contains a Test class to verify that the generated Hash is same as that
8
8
returned by the hashlib library
32
32
class SHA1Hash :
33
33
"""
34
34
Class to contain the entire pipeline for SHA1 Hashing Algorithm
35
+ >>> SHA1Hash(bytes('Allan', 'utf-8')).final_hash()
36
+ '872af2d8ac3d8695387e7c804bf0e02c18df9e6e'
35
37
"""
36
38
def __init__ (self , data ):
37
39
"""
@@ -47,6 +49,8 @@ def __init__(self, data):
47
49
def rotate (n , b ):
48
50
"""
49
51
Static method to be used inside other methods. Left rotates n by b.
52
+ >>> SHA1Hash('').rotate(12,2)
53
+ 48
50
54
"""
51
55
return ((n << b ) | (n >> (32 - b ))) & 0xffffffff
52
56
@@ -68,7 +72,7 @@ def split_blocks(self):
68
72
def expand_block (self , block ):
69
73
"""
70
74
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
75
+ list of 80 integers after some bit operations
72
76
"""
73
77
w = list (struct .unpack ('>16L' , block )) + [0 ] * 64
74
78
for i in range (16 , 80 ):
@@ -146,3 +150,5 @@ def main():
146
150
147
151
if __name__ == '__main__' :
148
152
main ()
153
+ import doctest
154
+ doctest .testmod ()
0 commit comments