SNode.C
Loading...
Searching...
No Matches
base64.cpp
Go to the documentation of this file.
1/*
2 * SNode.C - a slim toolkit for network communication
3 * Copyright (C) Volker Christian <me@vchrist.at>
4 * 2020, 2021, 2022, 2023, 2024, 2025
5 *
6 * This program is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU Lesser General Public License as published
8 * by the Free Software Foundation, either version 3 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU Lesser General Public License for more details.
15 *
16 * You should have received a copy of the GNU Lesser General Public License
17 * along with this program. If not, see <http://www.gnu.org/licenses/>.
18 */
19
20#ifndef DOXYGEN_SHOULD_SKIP_THIS
21
22#include "utils/base64.h"
23
24#include "utils/sha1.h"
25
26#include <cctype>
27#include <string>
28#include <vector>
29
30#endif /* DOXYGEN_SHOULD_SKIP_THIS */
31
32namespace base64 {
33
34 std::string serverWebSocketKey(const std::string& clientWebSocketKey) {
35 const std::string GUID = "258EAFA5-E914-47DA-95CA-C5AB0DC85B11";
36
37 const std::string serverWebSocketKey(clientWebSocketKey + GUID);
38 std::vector<unsigned char> digest = utils::sha1(serverWebSocketKey);
39
40 return base64_encode(digest.data(), digest.size());
41 }
42
43 static bool is_base64(char c) {
44 return ((isalnum(c) != 0) || (c == '+') || (c == '/'));
45 }
46
47 std::string base64_encode(const unsigned char* bytes_to_encode, std::size_t length) {
48 static const std::string base64_chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
49 "abcdefghijklmnopqrstuvwxyz"
50 "0123456789+/";
51 std::string ret;
52 int i = 0;
53 unsigned char char_array_3[3];
54 unsigned char char_array_4[4];
55
56 while (length-- > 0) {
57 char_array_3[i++] = *(bytes_to_encode++);
58 if (i == 3) {
59 char_array_4[0] = (char_array_3[0] & 0xfc) >> 2;
60 char_array_4[1] = static_cast<unsigned char>(((char_array_3[0] & 0x03) << 4) + ((char_array_3[1] & 0xf0) >> 4));
61 char_array_4[2] = static_cast<unsigned char>(((char_array_3[1] & 0x0f) << 2) + ((char_array_3[2] & 0xc0) >> 6));
62 char_array_4[3] = char_array_3[2] & 0x3f;
63
64 for (i = 0; i < 4; i++) {
65 ret += base64_chars[char_array_4[i]];
66 }
67 i = 0;
68 }
69 }
70
71 if (i != 0) {
72 for (int j = i; j < 3; j++) {
73 char_array_3[j] = '\0';
74 }
75
76 char_array_4[0] = (char_array_3[0] & 0xfc) >> 2;
77 char_array_4[1] = static_cast<unsigned char>(((char_array_3[0] & 0x03) << 4) + ((char_array_3[1] & 0xf0) >> 4));
78 char_array_4[2] = static_cast<unsigned char>(((char_array_3[1] & 0x0f) << 2) + ((char_array_3[2] & 0xc0) >> 6));
79 char_array_4[3] = char_array_3[2] & 0x3f;
80
81 for (int j = 0; j < i + 1; j++) {
82 ret += base64_chars[char_array_4[j]];
83 }
84
85 while (i++ < 3) {
86 ret += '=';
87 }
88 }
89
90 return ret;
91 }
92
93 std::string base64_decode(const std::string& encoded_string) {
94 static const std::string base64_chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
95 "abcdefghijklmnopqrstuvwxyz"
96 "0123456789+/";
97 std::string::size_type in_len = encoded_string.size();
98 std::string::size_type i = 0;
99 std::string::size_type in_ = 0;
100 char char_array_4[4];
101 char char_array_3[3];
102 std::string ret;
103
104 while (in_len-- > 0 && (encoded_string[in_] != '=') && is_base64(encoded_string[in_])) {
105 char_array_4[i++] = encoded_string[in_];
106 in_++;
107 if (i == 4) {
108 for (i = 0; i < 4; i++) {
109 char_array_4[i] = static_cast<char>(base64_chars.find(char_array_4[i]));
110 }
111
112 char_array_3[0] = static_cast<char>((char_array_4[0] << 2) + ((char_array_4[1] & 0x30) >> 4));
113 char_array_3[1] = static_cast<char>(((char_array_4[1] & 0xf) << 4) + ((char_array_4[2] & 0x3c) >> 2));
114 char_array_3[2] = static_cast<char>(((char_array_4[2] & 0x3) << 6) + char_array_4[3]);
115
116 for (i = 0; (i < 3); i++) {
117 ret += char_array_3[i];
118 }
119 i = 0;
120 }
121 }
122
123 if (i != 0) {
124 for (std::string::size_type j = i; j < 4; j++) {
125 char_array_4[j] = 0;
126 }
127
128 for (std::string::size_type j = 0; j < 4; j++) {
129 char_array_4[j] = static_cast<char>(base64_chars.find(char_array_4[j]));
130 }
131
132 char_array_3[0] = static_cast<char>((char_array_4[0] << 2) + ((char_array_4[1] & 0x30) >> 4));
133 char_array_3[1] = static_cast<char>(((char_array_4[1] & 0xf) << 4) + ((char_array_4[2] & 0x3c) >> 2));
134 char_array_3[2] = static_cast<char>(((char_array_4[2] & 0x3) << 6) + char_array_4[3]);
135
136 for (std::string::size_type j = 0; j < i - 1; j++) {
137 ret += char_array_3[j];
138 }
139 }
140
141 return ret;
142 }
143} // namespace base64
std::string base64_decode(const std::string &encoded_string)
Definition base64.cpp:93
std::string serverWebSocketKey(const std::string &clientWebSocketKey)
Definition base64.cpp:34
static bool is_base64(char c)
Definition base64.cpp:43
std::string base64_encode(const unsigned char *bytes_to_encode, std::size_t length)
Definition base64.cpp:47