Thread: Need help with Multithreading & Pointers in C

  1. #1
    Registered User
    Join Date
    Jun 2019
    Posts
    3

    Question Need help with Multithreading & Pointers in C

    Okay so ill try my best at explaining this problem as I struggle with pointers.

    I have an assignment for university in which I have to alter a pre-made password cracking program and make it multi threaded but just cant seem to get my head around the problem i'm having. i will try and post only the necessary information

    So i have 4 pre created encrypted passwords stored within the array 'encrypted_passwords' and i need to use the multi-threading to be able to crack them much faster.

    I have then created a struct that contains that starting value for each thread and the stride for each so that they do not overlap, also i have the pointer to the passwords 'salt_and_encrypted' that is declared at the start.

    I then (in the main) create the threads and also set the values for the arguments for each thread before starting the threads running on the 'crack' method.

    Edit: Errors - CrackMT.c:126:30: warning: assignment from incompatible pointer type [-Wincompatible-pointer-types] t1_args.salt_and_encrypted = encrypted_passwords;
    The errors are related to each of the t1_args, t2_args, t3_args and t4_args where i attempt to set the salt_and_encrypted to encrypted_password.



    Code:
    char * salt_and_encrypted;
    char *encrypted_passwords[] = { 
    "$6$KB$./CyqoGai/YYFHYBAT.tSdTuYEqW/XY2Y53D7xV/rU6QtJBThh1i3zffnptK6pJTUgMiZnV6fwjyyFMckLJuP.",
    "$6$KB$sig9RP1foJmxO9Q/bzDukmLxWluzHxs5xGyNbQVdrFf.mc4jhztG6eV37qRKU/SHyuFvOh2H.bz8a3oEOkDLS.",
    "$6$KB$5f63QAb4.5CLFgeTNRuRBQwLgyAxEefgjvIRqzhJqP9SGGTfCuP0ZuGhuxzivK6Z8Ywod4XaMIjXl8Sxi99iv1",
    "$6$KB$CNwwbZPmnN53QUM0l19W9gkCsgUGWFPZntW9u94Oso4j0zHl6mQsPrNuOvFH4tSJiP4fo/MMq08lTeVXRtgGh1"
    };
    
    
    typedef struct arguments_t {
      int start;
      int stride;
      char *salt_and_encrypted;
    } arguments_t;
    
    void *crack(void *be){
    
    arguments_t *args = (arguments_t *) be; 
    printf("%c",'A');
    
      int w, x, y, z;    // Loop counters (added W for extra loop below)
      char salt[8];    // String used in hashing the password. Need space for \0
      char plain[8];   // The combination of letters currently being checked
      char *enc;       // Pointer to the encrypted password
      int count = 0;   // The number of combinations explored so far
    
      substr(salt, salt_and_encrypted, 0, 7);
        for(w=args->start; w<='Z'; w+=args->stride){
          for(x='A'; x<='Z'; x++){
            for(y='A'; y<='Z'; y++){
              for(z=0; z<=99; z++){
            sprintf(plain, "%c%c%c%02d", w, x, y, z);
            enc = (char *) crypt(plain, salt);
            count++;
            if(strcmp(salt_and_encrypted, enc) == 0)
            {
              //printf("#%-8d%s %s\n", count, plain, enc);
            } 
            else 
            {
              //printf(" %-8d%s %s\n", count, plain, enc);
            }
              }
            }
          }
        }
      printf("%d solutions explored\n", count);
    }
    
    int main(int argc, char *argv[]){
    
    //define 4 threads
    pthread_t t1, t2, t3, t4;
    
    //set argruments for each thread
      arguments_t t1_args;
      t1_args.salt_and_encrypted = encrypted_passwords;
      t1_args.start = 0;
      t1_args.stride = 4;
    
      arguments_t t2_args;
      t2_args.salt_and_encrypted = encrypted_passwords;
      t2_args.start = 1;
      t2_args.stride = 4;
    
      arguments_t t3_args;
      t3_args.salt_and_encrypted = encrypted_passwords;  
      t3_args.start = 2;
      t3_args.stride = 4;
    
      arguments_t t4_args;
      t4_args.salt_and_encrypted = encrypted_passwords;
      t4_args.start = 3;
      t4_args.stride = 4;
    
      void *crack();
    
      //create each thread
      pthread_create(&t1, NULL, crack, &t1_args);
      pthread_create(&t2, NULL, crack, &t2_args);
      pthread_create(&t3, NULL, crack, &t3_args);
      pthread_create(&t4, NULL, crack, &t4_args);
    
      //join the threads to end 
      pthread_join(t1, NULL);
      pthread_join(t2, NULL);
      pthread_join(t3, NULL);
      pthread_join(t4, NULL);
    }
    Any help will be appreciated as the course material that was covered was not useful at all. If i have missed anything from the code that would make it easier for you to help me I will add later if needed.
    Last edited by xbosworth; 06-05-2019 at 05:48 AM. Reason: Forgot to add errors

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    > t1_args.salt_and_encrypted = encrypted_passwords;
    The RHS is an array, so you need to say something like
    t1_args.salt_and_encrypted = encrypted_passwords[0];

    The prototype on line 76 (as posted) is useless.

    > substr(salt, salt_and_encrypted, 0, 7);
    salt_and_encrypted is the uninitialised global, not the parameter you pass with the same name.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Jun 2019
    Posts
    3
    Quote Originally Posted by Salem View Post
    > t1_args.salt_and_encrypted = encrypted_passwords;
    The RHS is an array, so you need to say something like
    t1_args.salt_and_encrypted = encrypted_passwords[0];

    The prototype on line 76 (as posted) is useless.

    > substr(salt, salt_and_encrypted, 0, 7);
    salt_and_encrypted is the uninitialised global, not the parameter you pass with the same name.
    if i was to do as you said and put t1_args.salt_and_encrypted = encrypted_password[0] wouldn't that set salt_and_encrypted to the single value that is in position 0 in the char array?
    What i need to do is get each thread to be looking for all 4 password but only do a specific number of searches per thread.

    I have got the program to run now but I feel that there is still something missing because as there are 3 Letters and 2 Numbers to be calculated that would be (26*26*26*100) equalling 1757600 but when running it there have only been 473200 being done total whereas each thread should be doing around 439400 each if my math is correct.

  4. #4
    Registered User
    Join Date
    Feb 2019
    Posts
    1,078
    Quote Originally Posted by xbosworth View Post
    if i was to do as you said and put t1_args.salt_and_encrypted = encrypted_password[0] wouldn't that set salt_and_encrypted to the single value that is in position 0 in the char array?
    You defined encrypted_passwords as an array of POINTERS, so encrypted_passwords[0] points to the first string, encrypted_passwords[1] to the second, ...

    Quote Originally Posted by xbosworth
    What i need to do is get each thread to be looking for all 4 password but only do a specific number of searches per thread.
    Why?

  5. #5
    Registered User
    Join Date
    Jun 2019
    Posts
    3
    I think i'm confusing myself more than anything regarding the first point but I understand what you mean now.

    on the second point i'm honestly not sure, its just how they made the assignment as we are supposed to be recording the running times of the original program and then alter it to run multi-threaded and write comparisons on them. the original program passes the encrypted_passwords array into the crack method but due to having to run multi-threaded I had to alter it to allow for the passing of the arguments to each thread.

  6. #6
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    > char *encrypted_passwords[]
    TBH, you have 4 of these, and 4 threads.
    The .start and .stride seem to be red herrings.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Multithreading help
    By nickman in forum C Programming
    Replies: 23
    Last Post: 09-19-2013, 01:23 PM
  2. looking for an example (MVC + multithreading)
    By klmdb in forum C# Programming
    Replies: 6
    Last Post: 02-11-2011, 06:12 AM
  3. Multithreading
    By GReaper in forum Windows Programming
    Replies: 4
    Last Post: 11-06-2010, 09:40 PM
  4. Multithreading
    By Lord CyKill in forum C++ Programming
    Replies: 4
    Last Post: 06-30-2003, 11:14 AM
  5. multithreading
    By thedumbmutt in forum A Brief History of Cprogramming.com
    Replies: 1
    Last Post: 11-13-2002, 11:54 AM

Tags for this Thread