-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathblock.cpp
More file actions
126 lines (118 loc) · 3.8 KB
/
block.cpp
File metadata and controls
126 lines (118 loc) · 3.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
#include "block.h"
#include "id.h"
#include "hash.h"
#include "crypto.h"
#include <time.h>
#include <stdio.h>
#include <string.h>
#include <openssl/pem.h>
#include <openssl/bio.h>
char pub_pem_ext[] = "-pub.pem";
bool hashcmp(uint8_t *hash1, uint8_t *hash2) { // returns true if hashes are different
for(int i = 0; i<32; i++) {
if( hash1[i]!=hash2[i] ) {
return true;
}
}
return false;
}
bool verifyHash(block* blk) {
uint8_t blkhash[128];
hashblk(blkhash, blk);
for(int i = 0; i< blk->difficulty; i++) {
if(blkhash[i]!=0x00) {
return false;
}
}
return true;
}
void genesisblk(block* blk) { // generates genesis block
//blk = (block*)malloc(sizeof(block));
(*blk).difficulty = 0x00;
(blk->src)[0] = 0x00;
(*blk).ts = time(NULL);
(*blk).nonce = 0x00;
for(int i = 0; i<32; i++) {
(*blk).phash[i] = 0x00;
(*blk).pl[i] = 0x00;
}
for(int i = 31; i<128; i++) {
(*blk).pl[i] = 0x00;
}
}
void printblk(block blk) { // prints details of the block
printf("-------------------------------------------\n");
printf("block difficulty: %u\n", blk.difficulty);
printf("src: %s\n", blk.src);
printf("timestamp: %lu\n", blk.ts);
if(blk.src[0]!=0x00) {
RSA *rsa_pub;
char *pub_key_file = (char*) malloc(15);
memcpy(pub_key_file, blk.src, strlen(blk.src));
memcpy(pub_key_file+strlen(blk.src), pub_pem_ext, strlen(pub_pem_ext)+1);
rsa_pub = createRSAWithFilename(pub_key_file, 1);
unsigned char *dec = (unsigned char*)malloc(RSA_size(rsa_pub));
int dec_len;
dec_len = RSA_public_decrypt(128, blk.pl, dec, rsa_pub, RSA_PKCS1_PADDING);
if(dec_len<0) {
printf("Error in RSA_public_decrypt()\n");
}
printf("message: ");
for(int i = 0; i<dec_len; i++) {
printf("%c", *(dec+i));
}
printf("\n");
free(dec);
}
printf("nonce: %u\n", blk.nonce);
printf("previous hash: ");
for(int i=0;i<5;i++) {
printf("%02x", blk.phash[i]);
}
printf("\n");
printf("-------------------------------------------\n");
}
void hashblk(uint8_t* bhash, block* blk) { // returns hash of nonce, pl and phash
uint8_t blkbitrep[164];
memcpy(blkbitrep, &(blk->nonce), sizeof(blk->nonce));
memcpy(blkbitrep+sizeof(blk->nonce), blk->pl, sizeof(blk->pl));
memcpy(blkbitrep+sizeof(blk->nonce)+sizeof(blk->pl), blk->phash, sizeof(blk->phash));
sha256(bhash, blkbitrep, sizeof(blkbitrep));
}
void genblk(block* newblk, block* prevblk, char* msg, uint8_t len, RSA *rsa_pri) { //generate new linked block
//newblk = (block*)malloc(sizeof(block));
newblk->difficulty = 0x01;
newblk->nonce = 0x00;
hashblk(newblk->phash, prevblk);
newblk->ts = time(NULL);
memcpy(newblk->src, host_id, 8);
unsigned char *cip = (unsigned char*)malloc(RSA_size(rsa_pri));
if(RSA_private_encrypt(strlen(msg), (const unsigned char*)msg, cip, rsa_pri, RSA_PKCS1_PADDING) != 128 ) {
printf("Error in RSA_private_encrypt()\n");
}
memcpy(newblk->pl, cip, 128);
while(!verifyHash(newblk)) {
(newblk->nonce)++;
}
}
void genblk_hash(block* newblk, uint8_t* phash, char* msg, uint8_t len, RSA *rsa_pri) { //generate new linked block with previous hash as phash
//newblk = (block*)malloc(sizeof(block));
newblk->difficulty = 0x01;
newblk->nonce = 0x00;
memcpy(newblk->phash, phash, 32);
newblk->ts = time(NULL);
memcpy(newblk->src, host_id, 8);
unsigned char *cip = (unsigned char*)malloc(RSA_size(rsa_pri));
if(RSA_private_encrypt(strlen(msg), (const unsigned char*)msg, cip, rsa_pri, RSA_PKCS1_PADDING) != 128 ) {
printf("Error in RSA_private_encrypt()\n");
}
memcpy(newblk->pl, cip, 128);
printf("Generate true block?\n");
int x;
scanf(" %d", &x);
if(x!=0) {
while(!verifyHash(newblk)) {
(newblk->nonce)++;
}
}
}