BeeeOn Gateway  v2020.3.1-2-g6f737dc
Platform to interconnect the IoT world
Base64.h
1 #pragma once
2 
3 #include <sstream>
4 #include <Poco/Base64Encoder.h>
5 #include <Poco/Base64Decoder.h>
6 #include <Poco/StreamCopier.h>
7 
8 namespace BeeeOn {
9 
10 class Base64 {
11 public:
12  static std::string encode(char *b, size_t blen)
13  {
14  std::ostringstream sOut;
15  Poco::Base64Encoder base64(sOut);
16  base64.rdbuf()->setLineLength(0);
17  base64.write(b, blen);
18  base64.close();
19 
20  return sOut.str();
21  }
22 
23  static size_t decode(const std::string &s, char *b, size_t bcap)
24  {
25  std::istringstream sIn(s);
26  Poco::Base64Decoder base64(sIn);
27 
28  for (size_t i = 0; i < bcap; ++i) {
29  b[i] = base64.get();
30 
31  if (!base64.good() || base64.eof())
32  return i;
33  }
34 
35  return bcap;
36  }
37 
38  static std::string decode(const std::string &s)
39  {
40  std::istringstream sIn(s);
41  Poco::Base64Decoder base64(sIn);
42 
43  std::string result;
44  Poco::StreamCopier::copyToString(base64, result);
45  return result;
46  }
47 };
48 
49 }
Definition: Base64.h:10