forked from noah-/d2bs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathHash.cpp
158 lines (140 loc) · 4.46 KB
/
Hash.cpp
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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
#include <cstdio>
#include "D2Helpers.h"
#include "Hash.h"
using namespace std;
char* HashString(char* dataIn, ALG_ID algo) {
// set up the crypto environment
HCRYPTPROV provider;
HCRYPTHASH hash;
DWORD lenOut = 0;
if (!CryptAcquireContext(&provider, NULL, NULL, PROV_RSA_FULL, CRYPT_VERIFYCONTEXT))
return NULL;
if (!CryptCreateHash(provider, algo, 0, 0, &hash)) {
CryptReleaseContext(provider, 0);
return NULL;
}
// now we have a working crypto environment, let's encrypt
if (!CryptHashData(hash, (BYTE*)dataIn, strlen(dataIn), 0)) {
CryptDestroyHash(hash);
CryptReleaseContext(provider, 0);
return NULL;
}
if (!CryptGetHashParam(hash, HP_HASHVAL, NULL, &lenOut, 0)) {
CryptDestroyHash(hash);
CryptReleaseContext(provider, 0);
return NULL;
}
BYTE* result = new BYTE[lenOut];
memset(result, 0, lenOut);
if (!CryptGetHashParam(hash, HP_HASHVAL, result, &lenOut, 0)) {
delete[] result;
CryptDestroyHash(hash);
CryptReleaseContext(provider, 0);
return NULL;
}
// tear down the crypto environment
CryptDestroyHash(hash);
CryptReleaseContext(provider, 0);
// it's the caller's responsibility to clean up the result
char *szBuffer1 = new char[lenOut * 2 + 1], szBuffer2[10] = "";
memset(szBuffer1, 0, lenOut * 2 + 1);
for (DWORD i = 0; i < lenOut; i++) {
sprintf_s(szBuffer2, 10, "%.2x", result[i]);
strcat_s(szBuffer1, lenOut * 2 + 1, szBuffer2);
}
delete[] result;
return szBuffer1;
}
char* HashFile(wchar_t* file, ALG_ID algo) {
// set up the crypto environment
HCRYPTPROV provider;
HCRYPTHASH hash;
DWORD lenOut = 0;
if (!CryptAcquireContext(&provider, NULL, NULL, PROV_RSA_FULL, 0))
return NULL;
if (!CryptCreateHash(provider, algo, 0, 0, &hash)) {
CryptReleaseContext(provider, 0);
return NULL;
}
// now we have a working crypto environment, let's encrypt
// open the file
FILE* fp = NULL;
_wfopen_s(&fp, file, L"r");
if (!fp)
return NULL;
// read it to end, adding it to the hash stream
fseek(fp, 0, SEEK_END);
unsigned int size = ftell(fp);
fseek(fp, 0, SEEK_SET);
char* contents = new char[size];
if (fread(contents, sizeof(char), size, fp) != size && ferror(fp)) {
fclose(fp);
delete[] contents;
CryptDestroyHash(hash);
CryptReleaseContext(provider, 0);
return NULL;
}
fclose(fp);
if (!CryptHashData(hash, (BYTE*)contents, size, 0)) {
delete[] contents;
CryptDestroyHash(hash);
CryptReleaseContext(provider, 0);
return NULL;
}
delete[] contents;
if (!CryptGetHashParam(hash, HP_HASHVAL, NULL, &lenOut, 0)) {
CryptDestroyHash(hash);
CryptReleaseContext(provider, 0);
return NULL;
}
BYTE* result = new BYTE[lenOut];
memset(result, 0, lenOut);
if (!CryptGetHashParam(hash, HP_HASHVAL, result, &lenOut, 0)) {
delete[] result;
CryptDestroyHash(hash);
CryptReleaseContext(provider, 0);
return NULL;
}
// tear down the crypto environment
CryptDestroyHash(hash);
CryptReleaseContext(provider, 0);
// it's the caller's responsibility to clean up the result
char *szBuffer1 = new char[lenOut * 2 + 1], szBuffer2[10] = "";
memset(szBuffer1, 0, lenOut * 2 + 1);
for (DWORD i = 0; i < lenOut; i++) {
sprintf_s(szBuffer2, 10, "%.2x", result[i]);
strcat_s(szBuffer1, lenOut * 2 + 1, szBuffer2);
}
delete[] result;
return szBuffer1;
}
char* md5(char* str) {
return HashString(str, CALG_MD5);
}
char* sha1(char* str) {
return HashString(str, CALG_SHA1);
}
char* sha256(char* str) {
return HashString(str, CALG_SHA_256);
}
char* sha384(char* str) {
return HashString(str, CALG_SHA_384);
}
char* sha512(char* str) {
return HashString(str, CALG_SHA_512);
}
char* md5_file(wchar_t* file) {
return HashFile(file, CALG_MD5);
}
char* sha1_file(wchar_t* file) {
return HashFile(file, CALG_SHA1);
}
char* sha256_file(wchar_t* file) {
return HashFile(file, CALG_SHA_256);
}
char* sha384_file(wchar_t* file) {
return HashFile(file, CALG_SHA_384);
}
char* sha512_file(wchar_t* file) {
return HashFile(file, CALG_SHA_512);
}