To generate a hash value from
a given message
T.A.
Yang
·
The following program is originally from the
geeksforgeeks.org site, https://www.geeksforgeeks.org/hashlib-module-in-python/.
#
importing hashlib for getting sha256() hash
function import hashlib # A
string that has been stored as a byte stream # (due
to the prefix b) string = b"My name is apple and I am a vegetable?" #
Initializing the sha256() method sha256
= hashlib.sha256() #
Passing the byte stream as an argument sha256.update(string) #
sha256.hexdigest() hashes all the input data #
passed to the sha256() via sha256.update() # Acts
as a finalize method, after which all # the
input data gets hashed # hexdigest() hashes the data, and returns # the
output in hexadecimal format string_hash =
sha256.hexdigest() print(f"Hash:{string_hash}") |
EX1: Save this
program into a file as a Python file, say hashing1.py and then run that Python script
to generate the hash value from the given message.
NOTE: In the following screenshot
it is assumed that the file hashing1.py is stored under the folder
c:\csci3341dir. If you save the file in another folder, use that folder name
instead.
To hand in:
a.
A screenshot showing you have successfully
run the given script.
b.
Answer the following questions.
Q1: How many
bytes are produced as the hash value when you run the sha256
hashing function?
Ans1:
c.
What hash value would be
generated by the script if you change only a single character in the
message? For example, change the message to "My name is apple and I am a
vegetable." (That is, replace ? with a period.) As
shown in the screenshot below, a different hash is produced.
Revise the script and hand in a screenshot of running the revised script.