-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathHasherSha1Traits.hpp
224 lines (181 loc) · 7.22 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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
#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_ENHANCED_PROV, PROV_RSA_FULL, CRYPT_VERIFYCONTEXT)) {
auto err = GetLastError();
if (err == NTE_BAD_KEYSET) {
if (!CryptAcquireContext(&CryptProvider.Handle, NULL, MS_ENHANCED_PROV, PROV_RSA_FULL, CRYPT_VERIFYCONTEXT | 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_ENHANCED_PROV, PROV_RSA_FULL, CRYPT_VERIFYCONTEXT)) {
auto err = GetLastError();
if (err == NTE_BAD_KEYSET) {
if (!CryptAcquireContext(&CryptProvider.Handle, NULL, MS_ENHANCED_PROV, PROV_RSA_FULL, CRYPT_VERIFYCONTEXT | 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;
}
}
};
#else
#include "SHA1.hpp"
struct HasherSha1Traits {
public:
static constexpr size_t BlockSize = 512 / 8;
static constexpr size_t DigestSize = 160 / 8;
struct DigestType {
uint8_t Bytes[DigestSize];
};
struct ContextType {
SHA1_CTX sha1;
ContextType() noexcept {}
ContextType(const ContextType& Other) noexcept = default;
ContextType(ContextType&& Other) noexcept {
Other.sha1 = sha1;
}
ContextType& operator=(const ContextType& Other) noexcept = default;
ContextType& operator=(ContextType&& Other) noexcept {
sha1 = Other.sha1;
return *this;
}
};
static inline ContextType ContextCreate() noexcept {
ContextType Ctx;
SHA1Init(&Ctx.sha1);
return Ctx;
}
static inline ContextType ContextCreate(const void* lpBuffer, size_t cbBuffer) noexcept{
ContextType Ctx;
SHA1Init(&Ctx.sha1);
ContextUpdate(Ctx, lpBuffer, cbBuffer);
return Ctx;
}
static inline ContextType ContextCopy(const ContextType& Ctx) noexcept {
ContextType NewCtx;
NewCtx.sha1 = Ctx.sha1;
return NewCtx;
}
static inline void ContextUpdate(ContextType& Ctx, const void* lpBuffer, size_t cbBuffer) noexcept {
if constexpr (sizeof(size_t) <= sizeof(uint32_t)) {
SHA1Update(&Ctx.sha1, reinterpret_cast<const uint8_t*>(lpBuffer), static_cast<uint32_t>(cbBuffer));
} else {
size_t BytesRead = 0;
uint32_t BytesToRead = cbBuffer - BytesRead > UINT32_MAX ? UINT32_MAX : static_cast<uint32_t>(cbBuffer - BytesRead);
do {
SHA1Update(&Ctx.sha1, reinterpret_cast<const uint8_t*>(lpBuffer) + BytesRead, BytesToRead);
BytesRead += BytesToRead;
BytesToRead = cbBuffer - BytesRead > UINT32_MAX ? UINT32_MAX : static_cast<uint32_t>(cbBuffer - BytesRead);
} while (BytesToRead);
}
}
static inline void ContextEvaluate(ContextType Ctx, DigestType& Digest) noexcept {
SHA1Final(Digest.Bytes, &Ctx.sha1);
}
static inline void ContextDestroy(ContextType& Ctx) noexcept {}
};
#endif