2011-11-03 4 views
2

, 나는 단지 기본을 알고 나는이 같은 간단한 해쉬 암호를 발견 프로그램을 만들어야합니다C에서 스레드를 사용하여 간단한 프로그램을 만드는 방법은 무엇입니까? 나는 C 개발에 새로운 해요

#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)); 
} 

내가 이것 만 사용하는 것과 같은 일을해야합니다 C의 스레드. 어떻게하면됩니까?

man pthread_create 

가주의 깊게 읽고, 스레드를 사용하는 방법에 대한 매우 설명 예를 제공주의 사항 : 리눅스 쉘에

편집 error

답변

3

스레드를 사용하여 암호를 해시하는 것은 특히 직관적이거나 분명히 유용한 방법이 아니므로 이유가 분명하지 않습니다. 누군가을 원할 것입니다.

아마도 해시 계산은 어떤 방법으로 분할된다 : 아마도 하나의 스레드는 M을 통해 A로 시작 암호를 처리하고 다른 하나는 Z을 통해 N, 또는 파티셔닝 않습니다. 하나의 아이디어는 실행할 파티션을 결정하는 매개 변수를 사용하여 같은 함수를 여러 번 실행하는 것입니다. 다음은 프레임 워크를 보여주는 간단하고 기능적인 프로그램입니다.

#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;  
} 

내가 페도라를 사용하고 명령 g++ -pthread filename.c++ -o filename

+0

에서 살펴볼 것이다. 나는 당신이 그것을 볼 수 있도록 그것의 이미지로 내 게시물을 업데이 트합니다. –

+0

여기에 나와있는 것처럼 (long) arg에 대한 컴파일 오류 변경 (int) arg을 해결합니다. http : //stackoverflow.com/questions/1640423/error-cast-from-void-to-int-loses-precision –

+0

@Valter Henrique : 이상한 오류입니다. 해당 라인에서'int'를'long' (또는'long long')으로 변경하여 호환 가능한 크기를 찾으십시오. – wallyk

0

을 실행합니다. , 참조 번호 섹션의 기능 맨 페이지를 참조하십시오.

당신이 창문에 있다면 당신은 코드의 어느 부분 (들)을 결정해야 그 후의 pthreads-win32에서 here

의 decomentation이 병렬 처리 될 수 있습니다보고 다른 스레드에 해당 코드를 할당 할 수 있습니다.

+0

를 사용, GCC를 사용하여 리눅스를 구축하기 위해, 나는 코드를 컴파일하려고하지만 오류를 제공합니다 귀하의 제안 –

관련 문제