Title: Recalculating the hash in python Post by: Martin P. Hellwig on June 10, 2011, 02:19:13 PM Hi all,
I am trying to recalculate the hash but I think I am making somewhere a giant snafu. I attached my code, would anyone be willing to hint me in the right direction? Thanks <code> import hashlib def i2w(integer): """Convert integer to 4 byte word in a string. Will raise exception if overflows.""" work = bin(integer)[2::].zfill(32) if len(work) > 32: raise(OverflowError(integer)) tmp = list() while len(work) > 7: part = work[:8] work = work[8:] char = chr(int(part, 2)) tmp.append(char) return(''.join(tmp)) if __name__ == '__main__': # Contents of genesis block: http://blockexplorer.com/b/0 #"hash":"000000000019d6689c085ae165831e934ff763ae46a2a6c172b3f1b60a8ce26f", #"ver":1, #"prev_block":"0000000000000000000000000000000000000000000000000000000000000000", #"mrkl_root":"4a5e1e4baab89f3a32518a88c31bc87f618f76673e2cc77ab2127b7afdeda33b", #"time":1231006505, #"bits":486604799, #"nonce":2083236893, # hash = "000000000019d6689c085ae165831e934ff763ae46a2a6c172b3f1b60a8ce26f" # Try to reproduce the above hash with below data v = 1 p = "0000000000000000000000000000000000000000000000000000000000000000" m = "4a5e1e4baab89f3a32518a88c31bc87f618f76673e2cc77ab2127b7afdeda33b" t = 1231006505 b = 486604799 n = 2083236893 tmp = i2w(v) + p.decode('hex') + m.decode('hex') + i2w(t) + i2w(b) + i2w(n) one = hashlib.sha256() one.update(tmp) hash_one = one.digest() two = hashlib.sha256() two.update(hash_one) hash_two = two.digest() print(hash) print(hash_two.encode('hex')) <output> >>> 000000000019d6689c085ae165831e934ff763ae46a2a6c172b3f1b60a8ce26f >>> f40519089bfd56ee63f6207f11e69707ac85e5ca20ab0e4aeaa468dbc72e7ae0 Title: Re: Recalculating the hash in python Post by: Martin P. Hellwig on June 10, 2011, 03:11:44 PM Never mind I see already a couple of errors, I'll try to convert http://pastebin.com/Ya3604J0
And will update this here once I get something working. Title: Re: Recalculating the hash in python Post by: Martin P. Hellwig on June 10, 2011, 06:28:40 PM As promised:
Code: #! /usr/bin/env python Title: Re: Recalculating the hash in python Post by: titeuf_87 on June 11, 2011, 01:48:47 AM Looking at your code, you have some byte order issues:
First, the i2w function. I'm too tired to actually fix this, so I went ahead and used the struct module (http://docs.python.org/library/struct.html) (you should look into using it, it makes life a lot easier): Code: def i2w(integer): Then, when assembling the header, you still need to reverse p and m: Code: tmp = i2w(v) + p.decode('hex')[::-1] + m.decode('hex')[::-1] + i2w(t) + i2w(b) + i2w(n) And lastly, when printing out the result, you need to reverse it too: Code: print(hash_two[::-1].encode('hex')) Title: Re: Recalculating the hash in python Post by: Martin P. Hellwig on June 11, 2011, 10:07:48 AM Hi,
As you can see I have found my errors, corrected it and posted the updated code before you replied, sorry for wasting your time. |