Advertisement
Guest User

threedoors md5

a guest
Jun 15th, 2011
140
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 5.42 KB | None | 0 0
  1. //
  2. //  main.c
  3. //  threeDoorsMD5
  4. //
  5.  
  6. #include <stdio.h>
  7. #include <stdlib.h>
  8. #include <string.h>
  9. #include <time.h>
  10.  
  11. #include <openssl/md5.h>
  12.  
  13. //hashing
  14. unsigned char* hash(int number);
  15. void printHash(unsigned char* hash);
  16. int sameHash(unsigned char* hash1, unsigned char *hash2);
  17. unsigned char* copyHash(unsigned char* hash);
  18. //random numbers
  19. int randNum(void);
  20. int randNumNotTested(int * tested);
  21. int doorPick(int notThis);
  22. int allNumbersHaveBeenSkippedOrTested(int* tested);
  23. //vars
  24. unsigned char result[MD5_DIGEST_LENGTH];
  25.  
  26. //what our tests return
  27. typedef struct testRet{
  28.     int pickNum;
  29.     int count;
  30. } testRet;
  31.  
  32. //test function
  33. testRet* findHashRandomlyWithDoors();
  34. int findHashRandomly();
  35.  
  36. #define MAX_HASHES 100000
  37. #define NUM_TESTED 1
  38. #define NUM_SKIP 2
  39.  
  40. int main (int argc, const char * argv[])
  41. {
  42.     srand((int)time(NULL) );
  43.    
  44.     int pickNum[3];
  45.     pickNum[1] = 0;
  46.     pickNum[2] = 0;
  47.    
  48.     int totalCount = 0;
  49.    
  50.     float tests = 99.0f;
  51.    
  52.    
  53.     testRet *result;
  54.    
  55.     for(int i =0; i < tests; i++){
  56.        result = findHashRandomlyWithDoors();
  57.         totalCount += result->count;
  58.         int pick = result->pickNum;
  59.         pickNum[pick]++;
  60.     }
  61.    
  62.     printf("results for hashes randomly found with doors\n");
  63.     printf("found behind door 1: %d\n", pickNum[1]);
  64.     printf("found behind door 2: %d\n", pickNum[2]);
  65.     printf("average number of tries: %5.2f\n\n", totalCount/tests);
  66.    
  67.    
  68.     totalCount = 0;
  69.     for(int i =0; i < tests; i++){
  70.         int iResult = findHashRandomly();
  71.         totalCount += iResult;
  72.     }
  73.    
  74.     printf("results for hashes randomly\n");
  75.     printf("average number of tries: %5.2f\n", totalCount/tests);
  76.    
  77.    
  78.     return 0;
  79. }
  80.  
  81.  
  82. #pragma mark - Testing
  83. int findHashRandomly(){
  84.     int hashNum = randNum();
  85.     hash(hashNum);
  86.    
  87.     unsigned char* testFor = copyHash(result);
  88.    
  89.     int tested[MAX_HASHES];
  90.     int i;
  91.    
  92.     for(i = 0; i < MAX_HASHES; i++)
  93.         tested[i] = 0;
  94.    
  95.     int count = 0;    
  96.     while(1){
  97.         int number = randNumNotTested(tested);
  98.        
  99.         tested[number] = 1;
  100.         hash(number);
  101.         count++;
  102.         if(sameHash(testFor, result))
  103.             break;
  104.        
  105.     }
  106.    
  107.     free(testFor);
  108.     return count;
  109. }
  110. //returns number of attempts
  111. //and if it was found on first pick or 2nd pick
  112. testRet* findHashRandomlyWithDoors(){
  113.     int hashNum = randNum();
  114.    
  115.     hash(hashNum);
  116.     unsigned char* testFor = copyHash(result);
  117.  
  118.     int tested[MAX_HASHES];
  119.     int i;
  120.    
  121.     for(i = 0; i < MAX_HASHES; i++)
  122.         tested[i] = 0;
  123.    
  124.     int count = 0;
  125.     int doors[3];
  126.     int pickNum = 0;
  127.    
  128.     while(1){
  129.         doors[0] = randNumNotTested(tested);
  130.         doors[1] = doors[0];
  131.         while(doors[1] == doors[0])
  132.             doors[1] = randNumNotTested(tested);
  133.        
  134.         doors[2] = doors[1];
  135.         while(doors[2] == doors[1] || doors[2] == doors[0])
  136.             doors[2] = randNumNotTested(tested);
  137.        
  138.         int pick = doorPick(-1); // let's pick a door
  139.         int notOurPick = doorPick(pick); //now let's reveal one that isn't our pick
  140.        
  141.         int number = doors[notOurPick];
  142.         tested[number] = 1;
  143.        
  144.         hash(number);
  145.         count++;
  146.         if(sameHash(testFor, result)){
  147.             pickNum = 1;
  148.             break;
  149.         }
  150.        
  151.         //now let's change our pick
  152.         int originalPick = pick;
  153.         tested[originalPick] = NUM_SKIP;
  154.        
  155.         while(pick == originalPick || pick == notOurPick)
  156.             pick = doorPick(originalPick);
  157.        
  158.         number = doors[pick];
  159.         tested[number] = 1;
  160.        
  161.         hash(number);
  162.         count++;
  163.         if(sameHash(testFor, result)){
  164.             pickNum = 2;
  165.             break;
  166.         }
  167.     }
  168.     free(testFor);
  169.     testRet * ret = malloc(sizeof(testRet));
  170.     ret->count = count;
  171.     ret->pickNum = pickNum;
  172.     return ret;
  173. }
  174.  
  175. #pragma mark - Hashing
  176.  
  177. unsigned char* hash(int number){
  178.     MD5((unsigned char*)&number, sizeof(number), result);
  179.     return result;
  180. }
  181. void printHash(unsigned char* hash){
  182.     int i;
  183.     for(i=0; i <MD5_DIGEST_LENGTH; i++) {
  184.         printf("%02x",hash[i]);
  185.     }
  186.     printf("\n");
  187. }
  188. unsigned char* copyHash(unsigned char* hash){
  189.     char* copy = malloc(sizeof(MD5_DIGEST_LENGTH));
  190.     memcpy(copy, hash, MD5_DIGEST_LENGTH);
  191.     return (unsigned char*)copy;
  192. }
  193. int sameHash(unsigned char* hash1, unsigned char *hash2){
  194.     if(memcmp(hash1, hash2, MD5_DIGEST_LENGTH) == 0)
  195.         return 1;
  196.     else
  197.         return 0;;
  198. }
  199. #pragma mark - Random Numbers
  200. int randNum(void){
  201.     int random = rand() % MAX_HASHES;
  202.     return random;
  203.    
  204. }
  205. int randNumNotTested(int * tested){
  206.     int number = randNum();
  207.    
  208.     if(!allNumbersHaveBeenSkippedOrTested(tested)){
  209.         while(tested[number] == NUM_TESTED || tested[number] == NUM_SKIP )
  210.             number = randNum();        
  211.        
  212.     }else{    
  213.         while(tested[number] == NUM_TESTED)
  214.             number = randNum();
  215.     }
  216.  
  217.     return number;
  218. }
  219. int doorPick(int notThis){
  220.     int pick = notThis;
  221.    
  222.     while(pick == notThis)
  223.         pick = rand() % 3;
  224.    
  225.     return pick;
  226. }
  227.        
  228. int allNumbersHaveBeenSkippedOrTested(int* tested){
  229.    int i;
  230.    
  231.    for(i=0; i < MAX_HASHES; i++){
  232.        if(tested[i] == 0)
  233.            return 0;
  234.        
  235.    }
  236.    
  237.    return 1;
  238.    
  239. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement