Question

In: Computer Science

Answer in python! 5.16 LAB: Cryptographic Hashing Algorithms Encrypting text allows us to encrypt and decrypt...

Answer in python!

5.16 LAB: Cryptographic Hashing Algorithms Encrypting text allows us to encrypt and decrypt the text using a special key. Another method of encrypting text / passwords is called hashing. Hashing uses special algorithms to 'scramble' text so that it is tougher to hack. The hash function can take numbers, letters, and symbols then uses one of the special algorithms to output scrambled text. The longer the output string, the harder to hack the data. The difference between hashing and the Caesar Cipher encryption, is that you cannot 'decrypt' a hash to its original text. Since a hashed password cannot be decrypted, the user has to enter the password and a program has to hash it and test that value with data stored previously for the password. A salt is used, at times, to create a random sequence that is added to the password before using the hashing algorithm. This can help with Brute Force attacks that attempt to use common words to gain access. Python's hashlib module provides programmers with an API for accessing the different hashing algorithms. Some common hashing algorithms are: md5, sha1, sha224, sha256, and blake2b. The module hashlib has to be imported into your code and a specific algorithm set. An encoding format has to be set, a common encoding format is 'utf-8'. The hash_function( ) is defined in the default template. You need to complete the main function by creating a list called hash_list that contains the five hashing algorithm names described above. Then accept user input of a password to hash, and create a salt variable with 13 numbers and convert it to hex. Finally create a for loop that iterates over the hash_list, creates a new variable returned from a call to the hash_function sending each algorithms name and display info as described below. (There is an ending new line) Ex: If the input is: secretPass the output is: Testing hash algorithm: md5 Hashed Password = bd19f99253c948637d64a4acbd524047:0x40e18692da1 Testing hash algorithm: sha1 Hashed Password = e5fbad38af8ba59c2648e98b9ae4196dfcb9f719:0x40e18692da1 Testing hash algorithm: sha224 Hashed Password = ef0ed799dee72469e5d12ab096473fe6347ae64d5541e95f42478abc:0x40e18692da1 Testing hash algorithm: sha256 Hashed Password = e73b86702464baa976c947a2a8c06adedc1e45ff5a35a07db41385120ce1e10a:0x40e18692da1 Testing hash algorithm: blake2b Hashed Password = 386eef2364609396229c7b58f3606354c12224cecfbc97f7b435c83218eee0b93d453a8ffa1ca2fcfcf5452013bc671fb538f

Solutions

Expert Solution

The entire code is as follows:

import hashlib
import random

def main(s):
    hash_list = [ 
                    hashlib.md5(), 
                    hashlib.sha1(), 
                    hashlib.sha224(), 
                    hashlib.sha256(), 
                    hashlib.blake2b()
                ]
    hashing_name = ["md5", "sha1", "sha224", "sha256", "blake2b"]
    
    length_of_salt = 13
    salt = "".join(random.choice('0123456789') for i in range(length_of_salt))
    hexa = hex(int(salt))
    salt = hexa.split('x')[-1]
    salt = hexa.encode('utf-8')
    
    for i in range(5):
        m = hash_list[i]
        m.update(s+salt)
        print("Testing hash algorithm", hashing_name[i], "Hashed Password = ", m.hexdigest()+":"+hexa)
    
if __name__ == "__main__":
    s = input("Enter a string ").encode('utf-8')
    print(s)
    main(s)

In the main function we take the user input which is the password. We then pass that password into the main function.

In the main function using the hashlib lbrary we create a list of the 5 mentioned hashing algorithms.

After defining the library we move on to creating our autogenerated salt. We use the random function to randomly select characters from a string of 0 to 9. Once the salt is formed, we have to convert it into hex format. We do this by first converting the string of salt into decimals "int(salt)" then we convert the integer into a hexadecimal code "hex(int(salt))" but it is in the format of "0xw97678f776a7667d" so we have to split in on x and take the latter part. we do that by ".split('x')[-1]". Finally, we store the string as a 'utf-8'.

After this we simply have to run a for loop over all the hashing methods we have defined in the hashing list and then pass on the string to each function using the update method of the hashing methods. While passing the string using the update method we concatenate the string with the salt. We then print out the results using hexdigest and appending it at the end with the hex code that we generated above.

I hope this answers your query. If there are any further questions do let me know.


Related Solutions

Write a small program to encrypt and decrypt a message using Python library.
Write a small program to encrypt and decrypt a message using Python library.
A, B:    Design and Implement a C# windows form application to encrypt and decrypt text....
A, B:    Design and Implement a C# windows form application to encrypt and decrypt text. The application use to receive a string and display another encrypted string. The application also decrypt the encrypted string. The approach for encryption/decryption is simple one i.e. to encrypt we will add 1 to each character, so that "hello" would become "ifmmp", and to decrypt we would subtract 1 from each character.    C:   Test and evaluate application by applying different strings.      ...
how to write program in java for encrypt and decrypt input text using DH algorithm
how to write program in java for encrypt and decrypt input text using DH algorithm
I need the code in python where I can encrypt and decrypt any plaintext. For example,...
I need the code in python where I can encrypt and decrypt any plaintext. For example, the plaintext "hello" from each of these Block Cipher modes of Operation. Electronic Code Block Mode (ECB) Cipher block Mode (CBC) Cipher Feedback Mode (CFB) Output feedback Mode (OFB) Counter Mode (CTR) Here is an example, Affine cipher expressed in C. Encryption: char cipher(unsigned char block, char key) { return (key+11*block) } Decryption: char invcipher(unsigned char block, char key) { return (163*(block-key+256)) }
Please answer using python 3 and def functions! Lab 2 Drill 3: (function practice) create and...
Please answer using python 3 and def functions! Lab 2 Drill 3: (function practice) create and use a function named highest() that takes three inputs and returns the highest number. After you have got it working, try calling the function with inputs ‘hat’, ‘cat’, ‘rat’.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT