forked from bitcookies/winrar-keygen
-
Notifications
You must be signed in to change notification settings - Fork 0
/
HasherSha1Traits.hpp
153 lines (125 loc) · 5 KB
/
HasherSha1Traits.hpp
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
#pragma once
#ifdef _MSC_VER
#include <windows.h>
#include <wincrypt.h>
#include <system_error>
struct HasherSha1Traits {
public:
static constexpr size_t BlockSize = 512 / 8;
static constexpr size_t DigestSize = 160 / 8;
struct DigestType {
BYTE Bytes[DigestSize];
};
struct ContextType {
HCRYPTHASH hHash;
ContextType() noexcept :
hHash(NULL) {}
ContextType(HCRYPTHASH HashHandle) noexcept :
hHash(HashHandle) {}
ContextType(const ContextType& Other) noexcept = default;
ContextType(ContextType&& Other) noexcept : hHash(Other.hHash) {
Other.hHash = NULL;
}
ContextType& operator=(const ContextType& Other) noexcept = default;
ContextType& operator=(ContextType&& Other) noexcept {
hHash = Other.hHash;
Other.hHash = NULL;
return *this;
}
};
private:
static inline struct ContextProvider {
HCRYPTPROV Handle;
~ContextProvider() {
if (Handle) {
CryptReleaseContext(Handle, 0);
Handle = NULL;
}
}
} CryptProvider;
public:
static inline ContextType ContextCreate() {
ContextType Ctx;
if (CryptProvider.Handle == NULL) {
if (!CryptAcquireContext(&CryptProvider.Handle, NULL, MS_ENH_RSA_AES_PROV, PROV_RSA_AES, 0)) {
auto err = GetLastError();
if (err == NTE_BAD_KEYSET) {
if (!CryptAcquireContext(&CryptProvider.Handle, NULL, MS_ENH_RSA_AES_PROV, PROV_RSA_AES, CRYPT_NEWKEYSET)) {
err = GetLastError();
throw std::system_error(err, std::system_category());
}
} else {
throw std::system_error(err, std::system_category());
}
}
}
if (!CryptCreateHash(CryptProvider.Handle, CALG_SHA1, NULL, 0, &Ctx.hHash)) {
auto err = GetLastError();
throw std::system_error(err, std::system_category());
}
return Ctx;
}
static inline ContextType ContextCreate(const void* lpBuffer, size_t cbBuffer) {
ContextType Ctx;
if (CryptProvider.Handle == NULL) {
if (!CryptAcquireContext(&CryptProvider.Handle, NULL, MS_ENH_RSA_AES_PROV, PROV_RSA_AES, 0)) {
auto err = GetLastError();
if (err == NTE_BAD_KEYSET) {
if (!CryptAcquireContext(&CryptProvider.Handle, NULL, MS_ENH_RSA_AES_PROV, PROV_RSA_AES, CRYPT_NEWKEYSET)) {
err = GetLastError();
throw std::system_error(err, std::system_category());
}
} else {
throw std::system_error(err, std::system_category());
}
}
}
if (!CryptCreateHash(CryptProvider.Handle, CALG_SHA1, NULL, 0, &Ctx.hHash)) {
auto err = GetLastError();
throw std::system_error(err, std::system_category());
}
ContextUpdate(Ctx, lpBuffer, cbBuffer);
return Ctx;
}
static inline ContextType ContextCopy(const ContextType& Ctx) {
ContextType NewCtx;
if (!CryptDuplicateHash(Ctx.hHash, NULL, 0, &NewCtx.hHash)) {
auto err = GetLastError();
throw std::system_error(err, std::system_category());
}
return NewCtx;
}
static inline void ContextUpdate(ContextType& Ctx, const void* lpBuffer, size_t cbBuffer) {
if constexpr (sizeof(size_t) <= sizeof(DWORD)) {
if (!CryptHashData(Ctx.hHash, reinterpret_cast<const BYTE*>(lpBuffer), static_cast<DWORD>(cbBuffer), 0)) {
auto err = GetLastError();
throw std::system_error(err, std::system_category());
}
} else {
size_t BytesRead = 0;
DWORD BytesToRead = cbBuffer - BytesRead > MAXDWORD ? MAXDWORD : static_cast<DWORD>(cbBuffer - BytesRead);
do {
if (!CryptHashData(Ctx.hHash, reinterpret_cast<const BYTE*>(lpBuffer) + BytesRead, BytesToRead, 0)) {
auto err = GetLastError();
throw std::system_error(err, std::system_category());
}
BytesRead += BytesToRead;
BytesToRead = cbBuffer - BytesRead > MAXDWORD ? MAXDWORD : static_cast<DWORD>(cbBuffer - BytesRead);
} while (BytesToRead);
}
}
static inline void ContextEvaluate(const ContextType& Ctx, DigestType& Digest) {
DWORD SizeOfDigest = sizeof(Digest.Bytes);
if (!CryptGetHashParam(Ctx.hHash, HP_HASHVAL, Digest.Bytes, &SizeOfDigest, 0)) {
auto err = GetLastError();
throw std::system_error(err, std::system_category());
}
}
static inline void ContextDestroy(ContextType& Ctx) noexcept {
if (Ctx.hHash) {
CryptDestroyHash(Ctx.hHash);
Ctx.hHash = NULL;
}
}
};
#endif