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

0

2 Answers 2

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

Sign up to request clarification or add additional context in comments.

6 Comments

I try to compile your code but gives an error. I update my post with the image of it so you can see it.
I solve the compile error change (int) arg to (long) arg, as is showed here :stackoverflow.com/questions/1640423/…
@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.
I did that mate, it works fine, the output was : partition 2 partition 1, that's right, right ? :)
I don't have to use some block ? like semaphore here @wallyk ?
|
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.

1 Comment

I'm using Fedora, I will take a look in your suggestion

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.