使用MPI在C中并行执行MD5哈希蛮力

时间:2019-05-30 09:43:10

标签: c hash mpi md5 brute-force

因此,我对使用MPI相当陌生,现在我有一项家庭作业,可以使用bruteforce查找md5-hash的原始字符串。

我已经为单线程尝试实现了代码(C)。 我的代码:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdint.h>
#include <mpi.h>

#include <openssl/md5.h>
#define MaxPassLength 10

const char *string = "test";
const char *alphabet = "abcdefghijklmnopqrstuvwxyz";

int main(int argc, char* argv[]){
  // generate the Hash of string
  unsigned char testHash[MD5_DIGEST_LENGTH];
  MD5(string, strlen(string), testHash);
  for(int i = 0; i < MD5_DIGEST_LENGTH; i++){
    printf("%02x", testHash[i]);
  }
  printf("\n\n");

  // Variables needed
  char tryPassIndex[MaxPassLength];
  char tryPass[MaxPassLength];
  for (int i = 0; i < MaxPassLength; ++i) {
    tryPassIndex[i] = 0;
    tryPass[i] = alphabet[0];
  }
  unsigned char result[MD5_DIGEST_LENGTH];
  int possibleDigits = strlen(alphabet);
  int passLength = 2;

  int rank, size;

  // MPI Init
  // MPI_Init (&argc, &argv);
  // MPI_Comm_rank (MPI_COMM_WORLD, &rank);
  // MPI_Comm_size (MPI_COMM_WORLD, &size);

  while(1){

    for (int Counter=0; Counter<possibleDigits; ++Counter){
        tryPass[0] = alphabet[Counter];
        MD5(tryPass, passLength, result);
        // check if hashes match:
        if (!memcmp(result, testHash, MD5_DIGEST_LENGTH)) goto LabelHitFound;
    }
    // finished another Run on the first index: Increase further indices.

    for(int i=1; i<=MaxPassLength; ++i){
      if(tryPassIndex[i]<=possibleDigits){
        tryPassIndex[i]++;
        tryPass[i] = alphabet[tryPassIndex[i]];

        break;
      } else {
        tryPassIndex[i]=0;
        tryPass[tryPassIndex[i]] = alphabet[0];
        if(i==passLength){
          // here the length of the assumed pass increases
          printf("Progressed passwords with the length %d\n", passLength);
          passLength++;
        }

      }
    }
  }

  LabelHitFound:
  for(int i = 0; i < MD5_DIGEST_LENGTH; ++i){
    printf("%02x", result[i]);
  }
  MPI_Finalize();
  printf("\n");
  for(int i = 0; i<passLength; ++i)
    printf("%c", tryPass[i]);
  printf("\nCracked Password!\n");
  return EXIT_SUCCESS;
}

我的方法是通过增加长度来测试所有密码。 到目前为止,它的工作效果很好,但我不知道如何在其中使用MPI来“使其并行化”。

感谢您的帮助。

0 个答案:

没有答案