wxExtend
Additional templates and function helpers for wxWidgets
Loading...
Searching...
No Matches
crypto.h
1/*
2 ​​​​SPDX-License-Identifier: GPL-3.0-or-later
3 Copyright © 2016-2022 Amebis
4 Copyright © 2016 GÉANT
5*/
6
7#pragma once
8
9#include "common.h"
10
11#include <codeanalysis\warnings.h>
12#pragma warning(push)
13#pragma warning(disable: WXWIDGETS_CODE_ANALYSIS_WARNINGS)
14#include <wx/buffer.h>
15#include <wx/ffile.h>
16#include <wx/log.h>
17#include <wx/string.h>
18#pragma warning(pop)
19
20#include <Wincrypt.h>
21
24
29{
30protected:
31 HCRYPTPROV m_h;
32
33public:
38
39
43 virtual ~wxCryptoSession();
44
45
53 inline bool IsOk() const noexcept
54 {
55 return m_h != NULL;
56 }
57
58
62 inline operator HCRYPTPROV() const noexcept
63 {
64 return m_h;
65 }
66};
67
68
80
81
86{
87protected:
88 HCRYPTHASH m_h;
89
90public:
95
96
100 virtual ~wxCryptoHash();
101
102
110 inline bool IsOk() const noexcept
111 {
112 return m_h != NULL;
113 }
114
115
119 inline operator HCRYPTHASH() const noexcept
120 {
121 return m_h;
122 }
123
124
135 _Success_(return != 0) bool Hash(_In_reads_bytes_(size) const void *data, _In_ size_t size);
136
137
147 inline _Success_(return != 0) bool Hash(_In_ const wxMemoryBuffer &data)
148 {
149 return Hash(data.GetData(), data.GetDataLen());
150 }
151
152
162 inline _Success_(return != 0) bool HashAsUTF8(_In_ const wxString &str)
163 {
164 const wxScopedCharBuffer buf(str.ToUTF8());
165 return Hash(buf.data(), buf.length());
166 }
167
168
178 inline _Success_(return != 0) bool HashFile(_In_ const wxString &fileName)
179 {
180 wxFFile file(fileName, wxT("rb"));
181 if (file.IsOpened()) {
182 wxMemoryBuffer buf(4*1024);
183 void *data = buf.GetData();
184 const size_t nBlock = buf.GetBufSize();
185 while (!file.Eof())
186 Hash(data, file.Read(data, nBlock));
187
188 return true;
189 } else {
190 wxLogError(wxT("Can not open %s file for reading."), fileName);
191 return false;
192 }
193 }
194
195
205 virtual _Success_(return != 0) bool GetValue(_Out_ wxMemoryBuffer &hash);
206
207
217 _Success_(return != 0) bool Sign(_Inout_ wxMemoryBuffer &signature);
218
219
225 inline wxMemoryBuffer Sign()
226 {
227 wxMemoryBuffer signature;
228 wxVERIFY(Sign(signature));
229 return signature;
230 }
231};
232
233
238{
239public:
244
245
255 virtual _Success_(return != 0) bool GetValue(_Out_ wxMemoryBuffer &hash);
256};
257
258
259#if (NTDDI_VERSION > NTDDI_WINXPSP2)
260
264class WXEXTEND_API wxCryptoHashSHA256 : public wxCryptoHash
265{
266public:
270 wxCryptoHashSHA256(wxCryptoSession &session);
271
272
282 virtual _Success_(return != 0) bool GetValue(_Out_ wxMemoryBuffer &hash);
283};
284
285#endif
286
287
292{
293protected:
294 HCRYPTKEY m_h;
295
296public:
300 wxCryptoKey();
301
302
306 virtual ~wxCryptoKey();
307
308
316 inline bool IsOk() const noexcept
317 {
318 return m_h != NULL;
319 }
320
321
325 inline operator HCRYPTKEY() const noexcept
326 {
327 return m_h;
328 }
329
330
334 _Success_(return != 0) bool ImportPrivate(_Inout_ wxCryptoSession &session, _In_reads_bytes_(size) const void *data, _In_ size_t size);
335
336
340 _Success_(return != 0) bool ImportPublic(_Inout_ wxCryptoSession &session, _In_reads_bytes_(size) const void *data, _In_ size_t size);
341};
342
343
356_Success_(return != 0) bool WXEXTEND_API wxCryptoVerifySignature(_In_ const wxCryptoHash &hash, _In_reads_bytes_(signature_size) const void *signature_data, _In_ size_t signature_size, _In_ const wxCryptoKey &key);
357
358
370inline _Success_(return != 0) bool wxCryptoVerifySignature(_In_ const wxCryptoHash &hash, _In_ const wxMemoryBuffer &signature, _In_ const wxCryptoKey &key)
371{
372 return wxCryptoVerifySignature(hash, signature.GetData(), signature.GetDataLen(), key);
373}
374
Cryptographics Hash Base.
Definition crypto.h:86
HCRYPTHASH m_h
Hash Handle.
Definition crypto.h:88
bool IsOk() const noexcept
Has the hash creation been successful?
Definition crypto.h:110
SHA-1 Cryptographics Hash.
Definition crypto.h:238
Cryptographics Key Base.
Definition crypto.h:292
HCRYPTKEY m_h
Key Handle.
Definition crypto.h:294
bool IsOk() const noexcept
Has the key creation been successful?
Definition crypto.h:316
Cryptographics Session Base Class.
Definition crypto.h:29
bool IsOk() const noexcept
Has the session creation been successful?
Definition crypto.h:53
HCRYPTPROV m_h
Session Handle.
Definition crypto.h:31
RSA AES Cryptographics Session.
Definition crypto.h:73
#define WXEXTEND_API
Public function calling convention.
Definition common.h:56
bool WXEXTEND_API wxCryptoVerifySignature(const wxCryptoHash &hash, _In_reads_bytes_(signature_size) const void *signature_data, size_t signature_size, const wxCryptoKey &key)
Verifies if the hash matches signature and the public key.
#define wxVERIFY(cond)
Test if condition is true. When not true, raise debug assertion with the given message.
Definition common.h:84