join 1.0
lightweight network framework library
Loading...
Searching...
No Matches
hmac.hpp
Go to the documentation of this file.
1
25#ifndef __JOIN_HMAC_HPP__
26#define __JOIN_HMAC_HPP__
27
28// libjoin.
29#include <join/digest.hpp>
30
31namespace join
32{
34 class Hmac;
35
39 class Hmacbuf : public std::streambuf
40 {
41 public:
47 Hmacbuf (const std::string& algo, const std::string& key);
48
53 Hmacbuf (const Hmacbuf& other) = delete;
54
60 Hmacbuf& operator= (const Hmacbuf& other) = delete;
61
66 Hmacbuf (Hmacbuf&& other);
67
73 Hmacbuf& operator= (Hmacbuf&& other);
74
78 virtual ~Hmacbuf () = default;
79
85
86 protected:
92 virtual int_type overflow (int_type c = traits_type::eof ()) override;
93
95 static const std::streamsize _bufsize = 256;
96
98 std::unique_ptr <char []> _buf;
99
100 #if OPENSSL_VERSION_NUMBER < 0x30000000L
102 const EVP_MD* _md;
103
106 #else
108 EvpMacPtr _mac;
109
111 std::string _algo;
112
114 EvpMacCtxPtr _ctx;
115 #endif
116
118 std::string _key;
119 };
120
124 class Hmac : public std::ostream
125 {
126 public:
132 Hmac (Digest::Algorithm algo, const std::string& key);
133
138 Hmac (const Hmac& other) = delete;
139
145 Hmac& operator=(const Hmac& other) = delete;
146
151 Hmac (Hmac&& other);
152
158 Hmac& operator=(Hmac&& other);
159
163 virtual ~Hmac () = default;
164
170
178 static BytesArray md5bin (const char* message, std::streamsize size, const std::string& key);
179
186 static BytesArray md5bin (const BytesArray& message, const std::string& key);
187
194 static BytesArray md5bin (const std::string& message, const std::string& key);
195
203 static std::string md5hex (const char* data, std::streamsize size, const std::string& key);
204
211 static std::string md5hex (const BytesArray& data, const std::string& key);
212
219 static std::string md5hex (const std::string& data, const std::string& key);
220
228 static BytesArray sha1bin (const char* message, std::streamsize size, const std::string& key);
229
236 static BytesArray sha1bin (const BytesArray& message, const std::string& key);
237
244 static BytesArray sha1bin (const std::string& message, const std::string& key);
245
253 static std::string sha1hex (const char* data, std::streamsize size, const std::string& key);
254
261 static std::string sha1hex (const BytesArray& data, const std::string& key);
262
269 static std::string sha1hex (const std::string& data, const std::string& key);
270
278 static BytesArray sha224bin (const char* message, std::streamsize size, const std::string& key);
279
286 static BytesArray sha224bin (const BytesArray& message, const std::string& key);
287
294 static BytesArray sha224bin (const std::string& message, const std::string& key);
295
303 static std::string sha224hex (const char* data, std::streamsize size, const std::string& key);
304
311 static std::string sha224hex (const BytesArray& data, const std::string& key);
312
319 static std::string sha224hex (const std::string& data, const std::string& key);
320
328 static BytesArray sha256bin (const char* message, std::streamsize size, const std::string& key);
329
336 static BytesArray sha256bin (const BytesArray& message, const std::string& key);
337
344 static BytesArray sha256bin (const std::string& message, const std::string& key);
345
353 static std::string sha256hex (const char* data, std::streamsize size, const std::string& key);
354
361 static std::string sha256hex (const BytesArray& data, const std::string& key);
362
369 static std::string sha256hex (const std::string& data, const std::string& key);
370
378 static BytesArray sha384bin (const char* message, std::streamsize size, const std::string& key);
379
386 static BytesArray sha384bin (const BytesArray& message, const std::string& key);
387
394 static BytesArray sha384bin (const std::string& message, const std::string& key);
395
403 static std::string sha384hex (const char* data, std::streamsize size, const std::string& key);
404
411 static std::string sha384hex (const BytesArray& data, const std::string& key);
412
419 static std::string sha384hex (const std::string& data, const std::string& key);
420
428 static BytesArray sha512bin (const char* message, std::streamsize size, const std::string& key);
429
436 static BytesArray sha512bin (const BytesArray& message, const std::string& key);
437
444 static BytesArray sha512bin (const std::string& message, const std::string& key);
445
453 static std::string sha512hex (const char* data, std::streamsize size, const std::string& key);
454
461 static std::string sha512hex (const BytesArray& data, const std::string& key);
462
469 static std::string sha512hex (const std::string& data, const std::string& key);
470
478 static BytesArray sm3bin (const char* message, std::streamsize size, const std::string& key);
479
486 static BytesArray sm3bin (const BytesArray& message, const std::string& key);
487
494 static BytesArray sm3bin (const std::string& message, const std::string& key);
495
503 static std::string sm3hex (const char* data, std::streamsize size, const std::string& key);
504
511 static std::string sm3hex (const BytesArray& data, const std::string& key);
512
519 static std::string sm3hex (const std::string& data, const std::string& key);
520
521 protected:
524 };
525}
526
527#endif
Algorithm
algorithm.
Definition digest.hpp:167
HMAC stream.
Definition hmac.hpp:125
virtual ~Hmac()=default
destroy HMAC stream instance.
Hmac(const Hmac &other)=delete
copy constructor.
BytesArray finalize()
get keyed-hash message authentication code.
Definition hmac.cpp:218
Hmacbuf _hmacbuf
associated HMAC stream buffer.
Definition hmac.hpp:523
Hmac & operator=(const Hmac &other)=delete
copy assignment operator.
Hmac(Digest::Algorithm algo, const std::string &key)
default constructor.
Definition hmac.cpp:186
HMAC stream buffer.
Definition hmac.hpp:40
Hmacbuf & operator=(const Hmacbuf &other)=delete
copy assignment operator.
Hmacbuf(const Hmacbuf &other)=delete
copy constructor.
std::unique_ptr< char[]> _buf
internal buffer.
Definition hmac.hpp:98
virtual ~Hmacbuf()=default
destroy HMAC stream buffer instance.
const EVP_MD * _md
message digest.
Definition hmac.hpp:102
BytesArray finalize()
get keyed-hash message authentication code.
Definition hmac.cpp:104
static const std::streamsize _bufsize
internal buffer size.
Definition hmac.hpp:95
Hmacbuf(const std::string &algo, const std::string &key)
create HMAC stream buffer instance.
Definition hmac.cpp:40
std::string _key
key.
Definition hmac.hpp:118
virtual int_type overflow(int_type c=traits_type::eof()) override
writes characters to the associated output sequence from the put area.
Definition hmac.cpp:128
HmacCtxPtr _ctx
context.
Definition hmac.hpp:105
BytesArray sha256bin
Definition digest_test.cpp:56
BytesArray sha384bin
Definition digest_test.cpp:62
const std::string sha512hex
Definition digest_test.cpp:93
const std::string sha1hex
Definition digest_test.cpp:89
BytesArray sm3bin
Definition digest_test.cpp:80
BytesArray md5bin
message digest in binary format.
Definition digest_test.cpp:41
BytesArray sha224bin
Definition digest_test.cpp:50
const std::string sha384hex
Definition digest_test.cpp:92
const std::string sha224hex
Definition digest_test.cpp:90
const std::string sha256hex
Definition digest_test.cpp:91
BytesArray sha512bin
Definition digest_test.cpp:70
BytesArray sha1bin
Definition digest_test.cpp:45
const std::string sm3hex
Definition digest_test.cpp:94
const std::string md5hex
message digest in hexadecimal format.
Definition digest_test.cpp:88
const std::string key(65, 'a')
key.
Definition acceptor.hpp:32
std::unique_ptr< HMAC_CTX, HmacCtxDelete > HmacCtxPtr
Definition openssl.hpp:149
std::vector< uint8_t > BytesArray
bytes array.
Definition base64.hpp:39