join 1.0
lightweight network framework library
Loading...
Searching...
No Matches
digest.hpp
Go to the documentation of this file.
1
25#ifndef __JOIN_DIGEST_HPP__
26#define __JOIN_DIGEST_HPP__
27
28// libjoin.
29#include <join/openssl.hpp>
30#include <join/base64.hpp>
31
32namespace join
33{
37 enum class DigestErrc
38 {
42 };
43
47 class DigestCategory : public std::error_category
48 {
49 public:
54 virtual const char* name () const noexcept;
55
61 virtual std::string message (int code) const;
62 };
63
68 const std::error_category& getDigestCategory ();
69
75 std::error_code make_error_code (DigestErrc code);
76
82 std::error_condition make_error_condition (DigestErrc code);
83
85 class Digest;
86
90 class Digestbuf : public std::streambuf
91 {
92 public:
97 Digestbuf (const std::string& algo);
98
103 Digestbuf (const Digestbuf& other) = delete;
104
110 Digestbuf& operator= (const Digestbuf& other) = delete;
111
116 Digestbuf (Digestbuf&& other);
117
124
128 virtual ~Digestbuf () = default;
129
135
136 protected:
142 virtual int_type overflow (int_type c = traits_type::eof ()) override;
143
145 static const std::streamsize _bufsize = 256;
146
148 std::unique_ptr <char []> _buf;
149
151 const EVP_MD* _md;
152
155 };
156
160 class Digest : public std::ostream
161 {
162 public:
176
181 Digest (Algorithm algo);
182
187 Digest (const Digest& other) = delete;
188
194 Digest& operator=(const Digest& other) = delete;
195
200 Digest (Digest&& other);
201
207 Digest& operator=(Digest&& other);
208
212 virtual ~Digest () = default;
213
219
226 static BytesArray md5bin (const char* data, std::streamsize size);
227
233 static BytesArray md5bin (const BytesArray& data);
234
240 static BytesArray md5bin (const std::string& data);
241
248 static std::string md5hex (const char* data, std::streamsize size);
249
255 static std::string md5hex (const BytesArray& data);
256
262 static std::string md5hex (const std::string& data);
263
270 static BytesArray sha1bin (const char* data, std::streamsize size);
271
277 static BytesArray sha1bin (const BytesArray& data);
278
284 static BytesArray sha1bin (const std::string& data);
285
292 static std::string sha1hex (const char* data, std::streamsize size);
293
299 static std::string sha1hex (const BytesArray& data);
300
306 static std::string sha1hex (const std::string& data);
307
314 static BytesArray sha224bin (const char* data, std::streamsize size);
315
321 static BytesArray sha224bin (const BytesArray& data);
322
328 static BytesArray sha224bin (const std::string& data);
329
336 static std::string sha224hex (const char* data, std::streamsize size);
337
343 static std::string sha224hex (const BytesArray& data);
344
350 static std::string sha224hex (const std::string& data);
351
358 static BytesArray sha256bin (const char* data, std::streamsize size);
359
365 static BytesArray sha256bin (const BytesArray& data);
366
372 static BytesArray sha256bin (const std::string& data);
373
380 static std::string sha256hex (const char* data, std::streamsize size);
381
387 static std::string sha256hex (const BytesArray& data);
388
394 static std::string sha256hex (const std::string& data);
395
402 static BytesArray sha384bin (const char* data, std::streamsize size);
403
409 static BytesArray sha384bin (const BytesArray& data);
410
416 static BytesArray sha384bin (const std::string& data);
417
424 static std::string sha384hex (const char* data, std::streamsize size);
425
431 static std::string sha384hex (const BytesArray& data);
432
438 static std::string sha384hex (const std::string& data);
439
446 static BytesArray sha512bin (const char* data, std::streamsize size);
447
453 static BytesArray sha512bin (const BytesArray& data);
454
460 static BytesArray sha512bin (const std::string& data);
461
468 static std::string sha512hex (const char* data, std::streamsize size);
469
475 static std::string sha512hex (const BytesArray& data);
476
482 static std::string sha512hex (const std::string& data);
483
490 static BytesArray sm3bin (const char* data, std::streamsize size);
491
497 static BytesArray sm3bin (const BytesArray& data);
498
504 static BytesArray sm3bin (const std::string& data);
505
512 static std::string sm3hex (const char* data, std::streamsize size);
513
519 static std::string sm3hex (const BytesArray& data);
520
526 static std::string sm3hex (const std::string& data);
527
528 protected:
534 static const char* algorithm (Algorithm algo);
535
538
540 friend class Signature;
541
543 friend class Hmac;
544 };
545}
546
547namespace std
548{
550 template <> struct is_error_condition_enum <join::DigestErrc> : public true_type {};
551}
552
553#endif
digest error category.
Definition digest.hpp:48
virtual std::string message(int code) const
translate digest error code to human readable error string.
Definition digest.cpp:50
virtual const char * name() const noexcept
get digest error category name.
Definition digest.cpp:41
digest stream.
Definition digest.hpp:161
Digest & operator=(const Digest &other)=delete
copy assignment operator.
Digest(const Digest &other)=delete
copy constructor.
static const char * algorithm(Algorithm algo)
get algorithm name.
Definition digest.cpp:642
Algorithm
algorithm.
Definition digest.hpp:167
@ SM3
Definition digest.hpp:174
@ SHA1
Definition digest.hpp:169
@ SHA384
Definition digest.hpp:172
@ SHA512
Definition digest.hpp:173
@ SHA224
Definition digest.hpp:170
@ SHA256
Definition digest.hpp:171
@ MD5
Definition digest.hpp:168
Digest(Algorithm algo)
default constructor.
Definition digest.cpp:204
BytesArray finalize()
get message digest.
Definition digest.cpp:236
Digestbuf _digestbuf
associated digest stream buffer.
Definition digest.hpp:537
virtual ~Digest()=default
destroy digest stream instance.
digest stream buffer.
Definition digest.hpp:91
Digestbuf(const Digestbuf &other)=delete
copy constructor.
Digestbuf(const std::string &algo)
create digest stream buffer instance.
Definition digest.cpp:97
Digestbuf & operator=(const Digestbuf &other)=delete
copy assignment operator.
static const std::streamsize _bufsize
internal buffer size.
Definition digest.hpp:145
EvpMdCtxPtr _ctx
message digest context.
Definition digest.hpp:154
virtual ~Digestbuf()=default
destroy digest stream buffer instance.
BytesArray finalize()
get message digest.
Definition digest.cpp:141
std::unique_ptr< char[]> _buf
internal buffer.
Definition digest.hpp:148
const EVP_MD * _md
message digest.
Definition digest.hpp:151
virtual int_type overflow(int_type c=traits_type::eof()) override
writes characters to the associated output sequence from the put area.
Definition digest.cpp:160
HMAC stream.
Definition hmac.hpp:125
class used to manage signature.
Definition signature.hpp:100
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
Definition acceptor.hpp:32
std::vector< uint8_t > BytesArray
bytes array.
Definition base64.hpp:39
std::error_code make_error_code(join::Errc code)
Create an std::error_code object.
Definition error.cpp:154
std::unique_ptr< EVP_MD_CTX, EvpMdCtxDelete > EvpMdCtxPtr
Definition openssl.hpp:133
const std::error_category & getDigestCategory()
get error category.
Definition digest.cpp:69
DigestErrc
digest error codes.
Definition digest.hpp:38
std::error_condition make_error_condition(join::Errc code)
Create an std::error_condition object.
Definition error.cpp:163
Definition error.hpp:106