Block hashing algorithm : Algo of BitCoin


HI, Guys Learner is here with an important algorithm which is helpful in BitCoins Security.
So..Let’s start talking about Block Hashing Algorithm.

What is Block Hashing Algorithm?

Bitcoin mining uses the hashcash proof of work function; the hashcash algorithm requires the following parameters: a service string, a nonce, and a counter. In bitcoin, the service string is encoded in the block header data structure and includes a version field, the hash of the previous block, the root hash of the merkle tree of all transactions in the block, the current time, and the difficulty. Bitcoin stores the nonce in the extraNonce field which is part of the coinbase transaction, which is stored as the left most leaf node in the merkle tree (the coinbase is the special first transaction in the block). The counter parameter is small at 32-bits so each time it wraps the extraNonce field must be incremented (or otherwise changed) to avoid repeating work. The basics of the hashcash algorithm are quite easy to understand and it is described in more detail here. When mining bitcoin, the hashcash algorithm repeatedly hashes the block header while incrementing the counter & extraNonce fields. Incrementing the extraNonce field entails recomputing the merkle tree, as the coinbase transaction is the left most leaf node. The block is also occasionally updated as you are working on it.

Image result for Block hashing algorithm : Algo of BitCoin

How can you use it?

>>> import hashlib
>>> header_hex = (“01000000” +
“81cd02ab7e569e8bcd9317e2fe99f2de44d49ab2b8851ba4a308000000000000” +
“e320b6c2fffc8d750423db8b1eb942ae710e951ed797f7affc8892b0f1fc122b” +
“c7f5d74d” +
“f2b9441a” +
>>> header_bin = header_hex.decode(‘hex’)
>>> hash = hashlib.sha256(hashlib.sha256(header_bin).digest()).digest()
>>> hash.encode(‘hex_codec’)
>>> hash[::-1].encode(‘hex_codec’)

Small Example in PHP

  //This reverses and then swaps every other char
  function SwapOrder($in){
      $Split = str_split(strrev($in));
      for ($i = 0; $i < count($Split); $i+=2) {
          $x .= $Split[$i+1].$Split[$i];
      return $x;
  //makes the littleEndian
  function littleEndian($value){
      return implode (unpack('H*',pack("V*",$value)));
  $version = littleEndian(1);
  $prevBlockHash = SwapOrder('00000000000008a3a41b85b8b29ad444def299fee21793cd8b9e567eab02cd81');
  $rootHash = SwapOrder('2b12fcf1b09288fcaff797d71e950e71ae42b91e8bdb2304758dfcffc2b620e3');
  $time = littleEndian(1305998791);
  $bits = littleEndian(440711666); 
  $nonce = littleEndian(2504433986); 
  //concat it all
  $header_hex = $version . $prevBlockHash . $rootHash . $time . $bits . $nonce;
  //convert from hex to binary 
  $header_bin  = hex2bin($header_hex);
  //hash it then convert from hex to binary 
  $pass1 = hex2bin(  hash('sha256', $header_bin )  );
  //Hash it for the seconded time
  $pass2 = hash('sha256', $pass1);
  //fix the order
  $FinalHash = SwapOrder($pass2);
  echo   $FinalHash;
{Code with Code@ter}
Give Your Reviews

Leave a Reply