std::string chars =
33 "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
40std::string base64_encode(
conststd::string &input) {
41std::string base64_string;
44 for(uint32_t i = 0; i < input.size(); i += 3) {
45 charfirst_byte = input[i];
48base64_string.push_back(chars[first_byte >> 2]);
50 if(i + 1 < input.size()) {
51 charsecond_byte = input[i + 1];
56base64_string.push_back(
57chars[(((first_byte & 3) << 4) | ((second_byte & 0xF0) >> 4))]);
59 if(i + 2 < input.size()) {
60 charthird_byte = input[i + 2];
65base64_string.push_back(chars[((third_byte & 0xC0) >> 6) |
66((second_byte & 0x0F) << 2)]);
68base64_string.push_back(chars[(third_byte & 0x3F)]);
71base64_string.push_back(chars[((second_byte & 0x0F) << 2)]);
72base64_string.push_back(
'=');
76base64_string.push_back(chars[((first_byte & 3) << 4)]);
77base64_string.push_back(
'=');
78base64_string.push_back(
'=');
90uint8_t find_idx(
const charc) {
91 if(c >=
'A'&& c <=
'Z') {
93}
else if(c >=
'a'&& c <=
'z') {
95}
else if(c >=
'0'&& c <=
'9') {
97}
else if(c ==
'+') {
99}
else if(c ==
'/') {
110std::string base64_decode(
conststd::string &base64_str) {
113 for(uint32_t i = 0; i < base64_str.size(); i += 4) {
115 charfirst_byte = base64_str[i];
117 charsecond_byte = base64_str[i + 1];
121 charfirst_actual_byte =
static_cast<char>(
122(find_idx(first_byte) << 2) | ((find_idx(second_byte)) >> 4));
123base64_decoded.push_back(first_actual_byte);
124 if(i + 2 < base64_str.size() && base64_str[i + 2] !=
'=') {
126 charthird_byte = base64_str[i + 2];
130 charsecond_actual_byte =
131 static_cast<char>(((find_idx(second_byte) & 0x0F) << 4) |
132(find_idx(third_byte) >> 2));
133base64_decoded.push_back(second_actual_byte);
135 if(i + 3 < base64_str.size() && base64_str[i + 3] !=
'=') {
137 charfourth_byte = base64_str[i + 3];
140 charthird_actual_byte =
141 static_cast<char>(((find_idx(third_byte) & 0x03) << 6) |
142find_idx(fourth_byte));
143base64_decoded.push_back(third_actual_byte);
147 returnbase64_decoded;
159 "To err is human, but to really foul things up you need a computer.";
160std::string base64_str = ciphers::base64_encoding::base64_encode(str);
162 "VG8gZXJyIGlzIGh1bWFuLCBidXQgdG8gcmVhbGx5IGZvdWwgdGhpbmdzIHVwIHlvdSBuZW" 163 "VkIGEgY29tcHV0ZXIu";
165assert(base64_str == verify);
166std::string original_str =
167ciphers::base64_encoding::base64_decode(base64_str);
169assert(original_str == str);
173 "Man is distinguished, not only by his reason, but by this singular " 174 "passion from other animals, which is a lust of the mind, that by a " 175 "perseverance of delight in the continued and indefatigable generation " 176 "of knowledge, exceeds the short vehemence of any carnal pleasure.";
178base64_str = ciphers::base64_encoding::base64_encode(str);
180 "TWFuIGlzIGRpc3Rpbmd1aXNoZWQsIG5vdCBvbmx5IGJ5IGhpcyByZWFzb24sIGJ1dCBieS" 181 "B0aGlzIHNpbmd1bGFyIHBhc3Npb24gZnJvbSBvdGhlciBhbmltYWxzLCB3aGljaCBpcyBh" 182 "IGx1c3Qgb2YgdGhlIG1pbmQsIHRoYXQgYnkgYSBwZXJzZXZlcmFuY2Ugb2YgZGVsaWdodC" 183 "BpbiB0aGUgY29udGludWVkIGFuZCBpbmRlZmF0aWdhYmxlIGdlbmVyYXRpb24gb2Yga25v" 184 "d2xlZGdlLCBleGNlZWRzIHRoZSBzaG9ydCB2ZWhlbWVuY2Ugb2YgYW55IGNhcm5hbCBwbG" 187assert(base64_str == verify);
188original_str = ciphers::base64_encoding::base64_decode(base64_str);
190assert(original_str == str);
static void test()
Self-test implementations.
int main()
Main function.
Functions for Base64 Encoding and Decoding implementation.
Algorithms for encryption and decryption.
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4