2016-12-10 4 views
0

У меня есть программа, которая генерирует хэш-ключ из файла. А также проверяет, соответствует ли хэш результата ожидаемому хэшу. Но программа работает только один файл. Теперь я пытался создать хэш-ключ из каждого файла в данном каталоге и выполнить сравнение с каждым ожидаемым хешем.связать значение с соответствующим файлом

Для этого у меня есть код ниже, чтобы прочитать каталог. Но у меня нет успеха связать ожидаемый хеш с каждым файлом, который у меня есть в каталоге.

С strcmp Я могу сравнить, и он уже работает, но как я могу связать правильный ожидаемый хеш с соответствующим файлом? Вы знаете, как это сделать?

PCSTR text = "C:\\Users\\Jax\\Desktop\\files\\files2\\txt_1.txt"; 
PCSTR pptx = "C:\\Users\\Jax\\Desktop\\files\\files2\\Test.pptx"; 

DIR   *d; 
d = opendir("C:\\Users\\Jax\\Desktop\\files\\files2\\"); 
struct dirent *dir; 

char name[256][256]; 
int count = 0; 
int index = 0; 

char TextExpected[] = "811676652bf08c0a91a849d66bb2a46c"; 
char PptxExpected[] = "b011367338c3264f1f3f74107060d788"; 

