SNode.C
Loading...
Searching...
No Matches
base64 Namespace Reference

Functions

std::string serverWebSocketKey (const std::string &clientWebSocketKey)
 
static bool is_base64 (char c)
 
std::string base64_encode (const unsigned char *bytes_to_encode, std::size_t length)
 
std::string base64_decode (const std::string &encoded_string)
 

Function Documentation

◆ base64_decode()

std::string base64::base64_decode ( const std::string &  encoded_string)

Definition at line 115 of file base64.cpp.

115 {
116 static const std::string base64_chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
117 "abcdefghijklmnopqrstuvwxyz"
118 "0123456789+/";
119 std::string::size_type in_len = encoded_string.size();
120 std::string::size_type i = 0;
121 std::string::size_type in_ = 0;
122 char char_array_4[4];
123 char char_array_3[3];
124 std::string ret;
125
126 while (in_len-- > 0 && (encoded_string[in_] != '=') && is_base64(encoded_string[in_])) {
127 char_array_4[i++] = encoded_string[in_];
128 in_++;
129 if (i == 4) {
130 for (i = 0; i < 4; i++) {
131 char_array_4[i] = static_cast<char>(base64_chars.find(char_array_4[i]));
132 }
133
134 char_array_3[0] = static_cast<char>((char_array_4[0] << 2) + ((char_array_4[1] & 0x30) >> 4));
135 char_array_3[1] = static_cast<char>(((char_array_4[1] & 0xf) << 4) + ((char_array_4[2] & 0x3c) >> 2));
136 char_array_3[2] = static_cast<char>(((char_array_4[2] & 0x3) << 6) + char_array_4[3]);
137
138 for (i = 0; (i < 3); i++) {
139 ret += char_array_3[i];
140 }
141 i = 0;
142 }
143 }
144
145 if (i != 0) {
146 for (std::string::size_type j = i; j < 4; j++) {
147 char_array_4[j] = 0;
148 }
149
150 for (std::string::size_type j = 0; j < 4; j++) {
151 char_array_4[j] = static_cast<char>(base64_chars.find(char_array_4[j]));
152 }
153
154 char_array_3[0] = static_cast<char>((char_array_4[0] << 2) + ((char_array_4[1] & 0x30) >> 4));
155 char_array_3[1] = static_cast<char>(((char_array_4[1] & 0xf) << 4) + ((char_array_4[2] & 0x3c) >> 2));
156 char_array_3[2] = static_cast<char>(((char_array_4[2] & 0x3) << 6) + char_array_4[3]);
157
158 for (std::string::size_type j = 0; j < i - 1; j++) {
159 ret += char_array_3[j];
160 }
161 }
162
163 return ret;
164 }
static bool is_base64(char c)
Definition base64.cpp:65

References is_base64().

Here is the call graph for this function:

◆ base64_encode()

std::string base64::base64_encode ( const unsigned char *  bytes_to_encode,
std::size_t  length 
)

Definition at line 69 of file base64.cpp.

69 {
70 static const std::string base64_chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
71 "abcdefghijklmnopqrstuvwxyz"
72 "0123456789+/";
73 std::string ret;
74 int i = 0;
75 unsigned char char_array_3[3];
76 unsigned char char_array_4[4];
77
78 while (length-- > 0) {
79 char_array_3[i++] = *(bytes_to_encode++);
80 if (i == 3) {
81 char_array_4[0] = (char_array_3[0] & 0xfc) >> 2;
82 char_array_4[1] = static_cast<unsigned char>(((char_array_3[0] & 0x03) << 4) + ((char_array_3[1] & 0xf0) >> 4));
83 char_array_4[2] = static_cast<unsigned char>(((char_array_3[1] & 0x0f) << 2) + ((char_array_3[2] & 0xc0) >> 6));
84 char_array_4[3] = char_array_3[2] & 0x3f;
85
86 for (i = 0; i < 4; i++) {
87 ret += base64_chars[char_array_4[i]];
88 }
89 i = 0;
90 }
91 }
92
93 if (i != 0) {
94 for (int j = i; j < 3; j++) {
95 char_array_3[j] = '\0';
96 }
97
98 char_array_4[0] = (char_array_3[0] & 0xfc) >> 2;
99 char_array_4[1] = static_cast<unsigned char>(((char_array_3[0] & 0x03) << 4) + ((char_array_3[1] & 0xf0) >> 4));
100 char_array_4[2] = static_cast<unsigned char>(((char_array_3[1] & 0x0f) << 2) + ((char_array_3[2] & 0xc0) >> 6));
101 char_array_4[3] = char_array_3[2] & 0x3f;
102
103 for (int j = 0; j < i + 1; j++) {
104 ret += base64_chars[char_array_4[j]];
105 }
106
107 while (i++ < 3) {
108 ret += '=';
109 }
110 }
111
112 return ret;
113 }

Referenced by express::middleware::BasicAuthentication::BasicAuthentication(), web::websocket::client::SocketContextUpgradeFactory::prepare(), and serverWebSocketKey().

Here is the caller graph for this function:

◆ is_base64()

static bool base64::is_base64 ( char  c)
static

Definition at line 65 of file base64.cpp.

65 {
66 return ((isalnum(c) != 0) || (c == '+') || (c == '/'));
67 }

Referenced by base64_decode().

Here is the caller graph for this function:

◆ serverWebSocketKey()

std::string base64::serverWebSocketKey ( const std::string &  clientWebSocketKey)

Definition at line 56 of file base64.cpp.

56 {
57 const std::string GUID = "258EAFA5-E914-47DA-95CA-C5AB0DC85B11";
58
59 const std::string serverWebSocketKey(clientWebSocketKey + GUID);
60 std::vector<unsigned char> digest = utils::sha1(serverWebSocketKey);
61
62 return base64_encode(digest.data(), digest.size());
63 }
std::string serverWebSocketKey(const std::string &clientWebSocketKey)
Definition base64.cpp:56
std::string base64_encode(const unsigned char *bytes_to_encode, std::size_t length)
Definition base64.cpp:69
std::vector< unsigned char > sha1(const std::string &string)
Definition sha1.cpp:267

References base64_encode().

Referenced by web::websocket::client::SocketContextUpgradeFactory::create(), and web::websocket::server::SocketContextUpgradeFactory::create().

Here is the call graph for this function:
Here is the caller graph for this function: