#include <stdio.h>
#include <stdint.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <stdbool.h>
/*
* To build use gcc -std=c99 main.c
* This is C99 C code!
*/
bool debug;
bool found;
uint8_t *hex = NULL;
uint8_t *currentHash1 = NULL;
uint8_t *currentHash2 = NULL;
uint8_t *currentHash3 = NULL;
uint8_t *currentHash4 = NULL;
uint8_t *workingHash = NULL;
int workingHashMaxSize;
const int LOW = 0;
const int HIGH = 15;
int steps = 0;
void debug_messages();
void exercise_messages();
void setCurrentHash (uint8_t *hash1, uint8_t *hash2, uint8_t *hash3, uint8_t *hash4);
void bruteforce_messages();
void birthday_messages();
void print (uint8_t *old);
void print_s (uint8_t *old, int size);
void bruteNext (uint8_t *old, int cnt);
void brute();
void birthdayNext (uint8_t *old, int cnt);
void birthday();
void compare (uint8_t *newHash, uint8_t *msg, int cnt);
void hashAndPrint (uint8_t *msg, int size);
uint8_t* hash (uint8_t *msg, int size);
uint8_t* block (int blockID, uint8_t *xi, uint8_t *Hi);
/*
* Prints solutions to the debug questions given on the walk through
* Select debug = true for extended output.
*/
void debug_messages() {
int size;
printf("\nDebug Message i\n");
uint8_t msg1[] = {0xC,0x5,0x4,0xF,0xF,0xD,0xD,0xF};
size = sizeof(msg1)/sizeof(uint8_t);
hashAndPrint(msg1,size);
printf("\nDebug Message ii\n");
uint8_t msg2[] = {0xF,0x9,0x9,0xD,0xE,0xE,0x7,0x9};
size = sizeof(msg2)/sizeof(uint8_t);
hashAndPrint(msg2,size);
printf("\nDebug Message iii\n");
uint8_t msg3[] = {0xA,0x0,0x2,0x8,0xE,0xA,0x0,0xE,0x0,0xC,0x8,0xC,0xC,0x7,0xA,0x6,0x4,0x3,0x0,0x7,0xD,0x4,0x6,0x2,0xB,0xE,0xC,0xC};
size = sizeof(msg3)/sizeof(uint8_t);
hashAndPrint(msg3,size);
printf("\nDebug Message iv\n");
uint8_t msg4[] = {0xC,0xF,0xA,0xE,0x5,0xC,0x6,0xB,0x4,0xA,0xF,0xA,0x3,0xC,0xC,0x8,0x2,0x4,0x7,0xE,0x0,0x9,0x8,0xC,0x6,0x8,0x3,0xD};
size = sizeof(msg4)/sizeof(uint8_t);
hashAndPrint(msg4,size);
}
/*
* Prints solutions to the exercise questions given in the handout
* Select debug = true for extended output.
*/
void exercise_messages() {
int size;
printf("\nMessage i\n");
//0xFAB17E668145A9BCC783BFEE70013522734
uint8_t msg1[] = {0xF,0xA,0xB,0x1,0x7,0xE,0x6,0x6,0x8,0x1,0x4,0x5,0xA,0x9,0xB,0xC,0xC,0x7,0x8,0x3,0xB,0xF,0xE,0xE,0x7,0x0,0x0,0x1,0x3,0x5,0x2,0x2,0x7,0x3,0x4};
size = sizeof(msg1)/sizeof(uint8_t);
hashAndPrint(msg1,size);
printf("\nMessage ii\n");
//0xFAB17E668145A9BCC783BFEE70013521912
uint8_t msg2[] = {0xF,0xA,0xB,0x1,0x7,0xE,0x6,0x6,0x8,0x1,0x4,0x5,0xA,0x9,0xB,0xC,0xC,0x7,0x8,0x3,0xB,0xF,0xE,0xE,0x7,0x0,0x0,0x1,0x3,0x5,0x2,0x1,0x9,0x1,0x2};
size = sizeof(msg2)/sizeof(uint8_t);
hashAndPrint(msg2,size);
printf("\nMessage iii\n");
//0xEAB17E668145A9BCC783BFEE70013521558
uint8_t msg3[] = {0xE,0xA,0xB,0x1,0x7,0xE,0x6,0x6,0x8,0x1,0x4,0x5,0xA,0x9,0xB,0xC,0xC,0x7,0x8,0x3,0xB,0xF,0xE,0xE,0x7,0x0,0x0,0x1,0x3,0x5,0x2,0x1,0x5,0x5,0x8};
size = sizeof(msg3)/sizeof(uint8_t);
hashAndPrint(msg3,size);
printf("\nMessage iv\n");
//0x24A7B9FD294AFE569BB23197FFEAB67D
uint8_t msg4[] = {0x2,0x4,0xA,0x7,0xB,0x9,0xF,0xD,0x2,0x9,0x4,0xA,0xF,0xE,0x5,0x6,0x9,0xB,0xB,0x2,0x3,0x1,0x9,0x7,0xF,0xF,0xE,0xA,0xB,0x6,0x7,0xD};
size = sizeof(msg4)/sizeof(uint8_t);
hashAndPrint(msg4,size);
printf("\nMessage v\n");
//0x24A7B9FD294AFE569BB23197FFEAB67D
uint8_t msg5[] = {0x2,0x4,0xA,0x7,0xB,0x9,0xF,0xD,0x2,0x9,0x4,0xA,0xF,0xE,0x5,0x6,0x9,0xB,0xB,0x2,0x3,0x1,0x9,0x7,0xF,0xF,0xE,0xA,0xB,0x1,0x1,0x1,0x9,0x2,0x1,0x1,0x0,0xD};
size = sizeof(msg5)/sizeof(uint8_t);
hashAndPrint(msg5,size);
printf("\nMessage vi\n");
//0x24A7B9FD294AFE569BB23197FFEAB67D
uint8_t msg6[] = {0x3,0x4,0x8,0xF,0x3,0x0,0x5,0x1,0x8,0x2,0xA,0xE,0xF,0xD,0x9,0x8,0x6,0xC,0x4,0x5,0x8,0xA,0xB,0x3,0x9,0x4,0xB,0xB,0x4,0x3,0x9,0x9,0xA,0xF,0xB,0xD,0x2,0x3,0xC};
size = sizeof(msg6)/sizeof(uint8_t);
hashAndPrint(msg6,size);
printf("\nMessage vii\n");
//0x24A7B9FD294AFE569BB23197FFEAB67D
uint8_t msg7[] = {0x0,0x1,0x2,0x3,0x4,0x5,0x6,0x7,0x8,0x9,0xA,0xB,0xC,0xD,0xE,0xF,0xF,0xE,0xD,0xC,0xB,0xA,0x9,0x8,0x7,0x6,0x5,0x4,0x3,0x2,0x1,0x0,0x7,0xD,0xB,0x9,0x1,0xA,0x9,0xF};
size = sizeof(msg7)/sizeof(uint8_t);
hashAndPrint(msg7,size);
printf("\nMessage viii\n");
//0x24A7B9FD294AFE569BB23197FFEAB67D
uint8_t msg8[] = {0x0,0x0,0x1,0x1,0x2,0x2,0x3,0x3,0x4,0x4,0x5,0x5,0x6,0x6,0x7,0x7,0x8,0x8,0x9,0x9,0xA,0xA,0xB,0xB,0xC,0xC,0xD,0xD};
size = sizeof(msg8)/sizeof(uint8_t);
hashAndPrint(msg8,size);
printf("\nMessage ix\n");
//0x24A7B9FD294AFE569BB23197FFEAB67D
uint8_t msg9[] = {0xF,0xF,0xE,0xE,0xD,0xD,0xC,0xC,0xB,0xB,0xA,0xA,0x9,0x9,0x8,0x8,0x7,0x7,0x6,0x6,0x5,0x5,0x4,0x4,0x3,0x3,0x2,0x2,0x1,0x1,0x0,0x0,0x1,0x0,0x9,0x8};
size = sizeof(msg9)/sizeof(uint8_t);
hashAndPrint(msg9,size);
printf("\nMessage x\n");
//0x24A7B9FD294AFE569BB23197FFEAB67D
uint8_t msg10[] = {0x5,0x5,0x5,0x5,0x5,0x5,0x5,0x5,0x5,0x5,0x5,0x5,0x5,0x5,0x5,0x5,0x5,0x5,0x5,0x5,0x5,0x5,0x5,0x5,0x5,0x5,0x5,0x5,0x5,0x5,0x5,0x5,0x5,0x5,0x5,0x5,0x5,0x5,0x5,0x5,};
size = sizeof(msg10)/sizeof(uint8_t);
hashAndPrint(msg10,size);
}
void setCurrentHash (uint8_t *hash1, uint8_t *hash2, uint8_t *hash3, uint8_t *hash4) {
currentHash1 = hash1;
currentHash2 = hash2;
currentHash3 = hash3;
currentHash4 = hash4;
}
/*
* Prints collisions found for brute force question
* Select debug = true for extended output.
*/
void bruteforce_messages() {
clock_t start_h=clock();
uint8_t *hsh1 = NULL;
uint8_t *hsh2 = NULL;
uint8_t *hsh3 = NULL;
uint8_t *hsh4 = NULL;
printf("\nBegin Brute Force \n");
uint8_t bmsg1[] = {0xA,0xB,0xC,0xD,0xE,0xF,0x1,0x2,0x3,0x4};
uint8_t bmsg2[] = {0xF,0xA,0xB,0x1,0x7,0xE,0x6,0x6,0x8,0x1};
uint8_t bmsg3[] = {0xF,0xA,0xB,0x1,0x7,0xE,0x6,0x6,0x8,0x2};
uint8_t bmsg4[] = {0xE,0xA,0xB,0x1,0x7,0xE,0x6,0x6,0x8,0x1};
hsh1 = hash(bmsg1, 8);
hsh2 = hash(bmsg2, 8);
hsh3 = hash(bmsg3, 8);
hsh4 = hash(bmsg4, 8);
printf("Searching for the following hashes: \n");
printf("Hash i\n");
print(hsh1);
printf("Hash ii\n");
print(hsh2);
printf("Hash iii\n");
print(hsh3);
printf("Hash iv\n");
print(hsh4);
setCurrentHash(hsh1, hsh2, hsh3, hsh4);
brute();
free(hsh1);
free(hsh2);
free(hsh3);
free(hsh4);
clock_t end_h = clock();
double time_h = (double)(end_h-start_h)/CLOCKS_PER_SEC;
printf("Total Brute Force speed %f \n", time_h);
}
/*
* Prints collisions found for birthday attack question
* Select debug = true for extended output.
*/
void birthday_messages() {
clock_t start_h=clock();
uint8_t *hsh1 = NULL;
uint8_t *hsh2 = NULL;
uint8_t *hsh3 = NULL;
uint8_t *hsh4 = NULL;
printf("\nBegin Birthday Attack \n");
uint8_t bmsg1[] = {0xA,0xB,0xC,0xD,0xE,0xF,0x1,0x2,0x3,0x4};
uint8_t bmsg2[] = {0xF,0xA,0xB,0x1,0x7,0xE,0x6,0x6,0x8,0x1};
uint8_t bmsg3[] = {0xF,0xA,0xB,0x1,0x7,0xE,0x6,0x6,0x8,0x2};
uint8_t bmsg4[] = {0xE,0xA,0xB,0x1,0x7,0xE,0x6,0x6,0x8,0x1};
hsh1 = hash(bmsg1, 8);
hsh2 = hash(bmsg2, 8);
hsh3 = hash(bmsg3, 8);
hsh4 = hash(bmsg4, 8);
printf("Searching for the following hashes: \n");
printf("Hash i\n");
print(hsh1);
printf("Hash ii\n");
print(hsh2);
printf("Hash iii\n");
print(hsh3);
printf("Hash iv\n");
print(hsh4);
setCurrentHash(hsh1, hsh2, hsh3, hsh4);
birthday();
free(hsh1);
free(hsh2);
free(hsh3);
free(hsh4);
clock_t end_h = clock();
double time_h = (double)(end_h-start_h)/CLOCKS_PER_SEC;
printf("Total Brute Force speed %f \n", time_h);
}
/*
* Prints out a given hash
* Assumes hash size is 5
*/
void print (uint8_t *old) {
int size = 5;
for (int i = 0; i < size; i++) {
printf("%#x\n", old[i]);
}
}
/*
* Prints out a given hash
* does not assume size
*/
void print_s (uint8_t *old, int size) {
for (int i = 0; i < size; i++) {
printf("%#x\n", old[i]);
}
}
/*
* Will recursively brute force the next message
* Check workingHashMaxSize for the maximum size the message can be
*/
void bruteNext (uint8_t *old, int cnt) {
cnt += 1;
uint8_t *hsh = hash(old, cnt);
compare(hsh, old, cnt);
free(hsh);
if (cnt < workingHashMaxSize) {
for (int i = 0; i < 16; i++) {
old[cnt] = hex[i];
bruteNext(old,cnt);
}
}
}
/*
* Call this method to brute force new messages
* This method calls bruteNext recursively.
*/
void brute() {
int count = 0;
// Initially give the working hash capacity for 80 bits
// Each element in the array has 4 bits masked
// Effectively 40 bits of working data
workingHashMaxSize = 10;
workingHash = malloc(10);
if(!workingHash){
exit(1);
}
for (int i = 0; i < 16; i++) {
workingHash[0] = hex[i];
clock_t start_h=clock();
bruteNext(workingHash,count);
clock_t end_h = clock();
double time_h = (double)(end_h-start_h)/CLOCKS_PER_SEC;
printf("Speed of this round was: %f \n", time_h);
count = 0;
}
free(workingHash);
}
/*
* Will recursively brute force the next message
* Check workingHashMaxSize for the maximum size the message can be
*/
void birthdayNext (uint8_t *old, int cnt) {
cnt += 1;
steps += 1;
uint8_t *hsh = hash(old, cnt);
compare(hsh, old, cnt);
if (found == true) {
printf("number of steps taken to find collision was: %d \n", steps);
} else {
if (cnt < workingHashMaxSize) {
int i = rand() % (HIGH - LOW + 1) + LOW;
if ( i < 0 || i > 15 ) {
printf("Mistake in rand\n");
old[cnt] = 0x0;
} else {
old[cnt] = hex[i];
}
birthdayNext(old,cnt);
}
}
free(hsh);
}
/*
* Call this method to use the Birthday attack
* This method calls birthdayNext recursively.
*/
void birthday() {
printf("Birthday Attack!\n");
found = false;
int count = 0;
steps = 1;
// Initially give the working hash capacity for 80 bits
// Each element in the array has 4 bits masked
// Effectively 40 bits of working data
workingHashMaxSize = 10;
workingHash = malloc(10);
if(!workingHash){
exit(1);
}
while (!found) {
int i = rand() % (HIGH - LOW + 1) + LOW;
workingHash[0] = hex[i];
if ( i < 0 || i > 15 ) {
printf("Mistake in rand\n");
workingHash[0] = 0x0;
} else {
workingHash[0] = hex[i];
}
birthdayNext(workingHash,count);
count = 0;
}
free(workingHash);
}
/*
* Compares the generated hash with the hashes we're looking for
* Used when looking for possible collisions
*/
void compare (uint8_t *newHash, uint8_t *msg, int cnt) {
int size = cnt;
bool ok = true;
for ( int i = 0; i < size; i++ ) {
if ( newHash[i] == currentHash1[i] ) {
// Looks good!
} else {
ok = false;
break;
}
}
if ( ok == true ) {
printf("found collision\n");
print_s(msg, cnt);
printf("for hash\n");
print(newHash);
found = true;
}
ok = true;
for ( int i = 0; i < size; i++ ) {
if ( newHash[i] == currentHash2[i] ) {
// Looks good!
} else {
ok = false;
break;
}
}
if ( ok == true ) {
printf("found collision\n");
print_s(msg, cnt);
printf("for hash\n");
print(newHash);
found = true;
}
ok = true;
for ( int i = 0; i < size; i++ ) {
if ( newHash[i] == currentHash3[i] ) {
// Looks good!
} else {
ok = false;
break;
}
}
if ( ok == true ) {
printf("found collision\n");
print_s(msg, cnt);
printf("for hash\n");
print(newHash);
found = true;
}
ok = true;
for ( int i = 0; i < size; i++ ) {
if ( newHash[i] == currentHash4[i] ) {
// Looks good!
} else {
ok = false;
break;
}
}
if ( ok == true ) {
printf("found collision\n");
print_s(msg, cnt);
printf("for hash\n");
print(newHash);
found = true;
}
}
/*
* This method is used for printing out the final hash value of a message
* Useful when debug = false
*/
void hashAndPrint (uint8_t *msg, int size) {
uint8_t *msg_r = NULL;;
msg_r = malloc(size * sizeof(uint8_t));
if(!msg_r){
exit(1);
}
memcpy (msg_r,msg,size);
uint8_t *hsh = hash(msg_r, size);
print(hsh);
// Free memory on the heap
free(hsh);
free(msg_r);
}
/*
* Hash will first break the message into blocks of 4 bit, and append any nessisary padding
* It will call the block method when hashing each 4 bit block
* Finally, it will then combine the blocks to produce the final hash.
*/
uint8_t* hash (uint8_t* msg, int size) {
// if (debug == true) { clock_t start_h=clock(); }
//print_s(msg, size);
steps = steps + 1;
uint8_t *msg_r = NULL;;
int toAppend = 0;
// Check to see if message is correct size
if ( size >= 1 ) {
if (debug == true) { printf("Message size = %d\n", size); }
if ( size%5 != 0 ) {
if (debug == true) { printf("!!! ERROR !!! size of Message is not in 20 bit blocks\n"); }
while (size%5 != 0) {
size += 1;
toAppend += 1;
}
} else {
if (debug == true) { printf("Size of Message is in 20 bit blocks: OK\n"); }
}
} else {
if (debug == true) { printf("!!! FAIL !!! size of message is Zero\n"); }
}
if ( toAppend > 0 ) {
// Allocate storage for the new size
msg_r = malloc(size * sizeof(uint8_t));
if(!msg_r){
exit(1);
}
// Copy contents
memcpy (msg_r,msg,size-toAppend);
while (!(toAppend == 0)) {
// Append 0x0 to elements falset in array
msg_r[size-toAppend] = 0x0;
toAppend -= 1;
if (debug == true) { printf("!!! Attempting to fix !!! appended 0x0 for padding\n"); }
}
} else {
// Just copy contents
msg_r = malloc(size * sizeof(uint8_t));
if(!msg_r){
exit(1);
}
memcpy (msg_r,msg,size);
}
int id = 0;
uint8_t *nextBlock = NULL;
uint8_t blck[5];
for ( int i = 0; i < size; i+=5) {
for ( int u = 0; u < 5; u++) {
blck[u] = msg_r[i+u];
}
id += 1;
if ( i == 0 ) {
if (debug == true) { printf("Creating block with ID: %d\n", id); }
uint8_t Hi[5] = {0x2,0x5,0x9,0xB,0xE};
nextBlock = block(id,blck,Hi);
} else {
if (debug == true) { printf("Creating block with ID: %d\n", id); }
uint8_t Hi[5] = {nextBlock[0],nextBlock[1],nextBlock[2],nextBlock[3],nextBlock[4]};
nextBlock = block(id,blck,Hi);
}
}
// Print results
if (debug == true) { printf("Final Hash value \n"); }
if (debug == true) { printf("A: %#x\n", nextBlock[0]); }
if (debug == true) { printf("B: %#x\n", nextBlock[1]); }
if (debug == true) { printf("C: %#x\n", nextBlock[2]); }
if (debug == true) { printf("D: %#x\n", nextBlock[3]); }
if (debug == true) { printf("E: %#x\n", nextBlock[4]); }
// Free memory on the Heap
free(msg_r);
// Finish
// if (debug == true) { clock_t end_h = clock(); }
// if (debug == true) { double time_h = (double)(end_h-start_h)/CLOCKS_PER_SEC; }
// if (debug == true) { printf("hash speed: %f \n", time_h); }
return nextBlock;
}
uint8_t *block (int blockID, uint8_t *xi, uint8_t *Hi) {
if (debug == true) { printf("process block %d\n", blockID); }
if (debug == true) { printf("ABCDE = %#x %#x %#x %#x %#x \n", Hi[0], Hi[1], Hi[2], Hi[3], Hi[4] ); }
// left shift with carry for 4 bit
// (foo << 1 | foo >> 3)
// & 0xF to mask the leading bit
for (int i=5; i<20;i++){
uint8_t num = xi[i-1]^xi[i-4]^xi[i-5];
num = (num << 1 | num >> 3) & 0xF;
if (debug == true) { printf("%#x\n", num); }
xi[i] = num;
}
// Copy the ABCDE array for later use
uint8_t H15[5] = {Hi[0],Hi[1],Hi[2],Hi[3],Hi[4]};
if (debug == true) { printf("begin round 1\n"); }
for (int i=0; i<10;i++){
if (debug == true) { printf("step %d\n", i); }
// components
uint8_t A = Hi[0];
A = (A << 1 | A >> 3) & 0xF;
uint8_t F = (Hi[1]&Hi[2])|(~Hi[1]&Hi[3]) & 0xF;
uint8_t Xi = xi[i];
uint8_t E = Hi[4];
// product in modulo arithmatic 2^4
uint8_t t = (A + F + Xi + E)%0x10;
// temp variables
uint8_t tempA = Hi[0];
uint8_t tempB = Hi[1];
uint8_t tempC = Hi[2];
uint8_t tempD = Hi[3];
// remap
// shift t i times
for (int j = 1; j <= i; j++){
t = (t << 1 | t >> 3) & 0xF;
}
// shift b 3 times
for (int j = 1; j <= 3; j++){
tempB = (tempB << 1 | tempB >> 3) & 0xF;
}
Hi[0] = t; if (debug == true) { printf("%#x\n", Hi[0]); }
Hi[1] = tempA; if (debug == true) { printf("%#x\n", Hi[1]); }
Hi[2] = tempB; if (debug == true) { printf("%#x\n", Hi[2]); }
Hi[3] = tempC; if (debug == true) { printf("%#x\n", Hi[3]); }
Hi[4] = tempD; if (debug == true) { printf("%#x\n", Hi[4]); }
}
if (debug == true) { printf("begin round 2\n"); }
for (int i=10; i<20;i++){
if (debug == true) { printf("step %d\n", i); }
// components
uint8_t A = Hi[0];
// shift A i times
for (int j = 1; j <= 3; j++){
A = (A << 1 | A >> 3) & 0xF;
}
uint8_t G = (Hi[1]&Hi[3])|(Hi[2]&(~Hi[3])) & 0xF;
uint8_t Xi = xi[i];
uint8_t E = Hi[4];
// product in modulo arithmatic 2^4
uint8_t t = (A + G + Xi + E)%0x10;
// temp variables
uint8_t tempA = Hi[0];
uint8_t tempB = Hi[1];
uint8_t tempC = Hi[2];
uint8_t tempD = Hi[3];
// remap
// shift t i times
for (int j = 1; j <= i; j++){
t = (t << 1 | t >> 3) & 0xF;
}
// shift b 3 times
for (int j = 1; j <= 1; j++){
tempB = (tempB << 1 | tempB >> 3) & 0xF;
}
Hi[0] = t; if (debug == true) { printf("%#x\n", Hi[0]); }
Hi[1] = tempA; if (debug == true) { printf("%#x\n", Hi[1]); }
Hi[2] = tempB; if (debug == true) { printf("%#x\n", Hi[2]); }
Hi[3] = tempC; if (debug == true) { printf("%#x\n", Hi[3]); }
Hi[4] = tempD; if (debug == true) { printf("%#x\n", Hi[4]); }
}
if (debug == true) { printf("finalisation step\n"); }
uint8_t a = (Hi[0]+H15[0])%0x10;
uint8_t b = (Hi[1]+H15[1])%0x10;
uint8_t c = (Hi[2]+H15[2])%0x10;
uint8_t d = (Hi[3]+H15[3])%0x10;
uint8_t e = (Hi[4]+H15[4])%0x10;
int retElements = 5*sizeof(uint8_t);
uint8_t *ret = malloc(retElements);
if(!ret){
exit(1);
}
ret[0] = a; if (debug == true) { printf("A: %#x\n", ret[0]); }
ret[1] = b; if (debug == true) { printf("B: %#x\n", ret[1]); }
ret[2] = c; if (debug == true) { printf("C: %#x\n", ret[2]); }
ret[3] = d; if (debug == true) { printf("D: %#x\n", ret[3]); }
ret[4] = e; if (debug == true) { printf("E: %#x\n", ret[4]); }
return ret;
}
int main(int argc,char **argv) {
hex = malloc(16 * sizeof(uint8_t));
if(!hex){
exit(1);
}
hex[0] = 0x0; hex[1] = 0x1; hex[2] = 0x2; hex[3] = 0x3; hex[4] = 0x4; hex[5] = 0x5;
hex[6] = 0x6; hex[7] = 0x7; hex[8] = 0x8; hex[9] = 0x9; hex[10] = 0xA; hex[11] = 0xB;
hex[12] = 0xC; hex[13] = 0xD; hex[14] = 0xE; hex[15] = 0xF;
int number = -1;
begin:
printf("***** FINAL CRYPTOGRAPHY ASSIGNMENT ***** \n");
printf(" \n");
printf(" Student ID: 1067037\n");
printf(" Key: 1 for Yes \n");
printf(" Key: 0 for No \n");
printf(" \n");
printf("***************************************** \n");
printf(" \n");
printf(" \n");
printf(" \n");
printf("Do you want verbose debug messages, or standard print out (1/0)? \n");
scanf("%d", &number);
if ( number == 1 ) {
debug = true;
printf("Verbose debug messages set to TRUE\n");
} else {
debug = false;
printf("Verbose debug messages set to FALSE\n");
}
printf("For Exercise 21(a), key 1 \n");
printf("For Exercise 21(b), key 2 \n");
printf("For Exercise 21(c), key 3 \n");
scanf("%d", &number);
if ( number == 1 ) {
printf("For Debug messages, key 1 \n");
printf("For Exercise messages, key 2 \n");
scanf("%d", &number);
if ( number == 1 ) {
debug_messages();
number = -1;
} else {
exercise_messages();
number = -1;
}
}
if ( number == 2 ) {
bruteforce_messages();
number = -1;
}
if ( number == 3 ) {
birthday_messages();
number = -1;
}
printf("Would you like to select another option? (1/0)\n");
scanf("%d", &number);
if ( number == 1 ) {
number = -1;
goto begin;
}
free(hex);
return 0;
}
Refactorings
No refactoring yet !
Ants
December 12, 2009, December 12, 2009 06:59, permalink
You have a much bigger problem than a memory leak. You are corrupting your stack in lines 551-556 where you writing to Xi. Your for loop reads and writes to offsets 0 thru 19, but blck declared on line 509 which is passed in as Xi is only 5 bytes long.
Your memory leak is due to the loop in lines 511-526. nextBlock isn't free()'d before a new value is assigned to it.
The comment on line 600 seems to conflict with the code on line 601. It says shift A i times, but it's hardcoded to shift A 3 times. Similarly on line 619, it says shift B 3 times, but it only will shift once. Being lazy and not wanting to lookup the SHA algorithm, should I believe the comments or the code?
On lines 157-160 and 196-199, you are passing in a size of 8, but the bmsgX's are 10 bytes long. Is this intentional?
Dear All, I'm a new C programmer (as you will see from my code >.< ) I have written a functional SHA 5 bit hashing program. It needs simplification and refactoring. There's a memory leak, please help!!! Thankyou!! Any advice on how I can improve my C code would really be appreciated.
Thankyou!!!!