Advertisement
Guest User

Untitled

a guest
Jun 20th, 2019
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 3.91 KB | None | 0 0
  1. #define _GNU_SOURCE
  2. #include <crypt.h>
  3. #include <unistd.h>
  4. #include <stdio.h>
  5. #include <stdlib.h>
  6. #include <pthread.h>
  7. #include <sys/mman.h>
  8. #include <sys/stat.h>
  9. #include <fcntl.h>
  10. #include <string.h>
  11.  
  12. size_t getFileSize(const char* filename)
  13. {
  14.     struct stat st;
  15.     stat(filename, &st);
  16.     return st.st_size;
  17. }
  18.  
  19. typedef struct args
  20. {
  21.     int start;
  22.     int partSize;
  23. };
  24.  
  25. char *map;
  26. int isSolved = 0;
  27. char *password;
  28. char *salt;
  29. struct crypt_data data[1] = {0};
  30. size_t size;
  31. pthread_mutex_t smutex = PTHREAD_MUTEX_INITIALIZER;
  32. int pos = 0;
  33.  
  34. void *findPassword(void *arguments)
  35. {
  36.     // odczytujemy parametry startowe wątku
  37.     int start = ((struct args*)arguments)->start;
  38.     int partSize=((struct args*)arguments)->partSize;
  39.     int i=start;
  40.     while(i<start+partSize)
  41.     {
  42.         // sprawdzamy czy jakiś inny wątek nie znalazł rozwiązania
  43.         pthread_mutex_lock(&smutex);
  44.         if(isSolved == 1)
  45.         {
  46.             pthread_mutex_unlock(&smutex);
  47.             break;
  48.         }
  49.         pthread_mutex_unlock(&smutex);
  50.  
  51.         // weryfikujemy kolejne rozwiązanie w naszym obszarze pamięci
  52.         int j = 0;
  53.         char *tempPassword = (char*)malloc(100);
  54.         while(map[i]!=10)
  55.         {
  56.             tempPassword[j++] = map[i++];
  57.         }
  58.         i++;
  59.         tempPassword[j] = '\0';
  60.    
  61.     pthread_mutex_lock(&smutex);        
  62.     char *test = crypt_r(tempPassword, salt, data);
  63.        
  64.     if(strcmp(test,password)==0)
  65.         {
  66.             printf("\nHaslo to %s\n",tempPassword);
  67.             isSolved = 1;
  68.             free(tempPassword);
  69.             pthread_mutex_unlock(&smutex);
  70.             break;
  71.         }
  72.         pthread_mutex_unlock(&smutex);
  73.         free(tempPassword);
  74.  
  75.         //aktualizujemy progress
  76.         pthread_mutex_lock(&smutex);
  77.         pos+=j+1;
  78.         printf("%.2f%\r",((float)pos/size*100));
  79.         pthread_mutex_unlock(&smutex);
  80.  
  81.     }
  82.  
  83.     return NULL;
  84. }
  85.  
  86.  
  87. int main(int argc, char **argv)
  88. {
  89.     // liczba wątków jest odczytywana z parametru i porównywana
  90.     // z liczbą procesorów w systemie, wybierana mniejsza wartość
  91.     int numberOfThreads;
  92.     int  processors = sysconf(_SC_NPROCESSORS_ONLN);
  93.     if(atoi(argv[3]) < processors)
  94.     {
  95.         numberOfThreads = atoi(argv[3]);
  96.     }
  97.     else
  98.     {
  99.         numberOfThreads = processors;
  100.     }
  101.  
  102.     //tworzymy obiekty wątków w liczbie ustalonej powyżej
  103.     pthread_t *th = (pthread_t*)malloc(numberOfThreads*sizeof(pthread_t));
  104.     struct args* partArg = (struct args*)malloc(numberOfThreads*sizeof(struct args));
  105.  
  106.     // TODO
  107.     int used = 0;
  108.     password = argv[1];
  109.     salt = (char*)malloc(50);
  110.     salt[0] = '$';
  111.     salt[1] = argv[1][1];
  112.     salt[2] = '$';
  113.     int i = 3;
  114.  
  115.  
  116.     while(argv[1][i]!='$')
  117.     {
  118.             salt[i]=argv[1][i++];
  119.     }
  120.     salt[i]='\0';
  121.  
  122.     //otwieramy plik z hasłami
  123.     size = getFileSize(argv[2]);
  124.     int fd = open(argv[2], O_RDONLY);
  125.     if(fd<0)
  126.     {
  127.             printf("Blad\n");
  128.             return -1;
  129.     }
  130.     map = mmap(0, size, PROT_READ, MAP_PRIVATE, fd, 0);
  131.  
  132.     // dzielimy plik na części dla poszczególnych wątków
  133.     for(i=0; i<numberOfThreads; i++)
  134.     {
  135.         partArg[i].start=used;
  136.  
  137.         if(i==numberOfThreads-1)
  138.         {
  139.            partArg[i].partSize = size-used;
  140.            break;
  141.         }
  142.         partArg[i].partSize = size/numberOfThreads;
  143.  
  144.  
  145.         while(map[used+partArg[i].partSize]!=10 && map[used+partArg[i].partSize]!='\0')
  146.         {
  147.             partArg[i].partSize++;
  148.         }
  149.         used += partArg[i].partSize;
  150.     }
  151.  
  152.     // odpalamy wątki, każdy wykonuje funkcję findPassword
  153.     for(i=0; i<numberOfThreads; i++)
  154.     {
  155.         pthread_create(&th[i], NULL, findPassword, (void*)&partArg[i]);
  156.     }
  157.  
  158.     // czekamy aż wątki skończą pracę
  159.     for(i=0; i<numberOfThreads; i++) pthread_join(th[i], NULL);
  160.  
  161.     // zwalniamy plik
  162.     munmap(map, size);
  163.  
  164.     if(!isSolved) printf("Nie udalo sie odnalezc hasla\n");
  165.     free(salt);
  166.     return 0;
  167. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement