base.h 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. #pragma once
  2. #include <string>
  3. #include <array>
  4. namespace jwt {
  5. namespace alphabet {
  6. struct base64 {
  7. static const std::array<char, 64>& data() {
  8. static std::array<char, 64> data = {
  9. {'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P',
  10. 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', 'a', 'b', 'c', 'd', 'e', 'f',
  11. 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v',
  12. 'w', 'x', 'y', 'z', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '+', '/'}};
  13. return data;
  14. };
  15. static const std::string& fill() {
  16. static std::string fill = "=";
  17. return fill;
  18. }
  19. };
  20. struct base64url {
  21. static const std::array<char, 64>& data() {
  22. static std::array<char, 64> data = {
  23. {'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P',
  24. 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', 'a', 'b', 'c', 'd', 'e', 'f',
  25. 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v',
  26. 'w', 'x', 'y', 'z', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '-', '_'}};
  27. return data;
  28. };
  29. static const std::string& fill() {
  30. static std::string fill = "%3d";
  31. return fill;
  32. }
  33. };
  34. }
  35. class base {
  36. public:
  37. template<typename T>
  38. static std::string encode(const std::string& bin) {
  39. return encode(bin, T::data(), T::fill());
  40. }
  41. template<typename T>
  42. static std::string decode(const std::string& base) {
  43. return decode(base, T::data(), T::fill());
  44. }
  45. private:
  46. static std::string encode(const std::string& bin, const std::array<char, 64>& alphabet, const std::string& fill) {
  47. size_t size = bin.size();
  48. std::string res;
  49. // clear incomplete bytes
  50. size_t fast_size = size - size % 3;
  51. for (size_t i = 0; i < fast_size;) {
  52. uint32_t octet_a = (unsigned char)bin[i++];
  53. uint32_t octet_b = (unsigned char)bin[i++];
  54. uint32_t octet_c = (unsigned char)bin[i++];
  55. uint32_t triple = (octet_a << 0x10) + (octet_b << 0x08) + octet_c;
  56. res += alphabet[(triple >> 3 * 6) & 0x3F];
  57. res += alphabet[(triple >> 2 * 6) & 0x3F];
  58. res += alphabet[(triple >> 1 * 6) & 0x3F];
  59. res += alphabet[(triple >> 0 * 6) & 0x3F];
  60. }
  61. if (fast_size == size)
  62. return res;
  63. size_t mod = size % 3;
  64. uint32_t octet_a = fast_size < size ? (unsigned char)bin[fast_size++] : 0;
  65. uint32_t octet_b = fast_size < size ? (unsigned char)bin[fast_size++] : 0;
  66. uint32_t octet_c = fast_size < size ? (unsigned char)bin[fast_size++] : 0;
  67. uint32_t triple = (octet_a << 0x10) + (octet_b << 0x08) + octet_c;
  68. switch (mod) {
  69. case 1:
  70. res += alphabet[(triple >> 3 * 6) & 0x3F];
  71. res += alphabet[(triple >> 2 * 6) & 0x3F];
  72. res += fill;
  73. res += fill;
  74. break;
  75. case 2:
  76. res += alphabet[(triple >> 3 * 6) & 0x3F];
  77. res += alphabet[(triple >> 2 * 6) & 0x3F];
  78. res += alphabet[(triple >> 1 * 6) & 0x3F];
  79. res += fill;
  80. break;
  81. default:
  82. break;
  83. }
  84. return res;
  85. }
  86. static std::string decode(const std::string& base, const std::array<char, 64>& alphabet, const std::string& fill) {
  87. size_t size = base.size();
  88. size_t fill_cnt = 0;
  89. while (size > fill.size()) {
  90. if (base.substr(size - fill.size(), fill.size()) == fill) {
  91. fill_cnt++;
  92. size -= fill.size();
  93. if(fill_cnt > 2)
  94. throw std::runtime_error("Invalid input");
  95. }
  96. else break;
  97. }
  98. if ((size + fill_cnt) % 4 != 0)
  99. throw std::runtime_error("Invalid input");
  100. size_t out_size = size / 4 * 3;
  101. std::string res;
  102. res.reserve(out_size);
  103. auto get_sextet = [&](size_t offset) {
  104. for (size_t i = 0; i < alphabet.size(); i++) {
  105. if (alphabet[i] == base[offset])
  106. return i;
  107. }
  108. throw std::runtime_error("Invalid input");
  109. };
  110. size_t fast_size = size - size % 4;
  111. for (size_t i = 0; i < fast_size;) {
  112. uint32_t sextet_a = get_sextet(i++);
  113. uint32_t sextet_b = get_sextet(i++);
  114. uint32_t sextet_c = get_sextet(i++);
  115. uint32_t sextet_d = get_sextet(i++);
  116. uint32_t triple = (sextet_a << 3 * 6)
  117. + (sextet_b << 2 * 6)
  118. + (sextet_c << 1 * 6)
  119. + (sextet_d << 0 * 6);
  120. res += (triple >> 2 * 8) & 0xFF;
  121. res += (triple >> 1 * 8) & 0xFF;
  122. res += (triple >> 0 * 8) & 0xFF;
  123. }
  124. if (fill_cnt == 0)
  125. return res;
  126. uint32_t triple = (get_sextet(fast_size) << 3 * 6)
  127. + (get_sextet(fast_size + 1) << 2 * 6);
  128. switch (fill_cnt) {
  129. case 1:
  130. triple |= (get_sextet(fast_size + 2) << 1 * 6);
  131. res += (triple >> 2 * 8) & 0xFF;
  132. res += (triple >> 1 * 8) & 0xFF;
  133. break;
  134. case 2:
  135. res += (triple >> 2 * 8) & 0xFF;
  136. break;
  137. default:
  138. break;
  139. }
  140. return res;
  141. }
  142. };
  143. }