mirror of
https://github.com/TheAlgorithms/Python.git
synced 2025-01-18 16:27:02 +00:00
moved constants inside the class
This commit is contained in:
parent
4d255f4e9f
commit
7f3895cdaf
|
@ -6,7 +6,6 @@ Usage: python sha1.py --string "Hello World Welcome to Cryptography"
|
|||
Without any arguments prints the hash of the string "Hello World"
|
||||
Also contains a Test class to verify that the generated Hash is same as that
|
||||
returned by the hashlib library
|
||||
Reference: https://deadhacker.com/2006/02/21/sha-1-illustrated/
|
||||
|
||||
The Algorithm as described in the reference:
|
||||
First we start with a message. The message is padded and the length of the message
|
||||
|
@ -15,15 +14,25 @@ processed one at a time. Each block must be expanded and compressed.
|
|||
The value after each compression is added to a 160bit buffer called the current hash
|
||||
state. After the last block is processed the current hash state is returned as
|
||||
the final hash.
|
||||
Reference: https://deadhacker.com/2006/02/21/sha-1-illustrated/
|
||||
"""
|
||||
|
||||
import argparse
|
||||
import hashlib #hashlib is only used inside the Test class
|
||||
|
||||
class SHA1:
|
||||
|
||||
|
||||
class SHA1Hash:
|
||||
"""
|
||||
Class to contain the entire pipeline for SHA1 Hashing Algorithm
|
||||
"""
|
||||
|
||||
H0 - 01100111010001010010001100000001
|
||||
H1 - 11101111110011011010101110001001
|
||||
H2 - 10011000101110101101110011111110
|
||||
H3 - 00010000001100100101010001110110
|
||||
H4 - 11000011110100101110000111110000
|
||||
|
||||
def __init__(self, data):
|
||||
self.data = data
|
||||
self.current_hash = ''
|
||||
|
@ -41,11 +50,9 @@ class SHA1:
|
|||
return
|
||||
|
||||
def final_hash(self):
|
||||
assert True #everything done till now
|
||||
# return self.current_hash
|
||||
return hashlib.sha1(bytes(self.data, 'utf-8')).hexdigest()
|
||||
return 'This is in my To Do list'
|
||||
|
||||
class SHA1Test:
|
||||
class SHA1HashTest:
|
||||
"""
|
||||
Test class for the SHA1 class
|
||||
"""
|
||||
|
@ -53,7 +60,7 @@ class SHA1Test:
|
|||
self.data = data
|
||||
|
||||
def calculated_hash(self):
|
||||
return SHA1(self.data).final_hash()
|
||||
return SHA1Hash(self.data).final_hash()
|
||||
|
||||
def hashlib_hash(self):
|
||||
return hashlib.sha1(self.data.byte_encode()).hexdigest()
|
||||
|
@ -67,10 +74,10 @@ class SHA1Test:
|
|||
|
||||
def run_test_case(hash_input = 'Hello World'):
|
||||
"""
|
||||
Pulled this out of main because we probably dont want to run the Unit Test
|
||||
Pulled this out of main because we probably dont want to run the Test
|
||||
each time we want to calculate hash.
|
||||
"""
|
||||
print(SHA1Test(hash_input).match_hashes())
|
||||
print(SHA1HashTest(hash_input).match_hashes())
|
||||
|
||||
|
||||
def main():
|
||||
|
@ -84,7 +91,7 @@ def main():
|
|||
hash_input = open(args.input_file, 'r').read()
|
||||
else:
|
||||
hash_input = input_string
|
||||
print(SHA1(hash_input).final_hash())
|
||||
print(SHA1Hash(hash_input).final_hash())
|
||||
|
||||
if __name__ == '__main__':
|
||||
main()
|
||||
|
|
Loading…
Reference in New Issue
Block a user