-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathbase64.hpp
More file actions
62 lines (49 loc) · 1.65 KB
/
base64.hpp
File metadata and controls
62 lines (49 loc) · 1.65 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
/*
* base64.hpp
*
* Created on: Jan 15, 2016
* Author: rs
*/
#ifndef INCLUDE_FLEXIBITY_BASE64_HPP_
#define INCLUDE_FLEXIBITY_BASE64_HPP_
#include <boost/archive/iterators/binary_from_base64.hpp>
#include <boost/archive/iterators/insert_linebreaks.hpp>
#include <boost/archive/iterators/base64_from_binary.hpp>
#include <boost/archive/iterators/transform_width.hpp>
#include "flexibity/log.h"
namespace Flexibity{
using namespace boost::archive::iterators;
using namespace std;
class base64{
public:
typedef base64_from_binary< // convert binary values to base64 characters
transform_width<string::const_iterator, 6, 8>> // retrieve 6 bit integers from a sequence of 8 bit bytes
toBase64Iterator; // compose all the above operations in to a new iterator
typedef transform_width<binary_from_base64<string::const_iterator>, 8, 6> fromBase64Iterator;
static string encode(string data){
// Pad with 0 until a multiple of 3
unsigned int paddedCharacters = 0;
while(data.size() % 3 != 0){
paddedCharacters++;
data.push_back(0);
}
//GINFO("Padded " << paddedCharacters);
std::string encodedString(toBase64Iterator(data.begin()), toBase64Iterator(data.end() - paddedCharacters));
// Add '=' for each padded character used
while(paddedCharacters > 0){
encodedString.push_back('=');
paddedCharacters--;
}
//GINFO("encoded to " << encodedString);
return encodedString;
}
static string decode(string data){
size_t pos;
while ((pos = data.rfind("=")) != string::npos){
data.erase(pos);
}
return string(fromBase64Iterator(data.begin()), fromBase64Iterator(data.end()));
}
};
}
#endif /* INCLUDE_FLEXIBITY_BASE64_HPP_ */