fitsumayalew

Collision Free Property

May 12th, 2021
834
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <stdio.h>
  2. #include <string.h>
  3. #include <openssl/evp.h>
  4.  
  5. // Function to generate random strings
  6. static char *generateRandomString(char *str, size_t size);
  7.  
  8. const int RANDOM_STRING_LENGTH = 21;    // Random String Length
  9. const int FIRST_BYTES = 7;              // The First 6 bytes(24) bits
  10.  
  11.  
  12. int main(int argc, char *argv[]) {
  13.     srand(time(NULL));      // Seed the random generator with current time
  14.  
  15.     // Variable Initializations
  16.     EVP_MD_CTX *mdctx;
  17.     const EVP_MD *md;
  18.     unsigned char md_value[EVP_MAX_MD_SIZE];
  19.     unsigned int md_len, i;
  20.     int counter=0;
  21.     char randomString[RANDOM_STRING_LENGTH];
  22.     char hashedString[FIRST_BYTES];
  23.     char randomString2[RANDOM_STRING_LENGTH];
  24.     char hashedString2[FIRST_BYTES];
  25.  
  26.  
  27.     // Provide error if no argument provided
  28.     if (argv[1] == NULL) {
  29.         printf("Need to provide a hash id as argument!\n");
  30.         exit(1);
  31.     }
  32.  
  33.     // Try to get the hash provided if it doesn't exist provide an error
  34.     md = EVP_get_digestbyname(argv[1]);
  35.     if (md == NULL) {
  36.         printf("Unknown hash id provided %s\n", argv[1]);
  37.         exit(1);
  38.     }
  39.  
  40.  
  41.     // Main loop
  42.     for (;;) {
  43.  
  44.         // Generate a random strings
  45.         generateRandomString(randomString, RANDOM_STRING_LENGTH);
  46.         generateRandomString(randomString2, RANDOM_STRING_LENGTH);
  47.  
  48.  
  49.         // If random strings match somehow skip check
  50.         if (strcmp(randomString, randomString2) == 0) {
  51.             continue;
  52.         }
  53.  
  54.         // Hash both the random strings generated
  55.         mdctx = EVP_MD_CTX_new();
  56.         EVP_DigestInit_ex(mdctx, md, NULL);
  57.         EVP_DigestUpdate(mdctx, randomString, strlen(randomString));
  58.         EVP_DigestFinal_ex(mdctx, md_value, &md_len);
  59.         EVP_MD_CTX_free(mdctx);
  60.  
  61.         mdctx = EVP_MD_CTX_new();
  62.         EVP_DigestInit_ex(mdctx, md, NULL);
  63.         EVP_DigestUpdate(mdctx, randomString2, strlen(randomString2));
  64.         EVP_DigestFinal_ex(mdctx, md_value, &md_len);
  65.         EVP_MD_CTX_free(mdctx);
  66.  
  67.  
  68.         // Convert the integer hashed array to a hex string for the first 24 bits(6 bytes)
  69.         for (i = 0; i < 3; i++)
  70.             sprintf(&hashedString[i * 2], "%02x", (unsigned int) md_value[i]);
  71.  
  72.  
  73.         for (i = 0; i < 3; i++)
  74.             sprintf(&hashedString2[i * 2], "%02x", (unsigned int) md_value[i]);
  75.  
  76.         counter++; // Increment counter with every try
  77.  
  78.         // check if it is similar to the HASH VALUE and if so display no of tries and exit
  79.         if (strcmp(hashedString,hashedString2) == 0) {
  80.             printf("Found a collision after %d tries!\n",counter);
  81.             exit(0);
  82.         }
  83.     }
  84. }
  85.  
  86.  
  87.  
  88. static char *generateRandomString(char *str, size_t size) {
  89.     const char charset[] = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789,.-#'?!";
  90.     if (size) {
  91.         --size;
  92.         for (size_t n = 0; n < size; n++) {
  93.             int key = rand() % (int) (sizeof charset - 1);
  94.             str[n] = charset[key];
  95.         }
  96.         str[size] = '\0';
  97.     }
  98.     return str;
  99. }
  100.  
RAW Paste Data