GitWatcherBot  1.0.0
A Telegram Bot that notifies you when a new change is made in your repositories (issues, pull requests, stars, forks, and watches)
All Classes Namespaces Files Functions Variables Typedefs Macros
Base64.hpp
Go to the documentation of this file.
1 #pragma once
2 
3 #include <vector>
4 #include <string>
5 
7 static std::string base64Encode(const std::string &in) {
8 
9  std::string out;
10 
11  int val = 0, valb = -6;
12  for (std::uint8_t c: in) {
13  val = (val << 8) + c;
14  valb += 8;
15  while (valb >= 0) {
16  out.push_back("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"[(val >> valb) & 0x3F]);
17  valb -= 6;
18  }
19  }
20  if (valb > -6) out.push_back("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"[((val << 8) >> (valb + 8)) & 0x3F]);
21  while (out.size() % 4) out.push_back('=');
22  return out;
23 }
24 
26 static std::string base64Decode(const std::string &in) {
27 
28  std::string out;
29 
30  std::vector<int> T(256, -1);
31  for (int i = 0; i < 64; i++) T["ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"[i]] = i;
32 
33  int val = 0, valb = -8;
34  for (std::uint8_t c: in) {
35  if (T[c] == -1) break;
36  val = (val << 6) + T[c];
37  valb += 6;
38  if (valb >= 0) {
39  out.push_back(char((val >> valb) & 0xFF));
40  valb -= 8;
41  }
42  }
43  return out;
44 }
static std::string base64Encode(const std::string &in)
Encodes string to base64.
Definition: Base64.hpp:7
static std::string base64Decode(const std::string &in)
Decodes base64 to string.
Definition: Base64.hpp:26