SHOW:
|
|
- or go back to the newest paste.
1 | #Copyright (c) 2011, Joseph Matheney | |
2 | #All rights reserved. | |
3 | ||
4 | #Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: | |
5 | ||
6 | # Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. | |
7 | # 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. | |
8 | ||
9 | #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. | |
10 | ||
11 | #ifdef fail | |
12 | #!/bin/bash | |
13 | # NOTE you can chmod 0755 this file and then execute it to compile (or just copy and paste) | |
14 | gcc -o hashblock hashblock.c -lssl | |
15 | exit 0 | |
16 | #endif | |
17 | ||
18 | #include <openssl/sha.h> | |
19 | #include <stdio.h> | |
20 | #include <string.h> | |
21 | #include <stdlib.h> | |
22 | ||
23 | // this is the block header, it is 80 bytes long (steal this code) | |
24 | typedef struct block_header { | |
25 | unsigned int version; | |
26 | // dont let the "char" fool you, this is binary data not the human readable version | |
27 | unsigned char prev_block[32]; | |
28 | unsigned char merkle_root[32]; | |
29 | unsigned int timestamp; | |
30 | unsigned int bits; | |
31 | unsigned int nonce; | |
32 | } block_header; | |
33 | ||
34 | ||
35 | // we need a helper function to convert hex to binary, this function is unsafe and slow, but very readable (write something better) | |
36 | void hex2bin(unsigned char* dest, unsigned char* src) | |
37 | { | |
38 | unsigned char bin; | |
39 | int c, pos; | |
40 | char buf[3]; | |
41 | ||
42 | pos=0; | |
43 | c=0; | |
44 | buf[2] = 0; | |
45 | while(c < strlen(src)) | |
46 | { | |
47 | // read in 2 characaters at a time | |
48 | buf[0] = src[c++]; | |
49 | buf[1] = src[c++]; | |
50 | // convert them to a interger and recast to a char (uint8) | |
51 | dest[pos++] = (unsigned char)strtol(buf, NULL, 16); | |
52 | } | |
53 | ||
54 | } | |
55 | ||
56 | // this function is mostly useless in a real implementation, were only using it for demonstration purposes | |
57 | void hexdump(unsigned char* data, int len) | |
58 | { | |
59 | int c; | |
60 | ||
61 | c=0; | |
62 | while(c < len) | |
63 | { | |
64 | printf("%.2x", data[c++]); | |
65 | } | |
66 | printf("\n"); | |
67 | } | |
68 | ||
69 | // this function swaps the byte ordering of binary data, this code is slow and bloated (write your own) | |
70 | void byte_swap(unsigned char* data, int len) { | |
71 | int c; | |
72 | unsigned char tmp[len]; | |
73 | ||
74 | c=0; | |
75 | while(c<len) | |
76 | { | |
77 | tmp[c] = data[len-(c+1)]; | |
78 | c++; | |
79 | } | |
80 | ||
81 | c=0; | |
82 | while(c<len) | |
83 | { | |
84 | data[c] = tmp[c]; | |
85 | c++; | |
86 | } | |
87 | } | |
88 | ||
89 | int main() { | |
90 | // start with a block header struct | |
91 | block_header header; | |
92 | ||
93 | // we need a place to store the checksums | |
94 | unsigned char hash1[SHA256_DIGEST_LENGTH]; | |
95 | unsigned char hash2[SHA256_DIGEST_LENGTH]; | |
96 | ||
97 | // you should be able to reuse these, but openssl sha256 is slow, so your probbally not going to implement this anyway | |
98 | SHA256_CTX sha256_pass1, sha256_pass2; | |
99 | ||
100 | ||
101 | // we are going to supply the block header with the values from the generation block 0 | |
102 | header.version = 1; | |
103 | hex2bin(header.prev_block, "0000000000000000000000000000000000000000000000000000000000000000"); | |
104 | hex2bin(header.merkle_root, "4a5e1e4baab89f3a32518a88c31bc87f618f76673e2cc77ab2127b7afdeda33b"); | |
105 | header.timestamp = 1231006505; | |
106 | header.bits = 486604799; | |
107 | header.nonce = 2083236893; | |
108 | ||
109 | // the endianess of the checksums needs to be little, this swaps them form the big endian format you normally see in block explorer | |
110 | byte_swap(header.prev_block, 32); | |
111 | byte_swap(header.merkle_root, 32); | |
112 | ||
113 | // dump out some debug data to the terminal | |
114 | printf("sizeof(block_header) = %d\n", sizeof(block_header)); | |
115 | printf("Block header (in human readable hexadecimal representation): "); | |
116 | hexdump((unsigned char*)&header, sizeof(block_header)); | |
117 | ||
118 | // Use SSL's sha256 functions, it needs to be initialized | |
119 | SHA256_Init(&sha256_pass1); | |
120 | // then you 'can' feed data to it in chuncks, but here were just making one pass cause the data is so small | |
121 | SHA256_Update(&sha256_pass1, (unsigned char*)&header, sizeof(block_header)); | |
122 | // this ends the sha256 session and writes the checksum to hash1 | |
123 | SHA256_Final(hash1, &sha256_pass1); | |
124 | ||
125 | // to display this, we want to swap the byte order to big endian | |
126 | byte_swap(hash1, SHA256_DIGEST_LENGTH); | |
127 | printf("Useless First Pass Checksum: "); | |
128 | hexdump(hash1, SHA256_DIGEST_LENGTH); | |
129 | ||
130 | // but to calculate the checksum again, we need it in little endian, so swap it back | |
131 | byte_swap(hash1, SHA256_DIGEST_LENGTH); | |
132 | ||
133 | //same as above | |
134 | SHA256_Init(&sha256_pass2); | |
135 | SHA256_Update(&sha256_pass2, hash1, SHA256_DIGEST_LENGTH); | |
136 | SHA256_Final(hash2, &sha256_pass2); | |
137 | ||
138 | byte_swap(hash2, SHA256_DIGEST_LENGTH); | |
139 | printf("Target Second Pass Checksum: "); | |
140 | hexdump(hash2, SHA256_DIGEST_LENGTH); | |
141 | ||
142 | return 0; | |
143 | } |