SHARE
TWEET

hashblock.c

a guest Jun 24th, 2013 20,361 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /*
  2. #Copyright (c) 2011, Joseph Matheney
  3. #All rights reserved.
  4.  
  5. #Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
  6.  
  7. #    Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
  8. #    Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
  9.  
  10. #THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  11. */
  12.  
  13. #ifdef fail
  14.         #!/bin/bash
  15.         # NOTE you can chmod 0755 this file and then execute it to compile (or just copy and paste)
  16.         gcc -o hashblock hashblock.c -lssl
  17.         exit 0
  18. #endif
  19.  
  20. #include <openssl/sha.h>
  21. #include <stdio.h>
  22. #include <string.h>
  23. #include <stdlib.h>
  24.  
  25. // this is the block header, it is 80 bytes long (steal this code)
  26. typedef struct block_header {
  27.         unsigned int    version;
  28.         // dont let the "char" fool you, this is binary data not the human readable version
  29.         unsigned char   prev_block[32];
  30.         unsigned char   merkle_root[32];
  31.         unsigned int    timestamp;
  32.         unsigned int    bits;
  33.         unsigned int    nonce;
  34. } block_header;
  35.  
  36.  
  37. // we need a helper function to convert hex to binary, this function is unsafe and slow, but very readable (write something better)
  38. void hex2bin(unsigned char* dest, unsigned char* src)
  39. {
  40.         unsigned char bin;
  41.         int c, pos;
  42.         char buf[3];
  43.  
  44.         pos=0;
  45.         c=0;
  46.         buf[2] = 0;
  47.         while(c < strlen(src))
  48.         {
  49.                 // read in 2 characaters at a time
  50.                 buf[0] = src[c++];
  51.                 buf[1] = src[c++];
  52.                 // convert them to a interger and recast to a char (uint8)
  53.                 dest[pos++] = (unsigned char)strtol(buf, NULL, 16);
  54.         }
  55.        
  56. }
  57.  
  58. // this function is mostly useless in a real implementation, were only using it for demonstration purposes
  59. void hexdump(unsigned char* data, int len)
  60. {
  61.         int c;
  62.        
  63.         c=0;
  64.         while(c < len)
  65.         {
  66.                 printf("%.2x", data[c++]);
  67.         }
  68.         printf("\n");
  69. }
  70.  
  71. // this function swaps the byte ordering of binary data, this code is slow and bloated (write your own)
  72. void byte_swap(unsigned char* data, int len) {
  73.         int c;
  74.         unsigned char tmp[len];
  75.        
  76.         c=0;
  77.         while(c<len)
  78.         {
  79.                 tmp[c] = data[len-(c+1)];
  80.                 c++;
  81.         }
  82.        
  83.         c=0;
  84.         while(c<len)
  85.         {
  86.                 data[c] = tmp[c];
  87.                 c++;
  88.         }
  89. }
  90.  
  91. int main() {
  92.         // start with a block header struct
  93.         block_header header;
  94.        
  95.         // we need a place to store the checksums
  96.         unsigned char hash1[SHA256_DIGEST_LENGTH];
  97.         unsigned char hash2[SHA256_DIGEST_LENGTH];
  98.        
  99.         // you should be able to reuse these, but openssl sha256 is slow, so your probbally not going to implement this anyway
  100.     SHA256_CTX sha256_pass1, sha256_pass2;
  101.  
  102.  
  103.         // we are going to supply the block header with the values from the generation block 0
  104.         header.version =        1;
  105.         hex2bin(header.prev_block,              "0000000000000000000000000000000000000000000000000000000000000000");
  106.         hex2bin(header.merkle_root,             "4a5e1e4baab89f3a32518a88c31bc87f618f76673e2cc77ab2127b7afdeda33b");
  107.         header.timestamp =      1231006505;
  108.         header.bits =           486604799;
  109.         header.nonce =          2083236893;
  110.        
  111.         // the endianess of the checksums needs to be little, this swaps them form the big endian format you normally see in block explorer
  112.         byte_swap(header.prev_block, 32);
  113.         byte_swap(header.merkle_root, 32);
  114.        
  115.         // dump out some debug data to the terminal
  116.         printf("sizeof(block_header) = %d\n", (int) sizeof(block_header));
  117.         printf("Block header (in human readable hexadecimal representation): ");
  118.         hexdump((unsigned char*)&header, sizeof(block_header));
  119.  
  120.         // Use SSL's sha256 functions, it needs to be initialized
  121.     SHA256_Init(&sha256_pass1);
  122.     // then you 'can' feed data to it in chuncks, but here were just making one pass cause the data is so small
  123.     SHA256_Update(&sha256_pass1, (unsigned char*)&header, sizeof(block_header));
  124.     // this ends the sha256 session and writes the checksum to hash1
  125.     SHA256_Final(hash1, &sha256_pass1);
  126.        
  127.         // to display this, we want to swap the byte order to big endian
  128.         byte_swap(hash1, SHA256_DIGEST_LENGTH);
  129.         printf("Useless First Pass Checksum: ");
  130.         hexdump(hash1, SHA256_DIGEST_LENGTH);
  131.  
  132.         // but to calculate the checksum again, we need it in little endian, so swap it back
  133.         byte_swap(hash1, SHA256_DIGEST_LENGTH);
  134.        
  135.     //same as above
  136.     SHA256_Init(&sha256_pass2);
  137.     SHA256_Update(&sha256_pass2, hash1, SHA256_DIGEST_LENGTH);
  138.     SHA256_Final(hash2, &sha256_pass2);
  139.        
  140.         byte_swap(hash2, SHA256_DIGEST_LENGTH);
  141.         printf("Target Second Pass Checksum: ");
  142.         hexdump(hash2, SHA256_DIGEST_LENGTH);
  143.  
  144.         return 0;
  145. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top