0

I am trying to create a python program that will give me a final hash that has been provided for me.

Final Hash: 42EE53E049F4E104BF81A517C5ED52BE2D94487A253FCF978CE783A3529794BC using the following 5 hash values:

  • hash1: F7002A5259567B1F993E743D3128B6A97B153EACFC7BB914802DCFB43D23FA2E
  • hash2: 6E2B86DC5982F533C3A896E66B97D377D09E7988B7E27E9BE5DDBA9F34C325FC
  • hash3: 83AAB3327FFF40207AEB5919BD7FB06BAE953324FC71EE35816076CD6480334A
  • hash4: 0B794C734A46D75BE2EEE543F714E8D7E2D41D0549D4D8E1167B77B63080DE83
  • hash5: EC40BD8242061EF401305485800CA5D63A9AB6DA659221A27C7BFAD3A9694E6C

as well as an initial hash of: E3B0C44298FC1C149AFBF4C8996FB92427AE41E4649B934CA495991B7852B855

Those 5 hashes and the initial value need to match up to the final hash.

I have tried to create a while loop, and I have tried to reorder the hash values for example instead of "1,2,3,4,5" I have tried "3,5,4,1,2" to see if I get the final value.

import hashlib

chain = hashlib.sha256()

#Hash 1
hash_1 = hashlib.sha256(b"This is my first hash")
hex_dig = hash_1.hexdigest().upper()
print("\nOld:",hex_dig)

#Hash 2
hash_2 = hashlib.sha256(b"This is my second hash")
hex_dig2 = hash_2.hexdigest().upper()
print("\nOld:",hex_dig2)

#Hash 3
hash_3 = hashlib.sha256(b"This is my third hash")
hex_dig3 = hash_3.hexdigest().upper()
print("\nOld:", chain.hexdigest().upper())

#Hash 4
hash_4 = hashlib.sha256(b"This is my forth hash")
hex_dig4 = hash_4.hexdigest().upper()
print("\nold:", chain.hexdigest().upper())

#Hash 5
hash_5 = hashlib.sha256(b"This is my fifth hash")
hex_dig5 = hash_5.hexdigest().upper()
print("\nOld:", chain.hexdigest().upper())

print("\nUpdated Chain\nAdding:\t",hash_3.hexdigest().upper())
chain.update(hash_3.digest())
print("New:\t",chain.hexdigest().upper())

print("\nUpdated Chain\nAdding:\t",hash_1.hexdigest().upper())
chain.update(hash_1.digest())
print("New:\t",chain.hexdigest().upper())

print("\nUpdated Chain\nAdding:\t",hash_2.hexdigest().upper())
chain.update(hash_2.digest())
print("New:\t",chain.hexdigest().upper())

print("\nUpdated Chain\nAdding:\t",hash_5.hexdigest().upper())
chain.update(hash_5.digest())
print("New:\t",chain.hexdigest().upper())

print("\nUpdated Chain\nAdding:\t",hash_4.hexdigest().upper())
chain.update(hash_4.digest())
print("New:\t",chain.hexdigest().upper())

print("\nFinal Chain:\n", chain.hexdigest().upper())

chain = hashlib.sha256()
print("\nInitial Chain:\n",chain.hexdigest().upper())

I expect the output to be Final Chain: 42EE53E049F4E104BF81A517C5ED52BE2D94487A253FCF978CE783A3529794BC

tripleee
  • 175,061
  • 34
  • 275
  • 318

1 Answers1

0

You should add actual data with update() rather than the SHA256 digest of that data.

Instead of

hash_1 = hashlib.sha256(b"This is my first hash")
#...
chain.update(hash1.digest())

you want something more along the lines of

chain.update(b"This is my first hash")

If you also want to calculate and display the hash of each item you add with update, some easy refactoring should allow you to do that, too; but this is the gist of your problem, I believe.

Here's untested code to replace yours:

import hashlib

chain = hashlib.sha256()
print("Initial Chain: {0}".format(chain.hexdigest().upper()))

for data in (b"This is my first hash", b"This is my second hash",
        b"This is my third hash", b"This is my forth hash", b"This is my fifth hash"):
    hash = hashlib.sha256(data)
    print("Add: {0}".format(hash_1.hexdigest().upper()))
    chain.update(data)
    print("New:\t{0}".format(chain.hexdigest().upper()))
tripleee
  • 175,061
  • 34
  • 275
  • 318
  • How would I try to find a combination of those hashes to equal the final hash though? That's what I'm trying to figure out.. I feel like I've tried everything. – Shakespeareeee Jul 24 '19 at 06:54
  • https://stackoverflow.com/questions/104420/how-to-generate-all-permutations-of-a-list-in-python – tripleee Jul 24 '19 at 07:04