2

I'm new in C development, I know just the basics and I need to create a program that discover a simple hash password like this one:

#define _GNU_SOURCE
#include <stdio.h>
#include <string.h>
#include <crypt.h>
#include <stdlib.h>

#define SIZE_HASH 256
#define SIZE_PASSWORD 4

/* Get the hash from a passwod and put the result in hash. The array hash shoud have at lest 14 elements. */
void calculate_hash_password(const char *password, char *hash);

void increment_password(char *password);
void test_password(const char *p_hash, const char *password);

int main(int argc, char *argv[]) {
  int i;
  char password[SIZE_PASSWORD + 1];

  if (argc != 2) {
    printf("Use: %s <hash>", argv[0]);
    return 1;
  }

  for (i = 0; i < SIZE_PASSWORD; i++) {
    password[i] = 'a';
  }
  password[SIZE_PASSWORD] = '\0';

  while (1) {
    test_password(argv[1], password);
    increment_password(password);
  }
  return 0;
}

void test_password(const char *p_hash, const char *password) {
  char hash_calculado[SIZE_HASH + 1];

  calculate_hash_password(password, hash_calculado);
  if (!strcmp(p_hash, hash_calculado)) {
    printf("Achou! %s\n", password);
    exit(0);
  }
}

void increment_password(char *password) {
  int i;

  i = SIZE_PASSWORD - 1;
  while (i >= 0) {
    if (password[i] != 'z') {
      password[i]++;
      i = -2;
    } else {
      password[i] = 'a';
      i--;
    }
  }
  if (i == -1) {
    printf("Não achou!\n");
    exit(1);
  }
}


void calculate_hash_password(const char *password, char *hash) {
  struct crypt_data data;
  data.initialized = 0;
  strcpy(hash, crypt_r(password, "aa", &data));
}

I must do the same thing as this one but using threads in C. How can I do that ?

EDIT error

Valter Silva
  • 16,446
  • 52
  • 137
  • 218

2 Answers2

3

Using threads to hash passwords is not a particularly intuitive or obviously useful approach, so it is not clear why anyone would want to do that.

Presumably the calculation for hashing is split up in some way: perhaps one thread processes passwords beginning with A through M and another does N through Z, or some such partitioning. One idea would be to run the same function multiple times with a parameter which determines which partition to execute. Here is a simple, functioning program which demonstrates the framework.

#include <iostream>
#include <pthread.h>    

static void *calc_func (void *arg)
{                               
        int param = (int) arg;
        if (param == 1) 
        {
                // do first partition of calculation
                // ...  
                std::cout << "partition 1" << std::endl;
        }                       
        else            
        {
                // do second partition of calculation
                // ...  
                std::cout << "partition 2" << std::endl;
        }                       
}                               

int main (...)
{                       
        // ...          
        pthread_t threadh[2];

        if (pthread_create (&threadh[0], NULL, calc_func, (void *)1) != 0)
        {               
                std::cerr << "error creating thread 1" << std::endl;
        }                       

        if (pthread_create (&threadh[1], NULL, calc_func, (void *)2) != 0)
        {                               
                std::cerr << "error creating thread 2" << std::endl;
        }               
        // wait for threads to exit
        pthread_join (threadh[0], NULL);
        pthread_join (threadh[1], NULL);
        return 0;       
}

To build it on Linux using gcc, use the command g++ -pthread filename.c++ -o filename

wallyk
  • 56,922
  • 16
  • 83
  • 148
  • I try to compile your code but gives an error. I update my post with the image of it so you can see it. – Valter Silva Nov 04 '11 at 00:23
  • I solve the compile error change (int) arg to (long) arg, as is showed here :http://stackoverflow.com/questions/1640423/error-cast-from-void-to-int-loses-precision – Valter Silva Nov 04 '11 at 00:34
  • @Valter Henrique: That's a strange error. Try changing the `int` in that line to `long` (or maybe `long long`) to find a compatible size. – wallyk Nov 04 '11 at 00:35
  • I did that mate, it works fine, the output was : partition 2 partition 1, that's right, right ? :) – Valter Silva Nov 04 '11 at 01:02
  • I don't have to use some block ? like semaphore here @wallyk ? – Valter Silva Nov 04 '11 at 01:14
  • @ValterHenrique: Not for what I wrote. They are not dependent on each other for anything, so no need for handshaking. (I noticed the timing changed every time I ran it, sometimes showing `partition 1` on one line and `partition 2` on the other, sometimes they were on the same line followed by two newlines, and almost every time they alternated order of 1 and 2.) – wallyk Nov 04 '11 at 04:56
0

On a Linux shell execute:

man pthread_create

Read it carefully, and notice that provides a very descriptive example, on how to use threads. See also the man pages of the functions in the SEE ALSO section.

If you are on windows you can see the decomentation of pthreads-win32 here

After that you have to decide which part(s) of your code can be parallelized and assign that code to different threads.

Manos
  • 2,136
  • 17
  • 28