Kea: encode.h Source File

| Kea 3.0.0

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
20 class BaseNEncoder {
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
106 size_t getBitsPerDigit() {
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
134 size_t getMaxBitsToDigit() {
135 return (max_bits_to_digit_);
136 }
137
141 size_t getMaxDigitToBits() {
142 return (max_digit_to_bits_);
143 }
144
149 bool isCaseSensitive() {
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
168 size_t bits_per_digit_;
169
171 size_t digits_per_group_;
172
174 const char pad_char_;
175
177 size_t max_pad_;
178
180 bool case_sensitive_;
181
183 size_t max_bits_to_digit_;
184
186 size_t max_digit_to_bits_;
187};
188
191 class Base64Encoder : public BaseNEncoder {
192public:
194 static const char* DIGIT_SET;
195
197 static const std::vector<uint8_t> BITS_TABLE;
198
200 Base64Encoder()
201 : BaseNEncoder("base64", DIGIT_SET, BITS_TABLE, 6, 4, '=', 2, true) {
202 }
203
205 ~Base64Encoder() = default;
206};
207
210 class Base32HexEncoder : public BaseNEncoder {
211public:
213 static const char* DIGIT_SET;
214
216 static const std::vector<uint8_t> BITS_TABLE;
217
219 Base32HexEncoder()
220 : BaseNEncoder("base32Hex", DIGIT_SET, BITS_TABLE, 5, 8, '=', 6, false) {
221 }
222
224 ~Base32HexEncoder() = default;
225};
226
229 class Base16Encoder : public BaseNEncoder {
230public:
232 static const char* DIGIT_SET;
233
235 static const std::vector<uint8_t> BITS_TABLE;
236
238 Base16Encoder()
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
293 inline 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
isc::util::encode::Base16Encoder::Base16Encoder
Base16Encoder()
Constructor.
Definition encode.h:238
isc::util::encode::Base16Encoder::~Base16Encoder
~Base16Encoder()=default
Destructor.
isc::util::encode::Base16Encoder::DIGIT_SET
static const char * DIGIT_SET
Set of digits used for encoding in Base16.
Definition encode.h:232
isc::util::encode::Base16Encoder::BITS_TABLE
static const std::vector< uint8_t > BITS_TABLE
Table that maps Base16 digits to their binary data value.
Definition encode.h:235
isc::util::encode::Base32HexEncoder::Base32HexEncoder
Base32HexEncoder()
Constructor.
Definition encode.h:219
isc::util::encode::Base32HexEncoder::DIGIT_SET
static const char * DIGIT_SET
Set of digits used for encoding in Base32Hex.
Definition encode.h:213
isc::util::encode::Base32HexEncoder::BITS_TABLE
static const std::vector< uint8_t > BITS_TABLE
Table that maps Base32Hex digits to their binary data value.
Definition encode.h:216
isc::util::encode::Base32HexEncoder::~Base32HexEncoder
~Base32HexEncoder()=default
Destructor.
isc::util::encode::Base64Encoder::~Base64Encoder
~Base64Encoder()=default
Destructor.
isc::util::encode::Base64Encoder::Base64Encoder
Base64Encoder()
Constructor.
Definition encode.h:200
isc::util::encode::Base64Encoder::BITS_TABLE
static const std::vector< uint8_t > BITS_TABLE
Table that maps Base64 digits to their binary data value.
Definition encode.h:197
isc::util::encode::Base64Encoder::DIGIT_SET
static const char * DIGIT_SET
Set of digits used for encoding in Base64.
Definition encode.h:194
isc::util::encode::BaseNEncoder::getAlgorithm
std::string getAlgorithm() const
Get the algorithm name.
Definition encode.h:85
isc::util::encode::BaseNEncoder::max_bits_to_digit_
size_t max_bits_to_digit_
Maximum index value of the digit set.
Definition encode.h:183
isc::util::encode::BaseNEncoder::digit_set_
const char * digit_set_
Set of digits (i.e. alphabet) used for encoding.
Definition encode.h:158
isc::util::encode::BaseNEncoder::pad_char_
const char pad_char_
Character used for padding out to group size (0 means no padding)
Definition encode.h:174
isc::util::encode::BaseNEncoder::encode
std::string encode(const std::vector< uint8_t > &input)
Encodes binary data using the encoder's algorithm.
Definition encode.cc:67
isc::util::encode::BaseNEncoder::getBitsPerDigit
size_t getBitsPerDigit()
Get the number of data bits represented by a digit.
Definition encode.h:106
isc::util::encode::BaseNEncoder::digits_per_group_
size_t digits_per_group_
Number of digits contained in a group.
Definition encode.h:171
isc::util::encode::BaseNEncoder::decode
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
isc::util::encode::BaseNEncoder::getPadChar
uint8_t getPadChar() const
Get the character used for padding out to group size (0 means no padding)
Definition encode.h:120
isc::util::encode::BaseNEncoder::max_digit_to_bits_
size_t max_digit_to_bits_
Maximum index value of the algorithm bit table.
Definition encode.h:186
isc::util::encode::BaseNEncoder::algorithm_
std::string algorithm_
Name of the algorithm, used for logging.
Definition encode.h:155
isc::util::encode::BaseNEncoder::BaseNEncoder
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
isc::util::encode::BaseNEncoder::isCaseSensitive
bool isCaseSensitive()
Indicates whether or not the algorithm's digit set is case-sensitive.
Definition encode.h:149
isc::util::encode::BaseNEncoder::~BaseNEncoder
virtual ~BaseNEncoder()=default
Destructor.
isc::util::encode::BaseNEncoder::getDigitsPerGroup
size_t getDigitsPerGroup() const
Get the number of digits contained in a group.
Definition encode.h:113
isc::util::encode::BaseNEncoder::case_sensitive_
bool case_sensitive_
Indicates whether or not the algorithm's digit set is case-sensitive.
Definition encode.h:180
isc::util::encode::BaseNEncoder::getDigitSet
const char * getDigitSet() const
Get the digit set.
Definition encode.h:92
isc::util::encode::BaseNEncoder::digitToBits
uint8_t digitToBits(uint8_t digit)
Translate a digit into the appropriate algorithm bit value.
Definition encode.cc:57
isc::util::encode::BaseNEncoder::max_pad_
size_t max_pad_
Maximum number of pad characters in a group.
Definition encode.h:177
isc::util::encode::BaseNEncoder::bits_table_
std::vector< uint8_t > bits_table_
Table to translate digits to data used during decoding.
Definition encode.h:165
isc::util::encode::BaseNEncoder::getMaxPad
size_t getMaxPad()
Get the maximum number of pad characters in a group.
Definition encode.h:127
isc::util::encode::BaseNEncoder::bitsToDigit
char bitsToDigit(uint8_t bits)
Translate a byte of binary data into the appropriate algorithm digit.
Definition encode.cc:47
isc::util::encode::BaseNEncoder::getMaxDigitToBits
size_t getMaxDigitToBits()
Get the maximum index value of the algorithm bit table.
Definition encode.h:141
isc::util::encode::BaseNEncoder::bits_per_digit_
size_t bits_per_digit_
Number of data bits represented by a digit.
Definition encode.h:168
isc::util::encode::BaseNEncoder::getBitsTable
const std::vector< uint8_t > & getBitsTable() const
Get the digit lookup table.
Definition encode.h:99
isc::util::encode::BaseNEncoder::getMaxBitsToDigit
size_t getMaxBitsToDigit()
Get the maximum index value of the digit set.
Definition encode.h:134
isc::util::encode
Definition encode.cc:24
isc::util::encode::encodeBase64
string encodeBase64(const vector< uint8_t > &binary)
Encode binary data in the base64 format.
Definition encode.cc:337
isc::util::encode::decodeBase32Hex
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
isc::util::encode::encodeBase32Hex
string encodeBase32Hex(const vector< uint8_t > &binary)
Encode binary data in the base32-hex format.
Definition encode.cc:349
isc::util::encode::decodeHex
void decodeHex(const string &encoded_str, vector< uint8_t > &output)
Decode a base16 encoded string into binary data.
Definition encode.cc:367
isc::util::encode::encodeHex
string encodeHex(const vector< uint8_t > &binary)
Encode binary data in the base16 format.
Definition encode.cc:361
isc::util::encode::decodeBase64
void decodeBase64(const std::string &encoded_str, std::vector< uint8_t > &output)
Decode a base64 encoded string into binary data.
Definition encode.cc:343
isc::util::encode::toHex
std::string toHex(std::string value)
Encode in hexadecimal inline.
Definition encode.h:293
isc::util
Definition time_utils.cc:43
isc
Defines the logger used by the top-level component of kea-lfc.
Definition agent_parser.cc:148