FlyFar

tests/base64.h

Aug 27th, 2023
219
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.93 KB | Cybersecurity | 0 0
  1. #ifndef BASE64
  2. #define BASE64
  3.  
  4. #include <string>
  5. /*
  6. BASE 64 Encoding and Decoding
  7. Original : https://renenyffenegger.ch/notes/development/Base64/Encoding-and-decoding-base-64-with-cpp
  8. */
  9. static const std::string base64_chars =
  10.              "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
  11.              "abcdefghijklmnopqrstuvwxyz"
  12.              "0123456789+/";
  13.  
  14.  
  15. static inline bool is_base64(unsigned char c) {
  16.   return (isalnum(c) || (c == '+') || (c == '/'));
  17. }
  18.  
  19. std::string base64_encode(unsigned char const* bytes_to_encode, unsigned int in_len) {
  20.   std::string ret;
  21.   int i = 0;
  22.   int j = 0;
  23.   unsigned char char_array_3[3];
  24.   unsigned char char_array_4[4];
  25.  
  26.   while (in_len--) {
  27.     char_array_3[i++] = *(bytes_to_encode++);
  28.     if (i == 3) {
  29.       char_array_4[0] = (char_array_3[0] & 0xfc) >> 2;
  30.       char_array_4[1] = ((char_array_3[0] & 0x03) << 4) + ((char_array_3[1] & 0xf0) >> 4);
  31.       char_array_4[2] = ((char_array_3[1] & 0x0f) << 2) + ((char_array_3[2] & 0xc0) >> 6);
  32.       char_array_4[3] = char_array_3[2] & 0x3f;
  33.  
  34.       for(i = 0; (i <4) ; i++)
  35.         ret += base64_chars[char_array_4[i]];
  36.       i = 0;
  37.     }
  38.   }
  39.  
  40.   if (i)
  41.   {
  42.     for(j = i; j < 3; j++)
  43.       char_array_3[j] = '\0';
  44.  
  45.     char_array_4[0] = (char_array_3[0] & 0xfc) >> 2;
  46.     char_array_4[1] = ((char_array_3[0] & 0x03) << 4) + ((char_array_3[1] & 0xf0) >> 4);
  47.     char_array_4[2] = ((char_array_3[1] & 0x0f) << 2) + ((char_array_3[2] & 0xc0) >> 6);
  48.     char_array_4[3] = char_array_3[2] & 0x3f;
  49.  
  50.     for (j = 0; (j < i + 1); j++)
  51.       ret += base64_chars[char_array_4[j]];
  52.  
  53.     while((i++ < 3))
  54.       ret += '=';
  55.  
  56.   }
  57.  
  58.   return ret;
  59.  
  60. }
  61. std::string base64_decode(std::string const& encoded_string) {
  62.   int in_len = encoded_string.size();
  63.   int i = 0;
  64.   int j = 0;
  65.   int in_ = 0;
  66.   unsigned char char_array_4[4], char_array_3[3];
  67.   std::string ret;
  68.  
  69.   while (in_len-- && ( encoded_string[in_] != '=') && is_base64(encoded_string[in_])) {
  70.     char_array_4[i++] = encoded_string[in_]; in_++;
  71.     if (i ==4) {
  72.       for (i = 0; i <4; i++)
  73.         char_array_4[i] = base64_chars.find(char_array_4[i]);
  74.  
  75.       char_array_3[0] = (char_array_4[0] << 2) + ((char_array_4[1] & 0x30) >> 4);
  76.       char_array_3[1] = ((char_array_4[1] & 0xf) << 4) + ((char_array_4[2] & 0x3c) >> 2);
  77.       char_array_3[2] = ((char_array_4[2] & 0x3) << 6) + char_array_4[3];
  78.  
  79.       for (i = 0; (i < 3); i++)
  80.         ret += char_array_3[i];
  81.       i = 0;
  82.     }
  83.   }
  84.  
  85.   if (i) {
  86.     for (j = i; j <4; j++)
  87.       char_array_4[j] = 0;
  88.  
  89.     for (j = 0; j <4; j++)
  90.       char_array_4[j] = base64_chars.find(char_array_4[j]);
  91.  
  92.     char_array_3[0] = (char_array_4[0] << 2) + ((char_array_4[1] & 0x30) >> 4);
  93.     char_array_3[1] = ((char_array_4[1] & 0xf) << 4) + ((char_array_4[2] & 0x3c) >> 2);
  94.     char_array_3[2] = ((char_array_4[2] & 0x3) << 6) + char_array_4[3];
  95.  
  96.     for (j = 0; (j < i - 1); j++) ret += char_array_3[j];
  97.   }
  98.  
  99.   return ret;
  100. }
  101.  
  102. #endif // ! BASE64
Tags: base64 encode
Add Comment
Please, Sign In to add comment