Question
6.16 LAB: Cryptographic Hashing Algorithms Encrypting text allows us to encrypt and decrypt the text usinga special key. Another method of encrypting text / passwords
6.16 LAB: Cryptographic Hashing Algorithms
Encrypting text allows us to encrypt and decrypt the text usinga special key. Another method of encrypting text / passwords iscalled hashing. Hashing uses special algorithms to 'scramble' textso that it is tougher to hack. The hash function can take numbers,letters, and symbols then uses one of the special algorithms tooutput scrambled text. The longer the output string, the harder tohack the data. The difference between hashing and the Caesar Cipherencryption, is that you cannot 'decrypt' a hash to its originaltext.
Since a hashed password cannot be decrypted, the user has toenter the password and a program has to hash it and test that valuewith data stored previously for the password. A salt is used, attimes, to create a random sequence that is added to the passwordbefore using the hashing algorithm. This can help with Brute Forceattacks that attempt to use common words to gain access.
Python's hashlib module provides programmers with an API foraccessing the different hashing algorithms. Some common hashingalgorithms are: md5, sha1, sha224, sha256, and blake2b.
The module hashlib has to be imported into your code and aspecific algorithm set. An encoding format has to be set, a commonencoding format is 'utf-8'.
The hash_function( ) is defined in the default template. Youneed to complete the main function by creating a list calledhash_list that contains the five hashing algorithm names describedabove. Then accept user input of a password to hash, and create asalt variable with 13 numbers and convert it to hex. Finally createa for loop that iterates over the hash_list, creates a new variablereturned from a call to the hash_function sending each algorithmsname and display info as described below. (There is an ending newline)
Ex: If the input is:
secretPass
the output is:
Testing hash algorithm: md5Hashed Password = bd19f99253c948637d64a4acbd524047:0x40e18692da1Testing hash algorithm: sha1Hashed Password = e5fbad38af8ba59c2648e98b9ae4196dfcb9f719:0x40e18692da1Testing hash algorithm: sha224Hashed Password = ef0ed799dee72469e5d12ab096473fe6347ae64d5541e95f42478abc:0x40e18692da1Testing hash algorithm: sha256Hashed Password = e73b86702464baa976c947a2a8c06adedc1e45ff5a35a07db41385120ce1e10a:0x40e18692da1Testing hash algorithm: blake2bHashed Password = 386eef2364609396229c7b58f3606354c12224cecfbc97f7b435c83218eee0b93d453a8ffa1ca2fcfcf5452013bc671fb538f35e426b33e4f07cee1f04a16bc7:0x40e18692da1
Some code already provided:
import hashlib
def hash_function(password, salt, al):
if al == 'md5':
#md5
hash = hashlib.md5(salt.encode() +password.encode())
hash.update(password.encode('utf-8'))
return hash.hexdigest() + ':' + salt
elif (al == 'sha1'):
#sha1
hash = hashlib.sha1()
hash.update(password.encode('utf-8'))
return hash.hexdigest() + ':' + salt
elif al == 'sha224':
#sha224
hash = hashlib.sha224()
hash.update(password.encode('utf-8'))
return hash.hexdigest() + ':' + salt
elif al == 'sha256':
#sha256
hash = hashlib.sha256()
hash.update(password.encode('utf-8'))
return hash.hexdigest() + ':' + salt
elif al == 'blake2b':
#blake2b512
hash = hashlib.blake2b()
hash.update(password.encode('utf-8'))
return hash.hexdigest() + ':' + salt
else:
print("Error: No Algorithm!")
if __name__ == "__main__":
# TODO: create a list called hash_list that contains
# the five hashing algorithsm as strings
# md5, sha1, sha224, sha256, blake2b
# TODO: accept user input for a password to hash
# TODO: create a salt variable from 13 numberand
# convert it to hex
# TODO: use a for loop to iterate through thehash_list.
# Inside the loop create a new variable returned from a callto the hash_function
# Display the info as described above. (new line afterlast print)
Step by Step Solution
3.35 Rating (155 Votes )
There are 3 Steps involved in it
Step: 1
The detailed answer for the above question is provided below So this program uses a python module known as hashlib to enforce diverse hashing algorith...Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started