#include #include #include constexpr void compute_md5_table(uint32_t*, size_t); class Block { public: char* data; char* hash; char* previous_hash; Block(char*, char*); char* hmac(char*); }; Block::Block(char* data, char* prev) { this->data = data; this->hash = this->hmac(data); this->previous_hash = prev; } char* Block::hmac(char* input) { const size_t _TABLE_SIZE = 64; uint32_t table[_TABLE_SIZE]; compute_md5_table(table, _TABLE_SIZE); // values are taken straight from wikipedia size_t a0 = 0x67452301; // A size_t b0 = 0xefcdab89; // B size_t c0 = 0x98badcfe; // C size_t d0 = 0x10325476; // D // lots of reallocs inc size_t msg_len = strlen(input) + 1; uint8_t* buf = (uint8_t*)malloc(msg_len /* for the '1' bit */); memcpy(buf, input, msg_len); buf[strlen(input)] = 0x80; // adding in the '1' bit along with 7 bits for padding // calculate length of final message content with all padding size_t message_len_bits = msg_len * 8; while((message_len_bits) % 512 != 64 ) { message_len_bits += 8; } // prepare out new hash buffer which has a mutaable state uint8_t* hash_buf = (uint8_t*)malloc(message_len_bits / 8); memcpy(hash_buf, buf, msg_len); size_t hunk_count = msg_len/8; for(size_t i =0; i < hunk_count; i+=8) { uint8_t* hunk = &hash_buf[i]; // length should always be 8(bytes)/512 bits // split the hunk into pieces uint32_t parts[16]; for(size_t p = 0; p < 16; p+=32) { parts[p] = (uint32_t)hunk[p]; } for(size_t z = 0;z<16;z++) { printf("%08X ", parts[z]); } printf("\n"); } free(hash_buf); free(buf); return NULL; } int main(void) { Block* b = new Block("something", NULL); return 0; } constexpr void compute_md5_table(uint32_t* table, size_t size) { for(int i = 0; i < 64; i++) { table[i] = floor( pow(2,32) * abs( sin(i+1) ) ); } }