<?xml version="1.0"?>
<feed xmlns="http://www.w3.org/2005/Atom" xml:lang="en">
	<id>https://en.bitcoin.it/w/api.php?action=feedcontributions&amp;feedformat=atom&amp;user=RomanKnight</id>
	<title>Bitcoin Wiki - User contributions [en]</title>
	<link rel="self" type="application/atom+xml" href="https://en.bitcoin.it/w/api.php?action=feedcontributions&amp;feedformat=atom&amp;user=RomanKnight"/>
	<link rel="alternate" type="text/html" href="https://en.bitcoin.it/wiki/Special:Contributions/RomanKnight"/>
	<updated>2026-04-15T00:16:36Z</updated>
	<subtitle>User contributions</subtitle>
	<generator>MediaWiki 1.43.8</generator>
	<entry>
		<id>https://en.bitcoin.it/w/index.php?title=Block_hashing_algorithm&amp;diff=43698</id>
		<title>Block hashing algorithm</title>
		<link rel="alternate" type="text/html" href="https://en.bitcoin.it/w/index.php?title=Block_hashing_algorithm&amp;diff=43698"/>
		<updated>2014-01-12T00:07:36Z</updated>

		<summary type="html">&lt;p&gt;RomanKnight: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;When generating, you constantly hash the block header. The block is also occasionally updated as you are working on it. A block header contains these fields:&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! Field&lt;br /&gt;
