33const std::string chars =
34 "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
41std::string base64_encode(
const std::string &input) {
42 std::string base64_string;
45 for (uint32_t i = 0; i < input.size(); i += 3) {
46 char first_byte = input[i];
49 base64_string.push_back(chars[first_byte >> 2]);
51 if (i + 1 < input.size()) {
52 char second_byte = input[i + 1];
57 base64_string.push_back(
58 chars[(((first_byte & 3) << 4) | ((second_byte & 0xF0) >> 4))]);
60 if (i + 2 < input.size()) {
61 char third_byte = input[i + 2];
66 base64_string.push_back(chars[((third_byte & 0xC0) >> 6) |
67 ((second_byte & 0x0F) << 2)]);
69 base64_string.push_back(chars[(third_byte & 0x3F)]);
72 base64_string.push_back(chars[((second_byte & 0x0F) << 2)]);
73 base64_string.push_back(
'=');
77 base64_string.push_back(chars[((first_byte & 3) << 4)]);
78 base64_string.push_back(
'=');
79 base64_string.push_back(
'=');
91uint8_t find_idx(
const char c) {
92 if (c >=
'A' && c <=
'Z') {
94 }
else if (c >=
'a' && c <=
'z') {
96 }
else if (c >=
'0' && c <=
'9') {
98 }
else if (c ==
'+') {
100 }
else if (c ==
'/') {
111std::string base64_decode(
const std::string &base64_str) {
114 for (uint32_t i = 0; i < base64_str.size(); i += 4) {
116 char first_byte = base64_str[i];
118 char second_byte = base64_str[i + 1];
122 char first_actual_byte =
static_cast<char>(
123 (find_idx(first_byte) << 2) | ((find_idx(second_byte)) >> 4));
124 base64_decoded.push_back(first_actual_byte);
125 if (i + 2 < base64_str.size() && base64_str[i + 2] !=
'=') {
127 char third_byte = base64_str[i + 2];
131 char second_actual_byte =
132 static_cast<char>(((find_idx(second_byte) & 0x0F) << 4) |
133 (find_idx(third_byte) >> 2));
134 base64_decoded.push_back(second_actual_byte);
136 if (i + 3 < base64_str.size() && base64_str[i + 3] !=
'=') {
138 char fourth_byte = base64_str[i + 3];
141 char third_actual_byte =
142 static_cast<char>(((find_idx(third_byte) & 0x03) << 6) |
143 find_idx(fourth_byte));
144 base64_decoded.push_back(third_actual_byte);
148 return base64_decoded;
160 "To err is human, but to really foul things up you need a computer.";
161 std::string base64_str = ciphers::base64_encoding::base64_encode(str);
163 "VG8gZXJyIGlzIGh1bWFuLCBidXQgdG8gcmVhbGx5IGZvdWwgdGhpbmdzIHVwIHlvdSBuZW"
164 "VkIGEgY29tcHV0ZXIu";
166 assert(base64_str == verify);
167 std::string original_str =
168 ciphers::base64_encoding::base64_decode(base64_str);
170 assert(original_str == str);
174 "Man is distinguished, not only by his reason, but by this singular "
175 "passion from other animals, which is a lust of the mind, that by a "
176 "perseverance of delight in the continued and indefatigable generation "
177 "of knowledge, exceeds the short vehemence of any carnal pleasure.";
179 base64_str = ciphers::base64_encoding::base64_encode(str);
181 "TWFuIGlzIGRpc3Rpbmd1aXNoZWQsIG5vdCBvbmx5IGJ5IGhpcyByZWFzb24sIGJ1dCBieS"
182 "B0aGlzIHNpbmd1bGFyIHBhc3Npb24gZnJvbSBvdGhlciBhbmltYWxzLCB3aGljaCBpcyBh"
183 "IGx1c3Qgb2YgdGhlIG1pbmQsIHRoYXQgYnkgYSBwZXJzZXZlcmFuY2Ugb2YgZGVsaWdodC"
184 "BpbiB0aGUgY29udGludWVkIGFuZCBpbmRlZmF0aWdhYmxlIGdlbmVyYXRpb24gb2Yga25v"
185 "d2xlZGdlLCBleGNlZWRzIHRoZSBzaG9ydCB2ZWhlbWVuY2Ugb2YgYW55IGNhcm5hbCBwbG"
188 assert(base64_str == verify);
189 original_str = ciphers::base64_encoding::base64_decode(base64_str);
191 assert(original_str == str);
Functions for Base64 Encoding and Decoding implementation.
Algorithms for encryption and decryption.