hashes.sha1¶
Implementation of the SHA1 hash function and gives utilities to find hash of string or hash of text from a file. Also contains a Test class to verify that the generated hash matches what is returned by the hashlib library
- Usage: python sha1.py –string “Hello World!!”
python sha1.py –file “hello_world.txt” When run without any arguments, it prints the hash of the string “Hello World!! Welcome to Cryptography”
SHA1 hash or SHA1 sum of a string is a cryptographic function, which means it is easy to calculate forwards but extremely difficult to calculate backwards. What this means is you can easily calculate the hash of a string, but it is extremely difficult to know the original string if you have its hash. This property is useful for communicating securely, send encrypted messages and is very useful in payment systems, blockchain and cryptocurrency etc.
The algorithm as described in the reference: First we start with a message. The message is padded and the length of the message is added to the end. It is then split into blocks of 512 bits or 64 bytes. The blocks are then processed one at a time. Each block must be expanded and compressed. The value after each compression is added to a 160-bit 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/
Classes¶
Class to contain the entire pipeline for SHA1 hashing algorithm |
Functions¶
|
Provides option 'string' or 'file' to take input and prints the calculated SHA1 |
Module Contents¶
- class hashes.sha1.SHA1Hash(data)¶
Class to contain the entire pipeline for SHA1 hashing algorithm >>> SHA1Hash(bytes(‘Allan’, ‘utf-8’)).final_hash() ‘872af2d8ac3d8695387e7c804bf0e02c18df9e6e’
- expand_block(block)¶
Takes a bytestring-block of length 64, unpacks it to a list of integers and returns a list of 80 integers after some bit operations
- final_hash()¶
Calls all the other methods to process the input. Pads the data, then splits into blocks and then does a series of operations for each block (including expansion). For each block, the variable h that was initialized is copied to a,b,c,d,e and these 5 variables a,b,c,d,e undergo several changes. After all the blocks are processed, these 5 variables are pairwise added to h ie a to h[0], b to h[1] and so on. This h becomes our final hash which is returned.
- padding()¶
Pads the input message with zeros so that padded_data has 64 bytes or 512 bits
- static rotate(n, b)¶
Static method to be used inside other methods. Left rotates n by b. >>> SHA1Hash(‘’).rotate(12,2) 48
- split_blocks()¶
Returns a list of bytestrings each of length 64
- data¶
- h = [1732584193, 4023233417, 2562383102, 271733878, 3285377520]¶
- hashes.sha1.main()¶
Provides option ‘string’ or ‘file’ to take input and prints the calculated SHA1 hash. unittest.main() has been commented out because we probably don’t want to run the test each time.
- hashes.sha1.test_sha1_hash()¶