! Purpose&lt;br /&gt;
! Updated when...&lt;br /&gt;
! Size (Bytes)&lt;br /&gt;
|-&lt;br /&gt;
|Version&lt;br /&gt;
|Block version number&lt;br /&gt;
|You upgrade the software and it specifies a new version&lt;br /&gt;
|4&lt;br /&gt;
|-&lt;br /&gt;
|hashPrevBlock&lt;br /&gt;
|256-bit hash of the previous block header&lt;br /&gt;
|A new block comes in&lt;br /&gt;
|32&lt;br /&gt;
|-&lt;br /&gt;
|hashMerkleRoot&lt;br /&gt;
|256-bit hash based on all of the transactions in the block&lt;br /&gt;
|A transaction is accepted&lt;br /&gt;
|32&lt;br /&gt;
|-&lt;br /&gt;
|Time&lt;br /&gt;
|Current timestamp as seconds since 1970-01-01T00:00 UTC&lt;br /&gt;
|Every few seconds&lt;br /&gt;
|4&lt;br /&gt;
|-&lt;br /&gt;
|Bits&lt;br /&gt;
|Current [[target]] in compact format&lt;br /&gt;
|The [[difficulty]] is adjusted&lt;br /&gt;
|4&lt;br /&gt;
|-&lt;br /&gt;
|Nonce&lt;br /&gt;
|32-bit number (starts at 0)&lt;br /&gt;
|A hash is tried (increments)&lt;br /&gt;
|4&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
The body of the block contains the transactions. These are hashed only indirectly through the Merkle root. Because transactions aren&#039;t hashed directly, hashing a block with 1 transaction takes exactly the same amount of effort as hashing a block with 10,000 transactions.&lt;br /&gt;
&lt;br /&gt;
The compact format of target is a special kind of floating-point encoding using 3 bytes mantissa, the leading byte as exponent (where only the 5 lowest bits are used) and its base is 256.&lt;br /&gt;
Most of these fields will be the same for all users. There might be some minor variation in the timestamps. The nonce will usually be different, but it increases in a strictly linear way. &amp;quot;Nonce&amp;quot; starts at 0 and is incremented for each hash. Whenever Nonce overflows (which it does frequently), the extraNonce portion of the generation transaction is incremented, which changes the Merkle root.&lt;br /&gt;
&lt;br /&gt;
Given just those fields, people would frequently generate the exact same sequence of hashes as each other and the fastest CPU would almost always win. However, it is (nearly) impossible for two people to have the same Merkle root because the first transaction in your block is a generation &amp;quot;sent&amp;quot; to one of &#039;&#039;your&#039;&#039; unique Bitcoin addresses. Since your block is different from everyone else&#039;s blocks, you are (nearly) guaranteed to produce different hashes. Every hash you calculate has the same chance of winning as every other hash calculated by the network.&lt;br /&gt;
&lt;br /&gt;
Bitcoin uses: SHA256(SHA256(Block_Header)) but you have to be careful about byte-order.&lt;br /&gt;
&lt;br /&gt;
For example, this python code will calculate the hash of the block with the smallest hash as of June 2011, [http://blockexplorer.com/block/00000000000000001e8d6829a8a21adc5d38d0a473b144b6765798e61f98bd1d Block 125552].  The header is built from the six fields described above, concatenated together as little-endian values in hex notation:&lt;br /&gt;
&lt;br /&gt;
   &amp;gt;&amp;gt;&amp;gt; import hashlib&lt;br /&gt;
   &amp;gt;&amp;gt;&amp;gt; header_hex = (&amp;quot;01000000&amp;quot; +&lt;br /&gt;
     &amp;quot;81cd02ab7e569e8bcd9317e2fe99f2de44d49ab2b8851ba4a308000000000000&amp;quot; +&lt;br /&gt;
     &amp;quot;e320b6c2fffc8d750423db8b1eb942ae710e951ed797f7affc8892b0f1fc122b&amp;quot; +&lt;br /&gt;
     &amp;quot;c7f5d74d&amp;quot; +&lt;br /&gt;
     &amp;quot;f2b9441a&amp;quot; +&lt;br /&gt;
      &amp;quot;42a14695&amp;quot;)&lt;br /&gt;
   &amp;gt;&amp;gt;&amp;gt; header_bin = header_hex.decode(&#039;hex&#039;)&lt;br /&gt;
   &amp;gt;&amp;gt;&amp;gt; hash = hashlib.sha256(hashlib.sha256(header_bin).digest()).digest()&lt;br /&gt;
   &amp;gt;&amp;gt;&amp;gt; hash.encode(&#039;hex_codec&#039;)&lt;br /&gt;
   &#039;1dbd981fe6985776b644b173a4d0385ddc1aa2a829688d1e0000000000000000&#039;&lt;br /&gt;
   &amp;gt;&amp;gt;&amp;gt; hash[::-1].encode(&#039;hex_codec&#039;)&lt;br /&gt;
   &#039;00000000000000001e8d6829a8a21adc5d38d0a473b144b6765798e61f98bd1d&#039;&lt;br /&gt;
&lt;br /&gt;
Note that the actual hash, which is a 256-bit number, has lots of leading zero bits. When stored or printed as a big-endian hexadecimal constant, but it has leading zero bytes and if stored or printed  as little-endian, these are the trailing zero bytes. E.g. if interpretation as a string -- lowest (or start of) string address keeps lowest significant byte, thus little-endian.&lt;br /&gt;
The output of [http://blockexplorer.com blockexplorer] displays the hash values as big-endians numbers as notation for numbers is usual -- leading digits are the most significant digits read from left to right.&lt;br /&gt;
&lt;br /&gt;
For another example, [http://pastebin.com/bW3fQA2a here] is a version in plain C without any optimization, threading or error checking.&lt;br /&gt;
&lt;br /&gt;
Here is the same example in plain PHP without any optimization.&lt;br /&gt;
&lt;br /&gt;
  &amp;lt;?&lt;br /&gt;
    //This reverses and then swaps every other char&lt;br /&gt;
     function SwapOrder($in){&lt;br /&gt;
         $Split = str_split(strrev($in));&lt;br /&gt;
         $x=&#039;&#039;;&lt;br /&gt;
         for ($i = 0; $i &amp;lt; count($Split); $i+=2) {&lt;br /&gt;
             $x .= $Split[$i+1].$Split[$i];&lt;br /&gt;
         } &lt;br /&gt;
         return $x;&lt;br /&gt;
     }&lt;br /&gt;
     &lt;br /&gt;
     //makes the littleEndian&lt;br /&gt;
     function littleEndian($value){&lt;br /&gt;
         return implode (unpack(&#039;H*&#039;,pack(&amp;quot;V*&amp;quot;,$value)));&lt;br /&gt;
     }&lt;br /&gt;
     &lt;br /&gt;
     $version = littleEndian(1);&lt;br /&gt;
     $prevBlockHash = SwapOrder(&#039;00000000000008a3a41b85b8b29ad444def299fee21793cd8b9e567eab02cd81&#039;);&lt;br /&gt;
     $rootHash = SwapOrder(&#039;2b12fcf1b09288fcaff797d71e950e71ae42b91e8bdb2304758dfcffc2b620e3&#039;);&lt;br /&gt;
     $time = littleEndian(1305998791);&lt;br /&gt;
     $bits =littleEndian( 440711666); &lt;br /&gt;
     $nonce = littleEndian(2504433986); &lt;br /&gt;
    &lt;br /&gt;
     //concat it all&lt;br /&gt;
     $header_hex = $version . $prevBlockHash . $rootHash . $time . $bits . $nonce;&lt;br /&gt;
    &lt;br /&gt;
     //convert from hex to binary &lt;br /&gt;
     $header_bin  = hex2bin($header_hex);&lt;br /&gt;
      //hash it then convert from hex to binary &lt;br /&gt;
     $pass1 = hex2bin(  hash(&#039;sha256&#039;, $header_bin )  );&lt;br /&gt;
     //Hash it for the seconded time&lt;br /&gt;
     $pass2 = hash(&#039;sha256&#039;, $pass1);&lt;br /&gt;
     //fix the order&lt;br /&gt;
     $FinalHash = SwapOrder($pass2);&lt;br /&gt;
  &lt;br /&gt;
     echo   $FinalHash;&lt;br /&gt;
   ?&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
[[Category:Technical]]&lt;/div&gt;</summary>
		<author><name>RomanKnight</name></author>
	</entry>
	<entry>
		<id>https://en.bitcoin.it/w/index.php?title=Talk:Block_hashing_algorithm&amp;diff=43695</id>
		<title>Talk:Block hashing algorithm</title>
		<link rel="alternate" type="text/html" href="https://en.bitcoin.it/w/index.php?title=Talk:Block_hashing_algorithm&amp;diff=43695"/>
		<updated>2014-01-11T15:24:25Z</updated>

		<summary type="html">&lt;p&gt;RomanKnight: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;What is licensing/copyright information on that code? Public domain?&lt;br /&gt;
&lt;br /&gt;
&amp;quot;Because transactions aren&#039;t hashed directly, hashing a block with 1 transaction takes exactly the same amount of effort as hashing a block with 10,000 transactions.&amp;quot;&lt;br /&gt;
&lt;br /&gt;
Doesn&#039;t it take more effort to calculate the Merkle root for 10,000 transactions than it does than for 1 transaction? Of course, you only have to do this once per block, but it&#039;s misleading. It seems you&#039;re using &amp;quot;hashing a block&amp;quot; to specifically mean the (iterative) hash calculation, not the process of block generation. I recommend rewording to :&lt;br /&gt;
&lt;br /&gt;
&amp;quot;These are hashed only indirectly through the Merkle root. Because transactions aren&#039;t hashed directly, hashing a block with 1 transaction takes exactly the same amount of effort as hashing a block with 10,000 transactions, once the Merkle root of those transactions is calculated.&amp;quot; [[User:Therealplato|Therealplato]] 23:41, 28 January 2012 (GMT)&lt;br /&gt;
&lt;br /&gt;
I added PHP code this code will help beginners with what is going on. The code is simple and well documented anyone regardless of PHP knowledge should be able to read this and know how to hash Bitcoins. [[User:RomanKnight|RomanKnight]] 10:23, 11 January 2014 (EST)&lt;/div&gt;</summary>
		<author><name>RomanKnight</name></author>
	</entry>
	<entry>
		<id>https://en.bitcoin.it/w/index.php?title=Block_hashing_algorithm&amp;diff=43694</id>
		<title>Block hashing algorithm</title>
		<link rel="alternate" type="text/html" href="https://en.bitcoin.it/w/index.php?title=Block_hashing_algorithm&amp;diff=43694"/>
		<updated>2014-01-11T15:20:17Z</updated>

		<summary type="html">&lt;p&gt;RomanKnight: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;When generating, you constantly hash the block header. The block is also occasionally updated as you are working on it. A block header contains these fields:&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! Field&lt;br /&gt;
! Purpose&lt;br /&gt;
! Updated when...&lt;br /&gt;
! Size (Bytes)&lt;br /&gt;
|-&lt;br /&gt;
|Version&lt;br /&gt;
|Block version number&lt;br /&gt;
|You upgrade the software and it specifies a new version&lt;br /&gt;
|4&lt;br /&gt;
|-&lt;br /&gt;
|hashPrevBlock&lt;br /&gt;
|256-bit hash of the previous block header&lt;br /&gt;
|A new block comes in&lt;br /&gt;
|32&lt;br /&gt;
|-&lt;br /&gt;
|hashMerkleRoot&lt;br /&gt;
|256-bit hash based on all of the transactions in the block&lt;br /&gt;
|A transaction is accepted&lt;br /&gt;
|32&lt;br /&gt;
|-&lt;br /&gt;
|Time&lt;br /&gt;
|Current timestamp as seconds since 1970-01-01T00:00 UTC&lt;br /&gt;
|Every few seconds&lt;br /&gt;
|4&lt;br /&gt;
|-&lt;br /&gt;
|Bits&lt;br /&gt;
|Current [[target]] in compact format&lt;br /&gt;
|The [[difficulty]] is adjusted&lt;br /&gt;
|4&lt;br /&gt;
|-&lt;br /&gt;
|Nonce&lt;br /&gt;
|32-bit number (starts at 0)&lt;br /&gt;
|A hash is tried (increments)&lt;br /&gt;
|4&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
The body of the block contains the transactions. These are hashed only indirectly through the Merkle root. Because transactions aren&#039;t hashed directly, hashing a block with 1 transaction takes exactly the same amount of effort as hashing a block with 10,000 transactions.&lt;br /&gt;
&lt;br /&gt;
The compact format of target is a special kind of floating-point encoding using 3 bytes mantissa, the leading byte as exponent (where only the 5 lowest bits are used) and its base is 256.&lt;br /&gt;
Most of these fields will be the same for all users. There might be some minor variation in the timestamps. The nonce will usually be different, but it increases in a strictly linear way. &amp;quot;Nonce&amp;quot; starts at 0 and is incremented for each hash. Whenever Nonce overflows (which it does frequently), the extraNonce portion of the generation transaction is incremented, which changes the Merkle root.&lt;br /&gt;
&lt;br /&gt;
Given just those fields, people would frequently generate the exact same sequence of hashes as each other and the fastest CPU would almost always win. However, it is (nearly) impossible for two people to have the same Merkle root because the first transaction in your block is a generation &amp;quot;sent&amp;quot; to one of &#039;&#039;your&#039;&#039; unique Bitcoin addresses. Since your block is different from everyone else&#039;s blocks, you are (nearly) guaranteed to produce different hashes. Every hash you calculate has the same chance of winning as every other hash calculated by the network.&lt;br /&gt;
&lt;br /&gt;
Bitcoin uses: SHA256(SHA256(Block_Header)) but you have to be careful about byte-order.&lt;br /&gt;
&lt;br /&gt;
For example, this python code will calculate the hash of the block with the smallest hash as of June 2011, [http://blockexplorer.com/block/00000000000000001e8d6829a8a21adc5d38d0a473b144b6765798e61f98bd1d Block 125552].  The header is built from the six fields described above, concatenated together as little-endian values in hex notation:&lt;br /&gt;
&lt;br /&gt;
   &amp;gt;&amp;gt;&amp;gt; import hashlib&lt;br /&gt;
   &amp;gt;&amp;gt;&amp;gt; header_hex = (&amp;quot;01000000&amp;quot; +&lt;br /&gt;
     &amp;quot;81cd02ab7e569e8bcd9317e2fe99f2de44d49ab2b8851ba4a308000000000000&amp;quot; +&lt;br /&gt;
     &amp;quot;e320b6c2fffc8d750423db8b1eb942ae710e951ed797f7affc8892b0f1fc122b&amp;quot; +&lt;br /&gt;
     &amp;quot;c7f5d74d&amp;quot; +&lt;br /&gt;
     &amp;quot;f2b9441a&amp;quot; +&lt;br /&gt;
      &amp;quot;42a14695&amp;quot;)&lt;br /&gt;
   &amp;gt;&amp;gt;&amp;gt; header_bin = header_hex.decode(&#039;hex&#039;)&lt;br /&gt;
   &amp;gt;&amp;gt;&amp;gt; hash = hashlib.sha256(hashlib.sha256(header_bin).digest()).digest()&lt;br /&gt;
   &amp;gt;&amp;gt;&amp;gt; hash.encode(&#039;hex_codec&#039;)&lt;br /&gt;
   &#039;1dbd981fe6985776b644b173a4d0385ddc1aa2a829688d1e0000000000000000&#039;&lt;br /&gt;
   &amp;gt;&amp;gt;&amp;gt; hash[::-1].encode(&#039;hex_codec&#039;)&lt;br /&gt;
   &#039;00000000000000001e8d6829a8a21adc5d38d0a473b144b6765798e61f98bd1d&#039;&lt;br /&gt;
&lt;br /&gt;
Note that the actual hash, which is a 256-bit number, has lots of leading zero bits. When stored or printed as a big-endian hexadecimal constant, but it has leading zero bytes and if stored or printed  as little-endian, these are the trailing zero bytes. E.g. if interpretation as a string -- lowest (or start of) string address keeps lowest significant byte, thus little-endian.&lt;br /&gt;
The output of [http://blockexplorer.com blockexplorer] displays the hash values as big-endians numbers as notation for numbers is usual -- leading digits are the most significant digits read from left to right.&lt;br /&gt;
&lt;br /&gt;
For another example, [http://pastebin.com/bW3fQA2a here] is a version in plain C without any optimization, threading or error checking.&lt;br /&gt;
&lt;br /&gt;
Here is same example in plain PHP without any optimization.&lt;br /&gt;
&lt;br /&gt;
  &amp;lt;?&lt;br /&gt;
    //This reverses and then swaps every other char&lt;br /&gt;
     function SwapOrder($in){&lt;br /&gt;
         $Split = str_split(strrev($in));&lt;br /&gt;
         $x=&#039;&#039;;&lt;br /&gt;
         for ($i = 0; $i &amp;lt; count($Split); $i+=2) {&lt;br /&gt;
             $x .= $Split[$i+1].$Split[$i];&lt;br /&gt;
         } &lt;br /&gt;
         return $x;&lt;br /&gt;
     }&lt;br /&gt;
     &lt;br /&gt;
     //makes the littleEndian&lt;br /&gt;
     function littleEndian($value){&lt;br /&gt;
         return implode (unpack(&#039;H*&#039;,pack(&amp;quot;V*&amp;quot;,$value)));&lt;br /&gt;
     }&lt;br /&gt;
     &lt;br /&gt;
     $version = littleEndian(1);&lt;br /&gt;
     $prevBlockHash = SwapOrder(&#039;00000000000008a3a41b85b8b29ad444def299fee21793cd8b9e567eab02cd81&#039;);&lt;br /&gt;
     $rootHash = SwapOrder(&#039;2b12fcf1b09288fcaff797d71e950e71ae42b91e8bdb2304758dfcffc2b620e3&#039;);&lt;br /&gt;
     $time = littleEndian(1305998791);&lt;br /&gt;
     $bits =littleEndian( 440711666); &lt;br /&gt;
     $nonce = littleEndian(2504433986); &lt;br /&gt;
    &lt;br /&gt;
     //concat it all&lt;br /&gt;
     $header_hex = $version . $prevBlockHash . $rootHash . $time . $bits . $nonce;&lt;br /&gt;
    &lt;br /&gt;
     //convert from hex to binary &lt;br /&gt;
     $header_bin  = hex2bin($header_hex);&lt;br /&gt;
      //hash it then convert from hex to binary &lt;br /&gt;
     $pass1 = hex2bin(  hash(&#039;sha256&#039;, $header_bin )  );&lt;br /&gt;
     //Hash it for the seconded time&lt;br /&gt;
     $pass2 = hash(&#039;sha256&#039;, $pass1);&lt;br /&gt;
     //fix the order&lt;br /&gt;
     $FinalHash = SwapOrder($pass2);&lt;br /&gt;
  &lt;br /&gt;
     echo   $FinalHash;&lt;br /&gt;
   ?&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
[[Category:Technical]]&lt;/div&gt;</summary>
		<author><name>RomanKnight</name></author>
	</entry>
</feed>