|
1 | 1 | """ |
2 | | -Demonstrates implementation of SHA1 Hash function in a Python class and gives utilities |
3 | | -to find hash of string or hash of text from a file. |
| 2 | +Implementation of the SHA1 hash function and gives utilities to find hash of string or |
| 3 | +hash of text from a file. Also contains a Test class to verify that the generated hash |
| 4 | +matches what is returned by the hashlib library |
| 5 | +
|
4 | 6 | Usage: python sha1.py --string "Hello World!!" |
5 | 7 | python sha1.py --file "hello_world.txt" |
6 | 8 | When run without any arguments, it prints the hash of the string "Hello World!! |
7 | 9 | Welcome to Cryptography" |
8 | | -Also contains a Test class to verify that the generated Hash is same as that |
9 | | -returned by the hashlib library |
10 | 10 |
|
11 | | -SHA1 hash or SHA1 sum of a string is a cryptographic function which means it is easy |
| 11 | +SHA1 hash or SHA1 sum of a string is a cryptographic function, which means it is easy |
12 | 12 | to calculate forwards but extremely difficult to calculate backwards. What this means |
13 | | -is, you can easily calculate the hash of a string, but it is extremely difficult to |
14 | | -know the original string if you have its hash. This property is useful to communicate |
15 | | -securely, send encrypted messages and is very useful in payment systems, blockchain |
16 | | -and cryptocurrency etc. |
17 | | -The Algorithm as described in the reference: |
| 13 | +is you can easily calculate the hash of a string, but it is extremely difficult to know |
| 14 | +the original string if you have its hash. This property is useful for communicating |
| 15 | +securely, send encrypted messages and is very useful in payment systems, blockchain and |
| 16 | +cryptocurrency etc. |
| 17 | +
|
| 18 | +The algorithm as described in the reference: |
18 | 19 | First we start with a message. The message is padded and the length of the message |
19 | 20 | is added to the end. It is then split into blocks of 512 bits or 64 bytes. The blocks |
20 | 21 | are then processed one at a time. Each block must be expanded and compressed. |
21 | | -The value after each compression is added to a 160bit buffer called the current hash |
22 | | -state. After the last block is processed the current hash state is returned as |
| 22 | +The value after each compression is added to a 160-bit buffer called the current hash |
| 23 | +state. After the last block is processed, the current hash state is returned as |
23 | 24 | the final hash. |
| 25 | +
|
24 | 26 | Reference: https://deadhacker.com/2006/02/21/sha-1-illustrated/ |
25 | 27 | """ |
26 | 28 | import argparse |
|
30 | 32 |
|
31 | 33 | class SHA1Hash: |
32 | 34 | """ |
33 | | - Class to contain the entire pipeline for SHA1 Hashing Algorithm |
| 35 | + Class to contain the entire pipeline for SHA1 hashing algorithm |
34 | 36 | >>> SHA1Hash(bytes('Allan', 'utf-8')).final_hash() |
35 | 37 | '872af2d8ac3d8695387e7c804bf0e02c18df9e6e' |
36 | 38 | """ |
37 | 39 |
|
38 | 40 | def __init__(self, data): |
39 | 41 | """ |
40 | | - Inititates the variables data and h. h is a list of 5 8-digit Hexadecimal |
| 42 | + Initiates the variables data and h. h is a list of 5 8-digit hexadecimal |
41 | 43 | numbers corresponding to |
42 | 44 | (1732584193, 4023233417, 2562383102, 271733878, 3285377520) |
43 | 45 | respectively. We will start with this as a message digest. 0x is how you write |
44 | | - Hexadecimal numbers in Python |
| 46 | + hexadecimal numbers in Python |
45 | 47 | """ |
46 | 48 | self.data = data |
47 | 49 | self.h = [0x67452301, 0xEFCDAB89, 0x98BADCFE, 0x10325476, 0xC3D2E1F0] |
@@ -90,7 +92,7 @@ def final_hash(self): |
90 | 92 | For each block, the variable h that was initialized is copied to a,b,c,d,e |
91 | 93 | and these 5 variables a,b,c,d,e undergo several changes. After all the blocks |
92 | 94 | are processed, these 5 variables are pairwise added to h ie a to h[0], b to h[1] |
93 | | - and so on. This h becomes our final hash which is returned. |
| 95 | + and so on. This h becomes our final hash which is returned. |
94 | 96 | """ |
95 | 97 | self.padded_data = self.padding() |
96 | 98 | self.blocks = self.split_blocks() |
@@ -135,7 +137,7 @@ def test_sha1_hash(): |
135 | 137 | def main(): |
136 | 138 | """ |
137 | 139 | Provides option 'string' or 'file' to take input and prints the calculated SHA1 |
138 | | - hash. unittest.main() has been commented because we probably don't want to run |
| 140 | + hash. unittest.main() has been commented out because we probably don't want to run |
139 | 141 | the test each time. |
140 | 142 | """ |
141 | 143 | # unittest.main() |
|
0 commit comments