Question

c++ using i think the assignment want hashing function to check date if was efficiently transferred or missing any data if transferred please look for the graph like in L1 AND L2 they transfer data then when they all match then i think means transferred correctly or missing anything pleas help

im not sure if question want with hashing but please do any coding with hashing anything will help

Given the attached Merkle Tree from

Top Hash Hash o hash ash 1 Hash Hash Hah 1 0 ash00 bash ash 1 Hah 1-1 Hash Hash as Hash 0-1 0-0 hash(L4) hash(L.3) hash(L2) h

Let

L1=Then out spake brave Horatius,

The Captain of the Gate:

L2="To every man upon this earth

Death cometh soon or late.

L3=And how can man die better

Than facing fearful odds,

and

L4=For the ashes of his fathers,

And the temples of his Gods."

Create all hashes 0-0, 0-1, 1-0, 1-1, 0, 1 with top

Show all you need to do to confirm

L1 is 0-1, 0, top

L2 is 0-0, 1, top

L3 is 1-1, 0, top and

L4 is 1-0, 0, top

0 0
Add a comment Improve this question Transcribed image text
Answer #1

You have not framed the question pretty well but from what I have gathered from the question is that we have to generate the hashes 0-0, 0-1, 1-0, 1-1, 0, 1 and top hash value with the given information values L1, L2, L3, L4. Now we use sha256 encrypting algorithm to hash the values in a Merkle tree. I will not write the algorithm from scratch because there exists a python library that does the job of sha256 hashing. Writing the sha256 from hand is an extremely tedious task and cannot be done within my given limited time window to answer the question hence I have used python to answer your question.You can get the hash value of the Merkle root recursively but since we want the hash values at every step, we would do it step by step because the number of blocks is really small (4) being L1, L2, L3, L4. So first we will hash L1 with itself and we will do the same for the rest and the resulting hash values will be 0-0, 0-1, 1-0, 1-1 respectively. Then we will hash 0-0 with 0-1 and hash 1-0 with 1-1 and let the result be 0 and 1 respectively. Finally, we will hash 0 and 1 respectively to get the Merkle Root hash. Remember that when we say we are hashing we using sha256 hashing algorithm present in the hashlib library of python. So I will provide you with a python code along with the respective outputs. So here goes the code:-

(before that one very important thing you should notice and remember that sha256 encoding is possible on strings having even length and containing only hexadecimal characters, so first we have to change the string to its hexadecimal value and that is the extra step that I have done after that I have used those hexadecimal strings in sha256 encoding).

Python Code:-

import hashlib
import binascii

def hesh(a, b):
a1 = a.decode('hex')[::-1]
b1 = b.decode('hex')[::-1]
h = hashlib.sha256(hashlib.sha256(a1 + b1).digest()).digest() #sha256 encyption used to encrypt the data in a merkle tree
return h[::-1].encode('hex')

str1 = "Then out spake brave Horatius, The Captain of the Gate:"
str2 = "To every man upon this earth Death cometh soon or late."
str3 = "And how can man die better Than facing fearful odds, and"
str4 = "For the ashes of his fathers, And the temples of his Gods."

#hexa1 = str1.encode('utf-8')
#hexa1 = hexa1.hex()

'''
what i did in this step is that I used the small snippet of code
written above in comments to generate the hexadecimal values for
all the given strings str1, str2, str3, str4. Then i copied the
hexadecimal values from the terminal after running the code and
stored them into separate variables
'''

L1 = "5468656e206f7574207370616b6520627261766520486f7261746975732c20546865204361707461696e206f662074686520476174653a"
L2 = "546f206576657279206d616e2075706f6e207468697320656172746820446561746820636f6d65746820736f6f6e206f72206c6174652e"
L3 = "416e6420686f772063616e206d616e2064696520626574746572205468616e20666163696e67206665617266756c206f6464732c20616e64"
L4 = "466f7220746865206173686573206f662068697320666174686572732c20416e64207468652074656d706c6573206f662068697320476f64732e"

hash00 = hesh(L1, L1);
hash01 = hesh(L2, L2);
hash10 = hesh(L3, L3);
hash11 = hesh(L4, L4);

print ("Hash00 is " + hash00)
print ("Hash01 is " + hash01)
print ("Hash10 is " + hash10)
print ("Hash11 is " + hash11)

hash0 = hesh(hash00, hash01)
hash1 = hesh(hash10, hash11)

print ("Hash0 is " + hash0)
print ("Hash1 is " + hash1)

merkle_root = hesh(hash0, hash1)

print ("Value at merkle root is " + merkle_root)

If you have any problem regarding the concept or of what I did in this problem just leave a comment down below regarding your problem and I will be more than happy to help you any time. Thank you. Happy coding.

Add a comment
Know the answer?
Add Answer to:
c++ using i think the assignment want hashing function to check date if was efficiently transferred or missing any data...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT