Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //
- // main.c
- // threeDoorsMD5
- //
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- #include <time.h>
- #include <openssl/md5.h>
- //hashing
- unsigned char* hash(int number);
- void printHash(unsigned char* hash);
- int sameHash(unsigned char* hash1, unsigned char *hash2);
- unsigned char* copyHash(unsigned char* hash);
- //random numbers
- int randNum(void);
- int randNumNotTested(int * tested);
- int doorPick(int notThis);
- int allNumbersHaveBeenSkippedOrTested(int* tested);
- //vars
- unsigned char result[MD5_DIGEST_LENGTH];
- //what our tests return
- typedef struct testRet{
- int pickNum;
- int count;
- } testRet;
- //test function
- testRet* findHashRandomlyWithDoors();
- int findHashRandomly();
- #define MAX_HASHES 100000
- #define NUM_TESTED 1
- #define NUM_SKIP 2
- int main (int argc, const char * argv[])
- {
- srand((int)time(NULL) );
- int pickNum[3];
- pickNum[1] = 0;
- pickNum[2] = 0;
- int totalCount = 0;
- float tests = 99.0f;
- testRet *result;
- for(int i =0; i < tests; i++){
- result = findHashRandomlyWithDoors();
- totalCount += result->count;
- int pick = result->pickNum;
- pickNum[pick]++;
- }
- printf("results for hashes randomly found with doors\n");
- printf("found behind door 1: %d\n", pickNum[1]);
- printf("found behind door 2: %d\n", pickNum[2]);
- printf("average number of tries: %5.2f\n\n", totalCount/tests);
- totalCount = 0;
- for(int i =0; i < tests; i++){
- int iResult = findHashRandomly();
- totalCount += iResult;
- }
- printf("results for hashes randomly\n");
- printf("average number of tries: %5.2f\n", totalCount/tests);
- return 0;
- }
- #pragma mark - Testing
- int findHashRandomly(){
- int hashNum = randNum();
- hash(hashNum);
- unsigned char* testFor = copyHash(result);
- int tested[MAX_HASHES];
- int i;
- for(i = 0; i < MAX_HASHES; i++)
- tested[i] = 0;
- int count = 0;
- while(1){
- int number = randNumNotTested(tested);
- tested[number] = 1;
- hash(number);
- count++;
- if(sameHash(testFor, result))
- break;
- }
- free(testFor);
- return count;
- }
- //returns number of attempts
- //and if it was found on first pick or 2nd pick
- testRet* findHashRandomlyWithDoors(){
- int hashNum = randNum();
- hash(hashNum);
- unsigned char* testFor = copyHash(result);
- int tested[MAX_HASHES];
- int i;
- for(i = 0; i < MAX_HASHES; i++)
- tested[i] = 0;
- int count = 0;
- int doors[3];
- int pickNum = 0;
- while(1){
- doors[0] = randNumNotTested(tested);
- doors[1] = doors[0];
- while(doors[1] == doors[0])
- doors[1] = randNumNotTested(tested);
- doors[2] = doors[1];
- while(doors[2] == doors[1] || doors[2] == doors[0])
- doors[2] = randNumNotTested(tested);
- int pick = doorPick(-1); // let's pick a door
- int notOurPick = doorPick(pick); //now let's reveal one that isn't our pick
- int number = doors[notOurPick];
- tested[number] = 1;
- hash(number);
- count++;
- if(sameHash(testFor, result)){
- pickNum = 1;
- break;
- }
- //now let's change our pick
- int originalPick = pick;
- tested[originalPick] = NUM_SKIP;
- while(pick == originalPick || pick == notOurPick)
- pick = doorPick(originalPick);
- number = doors[pick];
- tested[number] = 1;
- hash(number);
- count++;
- if(sameHash(testFor, result)){
- pickNum = 2;
- break;
- }
- }
- free(testFor);
- testRet * ret = malloc(sizeof(testRet));
- ret->count = count;
- ret->pickNum = pickNum;
- return ret;
- }
- #pragma mark - Hashing
- unsigned char* hash(int number){
- MD5((unsigned char*)&number, sizeof(number), result);
- return result;
- }
- void printHash(unsigned char* hash){
- int i;
- for(i=0; i <MD5_DIGEST_LENGTH; i++) {
- printf("%02x",hash[i]);
- }
- printf("\n");
- }
- unsigned char* copyHash(unsigned char* hash){
- char* copy = malloc(sizeof(MD5_DIGEST_LENGTH));
- memcpy(copy, hash, MD5_DIGEST_LENGTH);
- return (unsigned char*)copy;
- }
- int sameHash(unsigned char* hash1, unsigned char *hash2){
- if(memcmp(hash1, hash2, MD5_DIGEST_LENGTH) == 0)
- return 1;
- else
- return 0;;
- }
- #pragma mark - Random Numbers
- int randNum(void){
- int random = rand() % MAX_HASHES;
- return random;
- }
- int randNumNotTested(int * tested){
- int number = randNum();
- if(!allNumbersHaveBeenSkippedOrTested(tested)){
- while(tested[number] == NUM_TESTED || tested[number] == NUM_SKIP )
- number = randNum();
- }else{
- while(tested[number] == NUM_TESTED)
- number = randNum();
- }
- return number;
- }
- int doorPick(int notThis){
- int pick = notThis;
- while(pick == notThis)
- pick = rand() % 3;
- return pick;
- }
- int allNumbersHaveBeenSkippedOrTested(int* tested){
- int i;
- for(i=0; i < MAX_HASHES; i++){
- if(tested[i] == 0)
- return 0;
- }
- return 1;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement