Bitcoin Forum
November 16, 2024, 10:29:54 AM *
News: Latest Bitcoin Core release: 28.0 [Torrent]
 
   Home   Help Search Login Register More  
Pages: [1]
  Print  
Author Topic: problem with sha256 hashing of block header  (Read 572 times)
instant_trader (OP)
Newbie
*
Offline Offline

Activity: 2
Merit: 0


View Profile
May 27, 2017, 08:36:00 AM
 #1

Hi

i have problem (or misunderstanding with sha256 hashing of block header)

i use this code

Code:
import hashlib, struct
 
ver = 2
prev_block = "000000000000000117c80378b8da0e33559b5997f2ad55e2f7d18ec1975b9717"
mrkl_root = "871714dcbae6c8193a2bb9b2a69fe1c0440399f38d94b3a0f1b447275a29978a"
time_ = 0x53058b35 # 2014-02-20 04:57:25
bits = 0x19015f53
 
# https://en.bitcoin.it/wiki/Difficulty
exp = bits >> 24
mant = bits & 0xffffff
target_hexstr = '%064x' % (mant * (1<<(8*(exp - 3))))
target_str = target_hexstr.decode('hex')
 
nonce = 0
while nonce < 0x100000000:
    header = ( struct.pack("<L", ver) + prev_block.decode('hex')[::-1] +
          mrkl_root.decode('hex')[::-1] + struct.pack("<LLL", time_, bits, nonce))
    hash = hashlib.sha256(hashlib.sha256(header).digest()).digest()
    print nonce, hash[::-1].encode('hex')
    if hash[::-1] < target_str:
        print 'success'
        break
    nonce += 1

and it is here online
 https://repl.it/Pkp

so as you can see it works perfectly and solve that block but when i trace it line by line (when it reaches correct nonce that is 856192328 0x33087548)

 first it make this block header

   
Code:
0200000017975b97c18ed1f7e255adf297599b55330edab87803c81701000000000000008a97295a2747b4f1a0b3948df3990344c0e19fa6b2b92b3a19c8e6badc141787358b0553535f011948750833

and then hash it with sha256 that output is

   
Code:
e6d91555891f9c0d0313625ff8cbfecc917063ade047c2eaf73e7a28862b127c

and then again hash

   
Code:
0000000000000000e067a478024addfecdc93628978aa52d91fabd4292982a50
it is solved

  but when i hash the header block with some sha256 online hasher like
    http://www.xorbin.com/tools/sha256-hash-calculator

the output is
   
Code:
1a4ad61b28b74f4ce89b589ed862ac204019c3297b6431529d141f8b82fb56ea
and again
   
Code:
a071333e34ecdca0c9f7d7f7d42192ebb6cc3e3e47a4b20a02108246a5bd8e1c

and you can see it is incorrect!

what is wrong whit it?

achow101
Moderator
Legendary
*
expert
Offline Offline

Activity: 3542
Merit: 6886


Just writing some code


View Profile WWW
May 27, 2017, 10:20:02 AM
 #2

When you are hashing with the script, you are hashing the bytes. When you use a different tool for hashing, you are hashing the string with the hexadecimal representation of the bytes. The bytes are what need to be hashed, the string is only to make it human readable.

instant_trader (OP)
Newbie
*
Offline Offline

Activity: 2
Merit: 0


View Profile
May 27, 2017, 10:33:00 AM
 #3

When you are hashing with the script, you are hashing the bytes. When you use a different tool for hashing, you are hashing the string with the hexadecimal representation of the bytes. The bytes are what need to be hashed, the string is only to make it human readable.

thanks,I checked it with byte[] and it works fine.  Smiley
Pages: [1]
  Print  
 
Jump to:  

Powered by MySQL Powered by PHP Powered by SMF 1.1.19 | SMF © 2006-2009, Simple Machines Valid XHTML 1.0! Valid CSS!