join 1.0
lightweight network framework library
Loading...
Searching...
No Matches
base64.hpp
Go to the documentation of this file.
1
25#ifndef JOIN_CRYPTO_BASE64_HPP
26#define JOIN_CRYPTO_BASE64_HPP
27
28// libjoin.
29#include <join/openssl.hpp>
30
31// C++.
32#include <iostream>
33#include <iomanip>
34#include <string>
35#include <vector>
36
37// C.
38#include <cstdint>
39
40namespace join
41{
43 using BytesArray = std::vector<uint8_t>;
44
50 inline std::string bin2hex (const BytesArray& bin)
51 {
52 std::stringstream oss;
53 for (size_t i = 0; i < bin.size (); ++i)
54 {
55 oss << std::hex << std::setw (2) << std::setfill ('0') << static_cast<uint32_t> (bin[i]);
56 }
57 return oss.str ();
58 }
59
63 class Encoderbuf : public std::streambuf
64 {
65 public:
69 Encoderbuf ();
70
75 Encoderbuf (const Encoderbuf& other) = delete;
76
82 Encoderbuf& operator= (const Encoderbuf& other) = delete;
83
88 Encoderbuf (Encoderbuf&& other);
89
96
100 virtual ~Encoderbuf () = default;
101
106 std::string get ();
107
108 protected:
114 virtual int_type overflow (int_type c = traits_type::eof ()) override;
115
117 static const std::streamsize _bufsize = 256;
118
120 std::unique_ptr<char[]> _buf;
121
124
126 std::string _out;
127 };
128
132 class Encoder : public std::ostream
133 {
134 public:
138 Encoder ();
139
144 Encoder (const Encoder& other) = delete;
145
151 Encoder& operator= (const Encoder& other) = delete;
152
157 Encoder (Encoder&& other);
158
164 Encoder& operator= (Encoder&& other);
165
169 virtual ~Encoder () = default;
170
175 std::string get ();
176
177 protected:
180 };
181
185 class Decoderbuf : public std::streambuf
186 {
187 public:
191 Decoderbuf ();
192
197 Decoderbuf (const Decoderbuf& other) = delete;
198
204 Decoderbuf& operator= (const Decoderbuf& other) = delete;
205
210 Decoderbuf (Decoderbuf&& other);
211
218
222 virtual ~Decoderbuf () = default;
223
228 BytesArray get ();
229
230 protected:
236 virtual int_type overflow (int_type c = traits_type::eof ()) override;
237
239 static const std::streamsize _bufsize = 256;
240
242 std::unique_ptr<char[]> _buf;
243
246
249 };
250
254 class Decoder : public std::ostream
255 {
256 public:
260 Decoder ();
261
266 Decoder (const Decoder& other) = delete;
267
273 Decoder& operator= (const Decoder& other) = delete;
274
279 Decoder (Decoder&& other);
280
286 Decoder& operator= (Decoder&& other);
287
291 virtual ~Decoder () = default;
292
297 BytesArray get ();
298
299 protected:
302 };
303
307 class Base64
308 {
309 public:
313 Base64 () = delete;
314
318 ~Base64 () = delete;
319
326 static std::string encode (const char* data, size_t size);
327
333 static std::string encode (const std::string& data);
334
340 static std::string encode (const BytesArray& data);
341
347 static BytesArray decode (const std::string& data);
348 };
349}
350
351#endif
base64 encode, decode class.
Definition base64.hpp:308
static std::string encode(const char *data, size_t size)
encode data.
Definition base64.cpp:345
static BytesArray decode(const std::string &data)
decode a base64 encoded string.
Definition base64.cpp:374
Base64()=delete
create the Base64 instance.
~Base64()=delete
destroy the Base64 instance.
decoder.
Definition base64.hpp:255
Decoder(const Decoder &other)=delete
copy constructor.
Decoderbuf _decoderbuf
associated decoder stream buffer.
Definition base64.hpp:301
Decoder()
default constructor.
Definition base64.cpp:300
Decoder & operator=(const Decoder &other)=delete
copy assignment operator.
BytesArray get()
get decoded data.
Definition base64.cpp:331
virtual ~Decoder()=default
destroy instance.
decoder stream buffer.
Definition base64.hpp:186
static const std::streamsize _bufsize
internal buffer size.
Definition base64.hpp:239
Decoderbuf & operator=(const Decoderbuf &other)=delete
copy assignment operator.
Decoderbuf(const Decoderbuf &other)=delete
copy constructor.
std::unique_ptr< char[]> _buf
internal buffer.
Definition base64.hpp:242
BytesArray _out
output buffer.
Definition base64.hpp:248
EvpEncodeCtxPtr _decodectx
decode context.
Definition base64.hpp:245
Decoderbuf()
create decoder stream buffer instance.
Definition base64.cpp:191
virtual ~Decoderbuf()=default
destroy encoder stream buffer instance.
virtual int_type overflow(int_type c=traits_type::eof()) override
writes characters to the associated output sequence from the put area.
Definition base64.cpp:249
BytesArray get()
get decoded data.
Definition base64.cpp:225
encoder.
Definition base64.hpp:133
std::string get()
get encoded string.
Definition base64.cpp:177
Encoder & operator=(const Encoder &other)=delete
copy assignment operator.
virtual ~Encoder()=default
destroy instance.
Encoderbuf _encoderbuf
associated encoder stream buffer.
Definition base64.hpp:179
Encoder(const Encoder &other)=delete
copy constructor.
Encoder()
default constructor.
Definition base64.cpp:146
encoder stream buffer.
Definition base64.hpp:64
virtual ~Encoderbuf()=default
destroy encoder stream buffer instance.
Encoderbuf & operator=(const Encoderbuf &other)=delete
copy assignment operator.
std::string get()
get encoded string.
Definition base64.cpp:75
Encoderbuf(const Encoderbuf &other)=delete
copy constructor.
virtual int_type overflow(int_type c=traits_type::eof()) override
writes characters to the associated output sequence from the put area.
Definition base64.cpp:99
std::string _out
output buffer.
Definition base64.hpp:126
EvpEncodeCtxPtr _encodectx
encode context.
Definition base64.hpp:123
std::unique_ptr< char[]> _buf
internal buffer.
Definition base64.hpp:120
Encoderbuf()
create encoder stream buffer instance.
Definition base64.cpp:41
static const std::streamsize _bufsize
internal buffer size.
Definition base64.hpp:117
Definition acceptor.hpp:32
std::unique_ptr< EVP_ENCODE_CTX, EvpEncodeCtxDelete > EvpEncodeCtxPtr
Definition openssl.hpp:118
std::string bin2hex(const BytesArray &bin)
convert bytes array to string.
Definition base64.hpp:50
std::vector< uint8_t > BytesArray
bytes array.
Definition base64.hpp:43