Given hash_function( ) defined in the default template, complete the main function that does the following tasks: Create a list called hash_list that contains the five hashing algorithm names described above. Read from the user a password to hash. Declare a salt variable and initialize the variable to the hex representation of 4458585599393. Hint: Use function hex(). Use a for loop to iterate over the hash_list and call the hash_function() with the hashing algorithm names in the list. Store the returned value of hash_function() in a variable and output the algorithm name used and the hashed password. Note: Output a new line after each hashed password is printed. If the input is: secretPass the output is: Testing hash algorithm: md5 Hashed Password = bd19f99253c948637d64a4acbd524047:0x40e18692da1 Testing hash algorithm: sha1 Hashed Password = e5fbad38af8ba59c2648e98b9ae4196dfcb9f719:0x40e18692da1 My code is: 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!") #return a null string because no encoding is done return "" if __name__ == "__main__": hash_list = ['md5', 'shal', 'sha224', 'sha256', 'blake2b'] password = str(input()) salt = hex(78246391246824) for i in range (len(hash_list)): print("Testing hash algorithm: " + hash_list[i]) temp=hash_function(password, salt, hash_list[i]) if temp!="": # to accomadate the case of no algorithm print("Hashed Password = " + hash_function(password, salt, hash_list[i]) + "\n") else: print("\n") But I'm not getting the correct output. I don't know what I did wrong.
Given hash_function( ) defined in the default template, complete the main function that does the following tasks:
- Create a list called hash_list that contains the five hashing
algorithm names described above. - Read from the user a password to hash.
- Declare a salt variable and initialize the variable to the hex representation of 4458585599393. Hint: Use function hex().
- Use a for loop to iterate over the hash_list and call the hash_function() with the hashing algorithm names in the list. Store the returned value of hash_function() in a variable and output the algorithm name used and the hashed password. Note: Output a new line after each hashed password is printed.
If the input is: secretPass
the output is:
Testing hash algorithm: md5 Hashed Password = bd19f99253c948637d64a4acbd524047:0x40e18692da1
Testing hash algorithm: sha1 Hashed Password = e5fbad38af8ba59c2648e98b9ae4196dfcb9f719:0x40e18692da1
My code is:
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!")
#return a null string because no encoding is done
return ""
if __name__ == "__main__":
hash_list = ['md5', 'shal', 'sha224', 'sha256', 'blake2b']
password = str(input())
salt = hex(78246391246824)
for i in range (len(hash_list)):
print("Testing hash algorithm: " + hash_list[i])
temp=hash_function(password, salt, hash_list[i])
if temp!="":
# to accomadate the case of no algorithm
print("Hashed Password = " + hash_function(password, salt, hash_list[i]) + "\n")
else:
print("\n")
But I'm not getting the correct output. I don't know what I did wrong.
Step by step
Solved in 3 steps