Skip to content

Commit 47bc34a

Browse files
QuantumNoviceharshildarji
authored andcommitted
Added pytests to sha1.py (TheAlgorithms#1098)
1 parent 4437439 commit 47bc34a

File tree

3 files changed

+8
-2
lines changed

3 files changed

+8
-2
lines changed

16L'

Whitespace-only changes.

Q'

Whitespace-only changes.

hashes/sha1.py

+8-2
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
Demonstrates implementation of SHA1 Hash function in a Python class and gives utilities
33
to find hash of string or hash of text from a file.
44
Usage: python sha1.py --string "Hello World!!"
5-
pyhton sha1.py --file "hello_world.txt"
5+
python sha1.py --file "hello_world.txt"
66
When run without any arguments, it prints the hash of the string "Hello World!! Welcome to Cryptography"
77
Also contains a Test class to verify that the generated Hash is same as that
88
returned by the hashlib library
@@ -32,6 +32,8 @@
3232
class SHA1Hash:
3333
"""
3434
Class to contain the entire pipeline for SHA1 Hashing Algorithm
35+
>>> SHA1Hash(bytes('Allan', 'utf-8')).final_hash()
36+
'872af2d8ac3d8695387e7c804bf0e02c18df9e6e'
3537
"""
3638
def __init__(self, data):
3739
"""
@@ -47,6 +49,8 @@ def __init__(self, data):
4749
def rotate(n, b):
4850
"""
4951
Static method to be used inside other methods. Left rotates n by b.
52+
>>> SHA1Hash('').rotate(12,2)
53+
48
5054
"""
5155
return ((n << b) | (n >> (32 - b))) & 0xffffffff
5256

@@ -68,7 +72,7 @@ def split_blocks(self):
6872
def expand_block(self, block):
6973
"""
7074
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
7276
"""
7377
w = list(struct.unpack('>16L', block)) + [0] * 64
7478
for i in range(16, 80):
@@ -146,3 +150,5 @@ def main():
146150

147151
if __name__ == '__main__':
148152
main()
153+
import doctest
154+
doctest.testmod()

0 commit comments

Comments
 (0)