Database System Concepts
Database System Concepts
7th Edition
ISBN: 9780078022159
Author: Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher: McGraw-Hill Education
Bartleby Related Questions Icon

Related questions

Question

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. 

Expert Solution
Check Mark
Knowledge Booster
Background pattern image
Computer Science
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.
Similar questions
SEE MORE QUESTIONS
Recommended textbooks for you
Text book image
Database System Concepts
Computer Science
ISBN:9780078022159
Author:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:McGraw-Hill Education
Text book image
Starting Out with Python (4th Edition)
Computer Science
ISBN:9780134444321
Author:Tony Gaddis
Publisher:PEARSON
Text book image
Digital Fundamentals (11th Edition)
Computer Science
ISBN:9780132737968
Author:Thomas L. Floyd
Publisher:PEARSON
Text book image
C How to Program (8th Edition)
Computer Science
ISBN:9780133976892
Author:Paul J. Deitel, Harvey Deitel
Publisher:PEARSON
Text book image
Database Systems: Design, Implementation, & Manag...
Computer Science
ISBN:9781337627900
Author:Carlos Coronel, Steven Morris
Publisher:Cengage Learning
Text book image
Programmable Logic Controllers
Computer Science
ISBN:9780073373843
Author:Frank D. Petruzella
Publisher:McGraw-Hill Education