Panoptes 1.0.0
Endpoint Detection and Response
Loading...
Searching...
No Matches
Functions
hash.h File Reference
#include <Windows.h>
#include <string>

Go to the source code of this file.

Functions

std::string GenerateMD5 (std::string filePath)
 Generate an MD5 hash of a file using the Windows Crypto API https://learn.microsoft.com/en-us/windows/win32/seccrypto/example-c-program&ndash;creating-an-md-5-hash-from-file-content.
 

Function Documentation

◆ GenerateMD5()

std::string GenerateMD5 ( std::string  filePath)

Generate an MD5 hash of a file using the Windows Crypto API https://learn.microsoft.com/en-us/windows/win32/seccrypto/example-c-program&ndash;creating-an-md-5-hash-from-file-content.

Parameters
filePathThe path to the file to hash
Returns
The MD5 hash of the file

Definition at line 8 of file hash.cpp.

9{
10 if (filePath.empty())
11 {
12 return "";
13 }
14 WCHAR fileHash[33];
15 HANDLE hFile = CreateFileA(filePath.c_str(), GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, FILE_FLAG_SEQUENTIAL_SCAN, NULL);
16 if (hFile == INVALID_HANDLE_VALUE) {
17 return "";
18 }
19
20 HCRYPTPROV hProv = 0;
21 HCRYPTHASH hHash = 0;
22 BYTE rgbHash[16];
23 DWORD cbHash = 0;
24 WCHAR rgbDigits[] = L"0123456789abcdef";
25
26 if (!CryptAcquireContext(&hProv, NULL, NULL, PROV_RSA_FULL, CRYPT_VERIFYCONTEXT) ||
27 !CryptCreateHash(hProv, CALG_MD5, 0, 0, &hHash)) {
28 CloseHandle(hFile);
29 return "";
30 }
31
32 BYTE rgbFile[1024];
33 DWORD bytesRead = 0;
34 while (ReadFile(hFile, rgbFile, sizeof(rgbFile), &bytesRead, NULL)) {
35 if (bytesRead == 0) break;
36 if (!CryptHashData(hHash, rgbFile, bytesRead, 0)) {
37 CryptReleaseContext(hProv, 0);
38 CryptDestroyHash(hHash);
39 CloseHandle(hFile);
40 return "";
41 }
42 }
43
44 cbHash = 16;
45 if (CryptGetHashParam(hHash, HP_HASHVAL, rgbHash, &cbHash, 0)) {
46 for (DWORD i = 0; i < cbHash; i++) {
47 fileHash[i * 2] = rgbDigits[rgbHash[i] >> 4];
48 fileHash[i * 2 + 1] = rgbDigits[rgbHash[i] & 0xf];
49 }
50 fileHash[32] = L'\0';
51 }
52
53 int size_needed = WideCharToMultiByte(CP_UTF8, 0, fileHash, -1, NULL, 0, NULL, NULL);
54 std::string result(size_needed, 0);
55 WideCharToMultiByte(CP_UTF8, 0, fileHash, -1, &result[0], size_needed, NULL, NULL);
56 std::string test = std::string(result.begin(), result.end());
57 CryptReleaseContext(hProv, 0);
58 CryptDestroyHash(hHash);
59 CloseHandle(hFile);
60 return result;
61}
ULONG result
Definition events.cpp:22
unsigned char BYTE
Definition inject.h:4
unsigned long DWORD
Definition inject.h:2

References result.