#include #include #include #include "sha256.h" int ArraySwap(BYTE *input, int size) { int index; BYTE byte_temp; for (index = 0; index < size / 2; index++) { byte_temp = input[index]; input[index] = input[SHA256_BLOCK_SIZE - 1 - index]; input[SHA256_BLOCK_SIZE - 1 - index] = byte_temp; } return 0; } struct BITCOIN_HEADER { char ver[4]; char HashPrevBlock[32]; char HashMerkleRoot[32]; char timestamp[4]; char target[4]; union { char nonce_str[4]; unsigned int nonce; }; }; BYTE buf[SHA256_BLOCK_SIZE]; extern int BitcoinMining(BYTE *output, BYTE const *input, int size); int main() { // int index; struct BITCOIN_HEADER bitcoin_header = { {0x01, 0x00, 0x00, 0x00}, {0x81, 0xcd, 0x02, 0xab, 0x7e, 0x56, 0x9e, 0x8b, 0xcd, 0x93, 0x17, 0xe2, 0xfe, 0x99, 0xf2, 0xde, 0x44, 0xd4, 0x9a, 0xb2, 0xb8, 0x85, 0x1b, 0xa4, 0xa3, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}, {0xe3, 0x20, 0xb6, 0xc2, 0xff, 0xfc, 0x8d, 0x75, 0x04, 0x23, 0xdb, 0x8b, 0x1e, 0xb9, 0x42, 0xae, 0x71, 0x0e, 0x95, 0x1e, 0xd7, 0x97, 0xf7, 0xaf, 0xfc, 0x88, 0x92, 0xb0, 0xf1, 0xfc, 0x12, 0x2b}, {0xc7, 0xf5, 0xd7, 0x4d}, {0xf2, 0xb9, 0x44, 0x1a}, }; // 0x42, 0xa1, 0x46, 0x95 // bitcoin_header.nonce = 0x9546a142; // BitcoinMining((unsigned char const *)&bitcoin_header, sizeof(bitcoin_header)); // ArraySwap(buf, sizeof(buf)); // for (index = 0; index < sizeof(buf); index++) // { // printf("%02x", (unsigned char)buf[index]); // } // printf("\n"); for (bitcoin_header.nonce = 0; bitcoin_header.nonce < 0xFFFFFFFFu; bitcoin_header.nonce++) { BitcoinBlockHashing(buf, (unsigned char const *)&bitcoin_header, sizeof(bitcoin_header)); if ((0 == *((unsigned int *)(buf + sizeof(buf) - 4))) && (0 == *((unsigned int *)(buf + sizeof(buf) - 8)))) { break; } } if (0xFFFFFFFFu != bitcoin_header.nonce ) { printf("mining success at nonce=0x%08X\n", bitcoin_header.nonce); } else { BitcoinBlockHashing(buf, (unsigned char const *)&bitcoin_header, sizeof(bitcoin_header)); if ((0 == *((unsigned int *)(buf + sizeof(buf) - 4))) && (0 == *((unsigned int *)(buf + sizeof(buf) - 8)))) { printf("mining success at nonce=0xFFFFFFFF\n"); } else { printf("mining failed!\n"); } } return 0; }