Kea 2.7.6
encode.h
Go to the documentation of this file.
1// Copyright (C) 2024 Internet Systems Consortium, Inc. ("ISC")
2//
3// This Source Code Form is subject to the terms of the Mozilla Public
4// License, v. 2.0. If a copy of the MPL was not distributed with this
5// file, You can obtain one at http://mozilla.org/MPL/2.0/.
6
7#ifndef ENCODE_H
8#define ENCODE_H
9
10#include <stdint.h>
11#include <string>
12#include <vector>
13
14namespace isc {
15namespace util {
16namespace encode {
17
21public:
22
35 BaseNEncoder(const std::string& algorithm,
36 const char* digit_set,
37 const std::vector<uint8_t>& bits_table,
38 size_t bits_per_digit,
39 size_t digits_per_group,
40 const char pad_char,
41 size_t max_pad,
42 bool case_sensitive);
43
45 virtual ~BaseNEncoder() = default;
46
53 std::string encode(const std::vector<uint8_t>& input);
54
61 void decode(const std::string& encoded_str, std::vector<uint8_t>& output);
62
69 char bitsToDigit(uint8_t bits);
70
80 uint8_t digitToBits(uint8_t digit);
81
85 std::string getAlgorithm() const {
86 return (algorithm_);
87 }
88
92 const char* getDigitSet() const {
93 return (digit_set_);
94 }
95
99 const std::vector<uint8_t>& getBitsTable() const {
100 return (bits_table_);
101 }
102
107 return (bits_per_digit_);
108 }
109
113 size_t getDigitsPerGroup() const {
114 return (digits_per_group_);
115 }
116
120 uint8_t getPadChar() const {
121 return (pad_char_);
122 }
123
127 size_t getMaxPad() {
128 return (max_pad_);
129 }
130
135 return (max_bits_to_digit_);
136 }
137
142 return (max_digit_to_bits_);
143 }
144
150 return (case_sensitive_);
151 }
152
153protected:
155 std::string algorithm_;
156
158 const char* digit_set_;
159
165 std::vector<uint8_t>bits_table_;
166
169
172
174 const char pad_char_;
175
177 size_t max_pad_;
178
181
184
187};
188
192public:
194 static const char* DIGIT_SET;
195
197 static const std::vector<uint8_t> BITS_TABLE;
198
201 : BaseNEncoder("base64", DIGIT_SET, BITS_TABLE, 6, 4, '=', 2, true) {
202 }
203
205 ~Base64Encoder() = default;
206};
207
211public:
213 static const char* DIGIT_SET;
214
216 static const std::vector<uint8_t> BITS_TABLE;
217
220 : BaseNEncoder("base32Hex", DIGIT_SET, BITS_TABLE, 5, 8, '=', 6, false) {
221 }
222
224 ~Base32HexEncoder() = default;
225};
226
230public:
232 static const char* DIGIT_SET;
233
235 static const std::vector<uint8_t> BITS_TABLE;
236
239 : BaseNEncoder("base16", DIGIT_SET, BITS_TABLE, 4, 2, '=', 0, false) {
240 }
241
243 ~Base16Encoder() = default;
244};
245
250std::string encodeBase32Hex(const std::vector<uint8_t>& binary);
251
258void decodeBase32Hex(const std::string& encoded_str, std::vector<uint8_t>& output);
259
264std::string encodeBase64(const std::vector<uint8_t>& binary);
265
272void decodeBase64(const std::string& encoded_str, std::vector<uint8_t>& output);
273
278std::string encodeHex(const std::vector<uint8_t>& binary);
279
286void decodeHex(const std::string& encoded_str, std::vector<uint8_t>& output);
287
293inline std::string toHex(std::string value) {
294 std::vector<uint8_t> bin(value.begin(), value.end());
295 return ("0x" + encodeHex(bin));
296}
297
298} // namespace encode
299} // namespace util
300} // namespace isc
301
302#endif // ENCODE_H
Class for encoding and decoding binary data using Base16 (aka Hex) as described in RFC 4648.
Definition encode.h:229
~Base16Encoder()=default
Destructor.
static const char * DIGIT_SET
Set of digits used for encoding in Base16.
Definition encode.h:232
static const std::vector< uint8_t > BITS_TABLE
Table that maps Base16 digits to their binary data value.
Definition encode.h:235
Class for encoding and decoding binary data using Base32Hex as described in RFC 4648.
Definition encode.h:210
static const char * DIGIT_SET
Set of digits used for encoding in Base32Hex.
Definition encode.h:213
static const std::vector< uint8_t > BITS_TABLE
Table that maps Base32Hex digits to their binary data value.
Definition encode.h:216
~Base32HexEncoder()=default
Destructor.
Class for encoding and decoding binary data using Base64 as described in RFC 4648.
Definition encode.h:191
~Base64Encoder()=default
Destructor.
static const std::vector< uint8_t > BITS_TABLE
Table that maps Base64 digits to their binary data value.
Definition encode.h:197
static const char * DIGIT_SET
Set of digits used for encoding in Base64.
Definition encode.h:194
Class for encoding and decoding binary data using an algorithm described in RFC 4648.
Definition encode.h:20
std::string getAlgorithm() const
Get the algorithm name.
Definition encode.h:85
size_t max_bits_to_digit_
Maximum index value of the digit set.
Definition encode.h:183
const char * digit_set_
Set of digits (i.e. alphabet) used for encoding.
Definition encode.h:158
const char pad_char_
Character used for padding out to group size (0 means no padding)
Definition encode.h:174
std::string encode(const std::vector< uint8_t > &input)
Encodes binary data using the encoder's algorithm.
Definition encode.cc:67
size_t getBitsPerDigit()
Get the number of data bits represented by a digit.
Definition encode.h:106
size_t digits_per_group_
Number of digits contained in a group.
Definition encode.h:171
void decode(const std::string &encoded_str, std::vector< uint8_t > &output)
Decodes an encoded string using the encoder's algorithm.
Definition encode.cc:150
uint8_t getPadChar() const
Get the character used for padding out to group size (0 means no padding)
Definition encode.h:120
size_t max_digit_to_bits_
Maximum index value of the algorithm bit table.
Definition encode.h:186
std::string algorithm_
Name of the algorithm, used for logging.
Definition encode.h:155
BaseNEncoder(const std::string &algorithm, const char *digit_set, const std::vector< uint8_t > &bits_table, size_t bits_per_digit, size_t digits_per_group, const char pad_char, size_t max_pad, bool case_sensitive)
Constructor.
Definition encode.cc:26
bool isCaseSensitive()
Indicates whether or not the algorithm's digit set is case-sensitive.
Definition encode.h:149
virtual ~BaseNEncoder()=default
Destructor.
size_t getDigitsPerGroup() const
Get the number of digits contained in a group.
Definition encode.h:113
bool case_sensitive_
Indicates whether or not the algorithm's digit set is case-sensitive.
Definition encode.h:180
const char * getDigitSet() const
Get the digit set.
Definition encode.h:92
uint8_t digitToBits(uint8_t digit)
Translate a digit into the appropriate algorithm bit value.
Definition encode.cc:57
size_t max_pad_
Maximum number of pad characters in a group.
Definition encode.h:177
std::vector< uint8_t > bits_table_
Table to translate digits to data used during decoding.
Definition encode.h:165
size_t getMaxPad()
Get the maximum number of pad characters in a group.
Definition encode.h:127
char bitsToDigit(uint8_t bits)
Translate a byte of binary data into the appropriate algorithm digit.
Definition encode.cc:47
size_t getMaxDigitToBits()
Get the maximum index value of the algorithm bit table.
Definition encode.h:141
size_t bits_per_digit_
Number of data bits represented by a digit.
Definition encode.h:168
const std::vector< uint8_t > & getBitsTable() const
Get the digit lookup table.
Definition encode.h:99
size_t getMaxBitsToDigit()
Get the maximum index value of the digit set.
Definition encode.h:134
string encodeBase64(const vector< uint8_t > &binary)
Encode binary data in the base64 format.
Definition encode.cc:337
void decodeBase32Hex(const std::string &encoded_str, std::vector< uint8_t > &output)
Decode a base32-hex encoded string into binary data.
Definition encode.cc:355
string encodeBase32Hex(const vector< uint8_t > &binary)
Encode binary data in the base32-hex format.
Definition encode.cc:349
void decodeHex(const string &encoded_str, vector< uint8_t > &output)
Decode a base16 encoded string into binary data.
Definition encode.cc:367
string encodeHex(const vector< uint8_t > &binary)
Encode binary data in the base16 format.
Definition encode.cc:361
void decodeBase64(const std::string &encoded_str, std::vector< uint8_t > &output)
Decode a base64 encoded string into binary data.
Definition encode.cc:343
std::string toHex(std::string value)
Encode in hexadecimal inline.
Definition encode.h:293
Defines the logger used by the top-level component of kea-lfc.