-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathHash.cpp
84 lines (73 loc) · 1.95 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
#include <stdio.h>
#include "Hash.h"
char *HashString(const char *dataIn, ALG_ID algo)
{
// set up the crypto environment
HCRYPTPROV provider;
HCRYPTHASH hash;
DWORD lenOut = 0;
// PROV_RSA_FULL NOT support ALG_SHA256, CALG_SHA384 or CALG_SHA512.
// use the provider as PROV_RSA_AES (Microsoft Enhanced RSA and AES Cryptographic Provider)
if (!CryptAcquireContext(&provider, NULL, NULL, PROV_RSA_AES, 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 *md5(const char *str)
{
return HashString(str, CALG_MD5);
}
char *sha1(const char *str)
{
return HashString(str, CALG_SHA1);
}
char *sha256(const char *str)
{
return HashString(str, CALG_SHA_256);
}
char *sha384(const char *str)
{
return HashString(str, CALG_SHA_384);
}
char *sha512(const char *str)
{
return HashString(str, CALG_SHA_512);
}