while ((dir = readdir(d)) != NULL) 
    { 
     printf("%s\n", dir->d_name); 


     strcpy(name[count],dir->d_name); 

     count++; 


     if(strcmp(dir->d_name,"test.pptx") == 0){ 
      // how can I do here to associate the hashExpected to the file "test.pptx" 
     } 
     if(strcmp(dir->d_name,"test.txt") == 0){ 
      // how can I do here to associate the hashExpected to the file "test.txt" 
     } 

    } 
    closedir(d); 

    while(count > 0) 
    { 
    ... 

Внутри время (число> 0) Выполнения код для генерации хэш-ключа для каждого файла в каталоге (число> 0).

Это полная программа, она работает только что ассоциирования часть я не имея успеха в получении его на работу:

#include <stdio.h> 
#include <windows.h> 
#include <Wincrypt.h> 
#include <dirent.h> 
#include <stdbool.h> 
#define BUFSIZE 1024 
#define MD5LEN 16 


int main() 
{ 
    DWORD dwStatus = 0; 
    HCRYPTPROV hProv = 0; 
    HCRYPTHASH hHash = 0; 
    HANDLE hFile = NULL; 
    BYTE rgbFile[BUFSIZE]; 
    DWORD cbRead = 0; 
    BYTE rgbHash[MD5LEN]; 
    DWORD cbHash = 0; 
    CHAR rgbDigits[] = "abcdef"; 

    PCSTR text = "C:\\Users\\Jax\\Desktop\\files\\files2\\txt_1.txt"; 
    PCSTR pptx = "C:\\Users\\Jax\\Desktop\\files\\files2\\Test.pptx"; 




    DIR   *d; 
    d = opendir("C:\\Users\\Jax\\Desktop\\files\\files2\\"); 
    struct dirent *dir; 
    struct dirent *test; 
    char name[256][256]; 
    int count = 0; 
    int index = 0; 

    int expected[25]; 
    int countcount; 

    char testtest[256][256]; 

    char TextExpected[] = "811676652bf08c0a91a849d66bb2a46c"; 
    char PptxExpected[] = "b011367338c3264f1f3f74107060d788"; 



    while ((dir = readdir(d)) != NULL) 
    { 
     printf("%s\n", dir->d_name); 


     strcpy(name[count],dir->d_name); 

     count++; 


     if(strcmp(dir->d_name,"test.pptx") == 0){ 
      // how can I do here to associate the hashExpected to the file "test.pptx" 
     } 
     if(strcmp(dir->d_name,"test.txt") == 0){ 
      // how can I do here to associate the hashExpected to the file "test.txt" 
     } 

    } 
    closedir(d); 

    while(count > 0) 
    { 

     bool incorrect = FALSE; 

     char hashResult[MD5LEN * 2 + 1] = ""; 
     hFile = CreateFile(text, GENERIC_READ, FILE_SHARE_READ, 
          NULL, OPEN_EXISTING, FILE_FLAG_SEQUENTIAL_SCAN, NULL); 

     if(INVALID_HANDLE_VALUE == hFile) 
     { 
      incorrect = TRUE; 
      dwStatus = GetLastError(); 
      printf("Error opening file %s\nError: %d\n", text, dwStatus); 
      return (int)dwStatus; 
     } 

     // Get handle to the crypto provider 
     if(!CryptAcquireContext(&hProv, NULL, NULL, PROV_RSA_FULL, CRYPT_VERIFYCONTEXT)) 
     { 
      dwStatus = GetLastError(); 
      printf("CryptAcquireContext failed: %d\n", dwStatus); 
      CloseHandle(hFile); 
      return (int)dwStatus; 
     } 

     if(!CryptCreateHash(hProv, CALG_MD5, 0, 0, &hHash)) 
     { 
      dwStatus = GetLastError(); 
      printf("CryptAcquireContext failed: %d\n", dwStatus); 
      CloseHandle(hFile); 
      CryptReleaseContext(hProv, 0); 
      return (int)dwStatus; 
     } 

     while(ReadFile(hFile, rgbFile, BUFSIZE, &cbRead, NULL)) 
     { 
      if(0 == cbRead) 
       break; 

      if(!CryptHashData(hHash, rgbFile, cbRead, 0)) 
      { 
       dwStatus = GetLastError(); 
       printf("CryptHashData failed: %d\n", dwStatus); 
       CryptReleaseContext(hProv, 0); 
       CryptDestroyHash(hHash); 
       CloseHandle(hFile); 
       return (int)dwStatus; 
      } 
     } 

     cbHash = MD5LEN; 

     if(CryptGetHashParam(hHash, HP_HASHVAL, rgbHash, &cbHash, 0)) 
     { 
      DWORD i; 

      printf("MD5 expected, versus MD5 of file %s is:\n", text); 
      printf("%s\n", TextExpected); 
      for(i = 0; i < cbHash; i++) 
      { 
       printf("%c%c", 
         rgbDigits[rgbHash[i] >> 4], 
         rgbDigits[rgbHash[i] & 0xf]); 
       hashResult[i * 2] = rgbDigits[rgbHash[i] >> 4]; 
       hashResult[i * 2 + 1] = rgbDigits[rgbHash[i] & 0xf]; 
      } 
      printf("\n"); 

      if(_strcmpi(hashResult, TextExpected) == 0) 
       printf("Hash is the same\n"); 
      else 
       printf("Hash is different\n"); 
     } 
     else 
     { 
      dwStatus = GetLastError(); 
      printf("CryptGetHashParam failed: %d\n", dwStatus); 
     } 

     CryptDestroyHash(hHash); 
     CryptReleaseContext(hProv, 0); 
     CloseHandle(hFile); 

     return (int)dwStatus; 
    } 
} 
+0

"* icual *"? Я знал, что откуда-то узнал этот код. (Это написано «равно».) – melpomene

ответ

0
strcpy(name[count],dir->d_name); 

d_name относительное имя файла, например "test.txt", но скорее всего, вы хотите использовать полное имя пути для функций криптографии. Вместо этого используйте полный путь. Пример:

strcpy(name[count],"c:\\dir\\"); 
strcat(name[count],dir->d_name); 

readdir результат «s включает "." и "..", которые в основном бесполезны, вы хотите, чтобы пропустить их:

if(strcmp(dir->d_name,".") == 0 || strcmp(dir->d_name,"..") == 0) 
    continue; 

while(count > 0) помещает код в бесконечном цикле. Вы имеете в виду, чтобы сделать это вместо:

int index = 0; 
while(index < count) 
{ 
    const char* filename = name[index]; 
    index++; 
} 

Обратите внимание, вы хотите использовать name[index] и не name. Вы, вероятно, не хотите выходить из цикла всякий раз, когда возникает ошибка, вместо этого вы хотите продолжить.

Наконец, добавьте функцию, которая будет искать ожидаемое значение хеша для известных файлов. Пример:

#include <stdio.h> 
#include <windows.h> 
#include <Wincrypt.h> 
#include <dirent.h> 
#include <stdbool.h> 
#define BUFSIZE 1024 
#define MD5LEN 16 

const char* get_expected_hash(const char* filename) 
{ 
    PCSTR text = "C:\\Users\\Jax\\Desktop\\files\\files2\\txt_1.txt"; 
    PCSTR pptx = "C:\\Users\\Jax\\Desktop\\files\\files2\\Test.pptx"; 

    const char* TextExpected = "811676652bf08c0a91a849d66bb2a46c"; 
    const char* PptxExpected = "b011367338c3264f1f3f74107060d788"; 

    if (stricmp(filename, text) == 0) 
     return TextExpected; 

    if (stricmp(filename, pptx) == 0) 
     return PptxExpected; 

    return "unknown hash"; 
} 

int main() 
{ 
    DWORD dwStatus = 0; 
    HCRYPTPROV hProv = 0; 
    HCRYPTHASH hHash = 0; 
    HANDLE hFile = NULL; 
    BYTE rgbFile[BUFSIZE]; 
    DWORD cbRead = 0; 
    BYTE rgbHash[MD5LEN]; 
    DWORD cbHash = 0; 
    CHAR rgbDigits[] = "abcdef"; 

    //const char* dirname = "C:\\Users\\Jax\\Desktop\\files\\files2\\"; 
    const char* dirname = "c:\\test\\"; 

    DIR *d; 
    d = opendir(dirname); 
    struct dirent *dir; 
    char files[256][256]; 
    int count = 0; 

    while ((dir = readdir(d)) != NULL) 
    { 
     if(strcmp(dir->d_name,".") == 0 || strcmp(dir->d_name,"..") == 0) 
      continue; 

     strcpy(files[count],dirname); 
     strcat(files[count],dir->d_name); 
     count++; 
    } 
    closedir(d); 

    // Get handle to the crypto provider 
    if(!CryptAcquireContext(&hProv, NULL, NULL, PROV_RSA_FULL, CRYPT_VERIFYCONTEXT)) 
    { 
     dwStatus = GetLastError(); 
     printf("CryptAcquireContext failed: %d\n", (int)dwStatus); 
     CloseHandle(hFile); 
     return (int)dwStatus; 
    } 

    int index = 0; 
    while(index < count) 
    { 
     const char* filename = files[index]; 
     index++; 

     printf("%s\n", filename); 

     char hashResult[MD5LEN * 2 + 1] = ""; 
     hFile = CreateFile(filename, GENERIC_READ, FILE_SHARE_READ, 
          NULL, OPEN_EXISTING, FILE_FLAG_SEQUENTIAL_SCAN, NULL); 

     if(INVALID_HANDLE_VALUE == hFile) 
     { 
      //incorrect = TRUE; 
      dwStatus = GetLastError(); 
      printf("Error opening file %s\nError: %d\n", filename, (int)dwStatus); 
      return (int)dwStatus; 
     } 

     if(!CryptCreateHash(hProv, CALG_MD5, 0, 0, &hHash)) 
     { 
      dwStatus = GetLastError(); 
      printf("CryptAcquireContext failed: %d\n", (int)dwStatus); 
      CloseHandle(hFile); 
      CryptReleaseContext(hProv, 0); 
      continue; 
     } 

     while(ReadFile(hFile, rgbFile, BUFSIZE, &cbRead, NULL)) 
     { 
      if(0 == cbRead) 
       break; 

      if(!CryptHashData(hHash, rgbFile, cbRead, 0)) 
      { 
       dwStatus = GetLastError(); 
       printf("CryptHashData failed: %d\n", (int)dwStatus); 
       CryptReleaseContext(hProv, 0); 
       CryptDestroyHash(hHash); 
       CloseHandle(hFile); 
       continue; 
      } 
     } 

     cbHash = MD5LEN; 

     if(CryptGetHashParam(hHash, HP_HASHVAL, rgbHash, &cbHash, 0)) 
     { 
      DWORD i; 

      printf("MD5 expected, versus MD5 of file %s is:\n", filename); 

      const char* TextExpected = get_expected_hash(filename); 
      printf("%s\n", TextExpected); 

      for(i = 0; i < cbHash; i++) 
      { 
       printf("%c%c", rgbDigits[rgbHash[i] >> 4], rgbDigits[rgbHash[i] & 0xf]); 
       hashResult[i * 2] = rgbDigits[rgbHash[i] >> 4]; 
       hashResult[i * 2 + 1] = rgbDigits[rgbHash[i] & 0xf]; 
      } 
      //printf("\n"); 

      if(_strcmpi(hashResult, TextExpected) == 0) 
       printf("Hash is the same\n"); 
      else 
       printf("Hash is different\n"); 
     } 
     else 
     { 
      dwStatus = GetLastError(); 
      printf("CryptGetHashParam failed: %d\n", (int)dwStatus); 
      continue; 
     } 

     CryptDestroyHash(hHash); 
     CloseHandle(hFile); 
    } 

    CryptReleaseContext(hProv, 0); 

    return 0; 
} 
Смежные вопросы