forked from bruderstein/nppPluginManager
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmd5.cpp
More file actions
92 lines (75 loc) · 1.85 KB
/
md5.cpp
File metadata and controls
92 lines (75 loc) · 1.85 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
#include <stdio.h>
#include <windows.h>
#include <Wincrypt.h>
#include <tchar.h>
#include "md5.h"
BOOL MD5::hash(const TCHAR *filename, TCHAR *hashBuffer, int hashBufferLength)
{
DWORD dwStatus = 0;
BOOL bResult = FALSE;
HCRYPTPROV hProv = 0;
HCRYPTHASH hHash = 0;
HANDLE hFile = NULL;
BYTE rgbFile[BUFSIZE];
DWORD cbRead = 0;
BYTE rgbHash[MD5LEN];
DWORD cbHash = 0;
if (hashBufferLength < ((MD5LEN * 2) + 1))
return FALSE;
hFile = CreateFile(filename,
GENERIC_READ,
FILE_SHARE_READ,
NULL,
OPEN_EXISTING,
FILE_FLAG_SEQUENTIAL_SCAN,
NULL);
if (INVALID_HANDLE_VALUE == hFile)
{
return FALSE;
}
// Get handle to the crypto provider
if (!CryptAcquireContext(&hProv,
NULL,
NULL,
PROV_RSA_FULL,
CRYPT_VERIFYCONTEXT))
{
return FALSE;
}
if (!CryptCreateHash(hProv, CALG_MD5, 0, 0, &hHash))
{
return FALSE;
}
do
{
if (!ReadFile(hFile, rgbFile, BUFSIZE, &cbRead, NULL))
return FALSE;
if (0 == cbRead)
{
break;
}
if (!CryptHashData(hHash, rgbFile, cbRead, 0))
{
return FALSE;
}
} while (bResult);
cbHash = MD5LEN;
if (CryptGetHashParam(hHash, HP_HASHVAL, rgbHash, &cbHash, 0))
{
TCHAR *currentHashBuffer = hashBuffer;
for (DWORD i = 0; i < cbHash; i++)
{
_stprintf_s(currentHashBuffer, hashBufferLength - (i * 2), _T("%02x"), rgbHash[i]);
currentHashBuffer += 2;
}
dwStatus = TRUE;
}
else
{
dwStatus = FALSE;
}
CryptDestroyHash(hHash);
CryptReleaseContext(hProv, 0);
CloseHandle(hFile);
return dwStatus;
}