/src/botan/build/include/public/botan/hex.h
Line | Count | Source |
1 | | /* |
2 | | * Hex Encoding and Decoding |
3 | | * (C) 2010 Jack Lloyd |
4 | | * |
5 | | * Botan is released under the Simplified BSD License (see license.txt) |
6 | | */ |
7 | | |
8 | | #ifndef BOTAN_HEX_CODEC_H_ |
9 | | #define BOTAN_HEX_CODEC_H_ |
10 | | |
11 | | #include <botan/secmem.h> |
12 | | #include <span> |
13 | | #include <string> |
14 | | #include <string_view> |
15 | | |
16 | | namespace Botan { |
17 | | |
18 | | /** |
19 | | * Perform hex encoding |
20 | | * @param output an array of at least input_length*2 bytes |
21 | | * @param input is some binary data |
22 | | * @param input_length length of input in bytes |
23 | | * @param uppercase should output be upper or lower case? |
24 | | */ |
25 | | void BOTAN_PUBLIC_API(2, 0) |
26 | | hex_encode(char output[], const uint8_t input[], size_t input_length, bool uppercase = true); |
27 | | |
28 | | /** |
29 | | * Perform hex encoding |
30 | | * @param input some input |
31 | | * @param input_length length of input in bytes |
32 | | * @param uppercase should output be upper or lower case? |
33 | | * @return hexadecimal representation of input |
34 | | */ |
35 | | std::string BOTAN_PUBLIC_API(2, 0) hex_encode(const uint8_t input[], size_t input_length, bool uppercase = true); |
36 | | |
37 | | /** |
38 | | * Perform hex encoding |
39 | | * @param input some input |
40 | | * @param uppercase should output be upper or lower case? |
41 | | * @return hexadecimal representation of input |
42 | | */ |
43 | 29.1k | inline std::string hex_encode(std::span<const uint8_t> input, bool uppercase = true) { |
44 | 29.1k | return hex_encode(input.data(), input.size(), uppercase); |
45 | 29.1k | } |
46 | | |
47 | | /** |
48 | | * Perform hex decoding |
49 | | * @param output an array of at least input_length/2 bytes |
50 | | * @param input some hex input |
51 | | * @param input_length length of input in bytes |
52 | | * @param input_consumed is an output parameter which says how many |
53 | | * bytes of input were actually consumed. If less than |
54 | | * input_length, then the range input[consumed:length] |
55 | | * should be passed in later along with more input. |
56 | | * @param ignore_ws ignore whitespace on input; if false, throw an |
57 | | exception if whitespace is encountered |
58 | | * @return number of bytes written to output |
59 | | */ |
60 | | size_t BOTAN_PUBLIC_API(2, 0) |
61 | | hex_decode(uint8_t output[], const char input[], size_t input_length, size_t& input_consumed, bool ignore_ws = true); |
62 | | |
63 | | /** |
64 | | * Perform hex decoding |
65 | | * @param output an array of at least input_length/2 bytes |
66 | | * @param input some hex input |
67 | | * @param input_length length of input in bytes |
68 | | * @param ignore_ws ignore whitespace on input; if false, throw an |
69 | | exception if whitespace is encountered |
70 | | * @return number of bytes written to output |
71 | | */ |
72 | | size_t BOTAN_PUBLIC_API(2, 0) |
73 | | hex_decode(uint8_t output[], const char input[], size_t input_length, bool ignore_ws = true); |
74 | | |
75 | | /** |
76 | | * Perform hex decoding |
77 | | * @param output an array of at least input_length/2 bytes |
78 | | * @param input some hex input |
79 | | * @param ignore_ws ignore whitespace on input; if false, throw an |
80 | | exception if whitespace is encountered |
81 | | * @return number of bytes written to output |
82 | | */ |
83 | | size_t BOTAN_PUBLIC_API(3, 0) hex_decode(uint8_t output[], std::string_view input, bool ignore_ws = true); |
84 | | |
85 | | /** |
86 | | * Perform hex decoding |
87 | | * @param output a contiguous byte buffer of at least input_length/2 bytes |
88 | | * @param input some hex input |
89 | | * @param ignore_ws ignore whitespace on input; if false, throw an |
90 | | * exception if whitespace is encountered |
91 | | * @return number of bytes written to output |
92 | | */ |
93 | | size_t BOTAN_PUBLIC_API(3, 0) hex_decode(std::span<uint8_t> output, std::string_view input, bool ignore_ws = true); |
94 | | |
95 | | /** |
96 | | * Perform hex decoding |
97 | | * @param input some hex input |
98 | | * @param input_length the length of input in bytes |
99 | | * @param ignore_ws ignore whitespace on input; if false, throw an |
100 | | exception if whitespace is encountered |
101 | | * @return decoded hex output |
102 | | */ |
103 | | std::vector<uint8_t> BOTAN_PUBLIC_API(2, 0) hex_decode(const char input[], size_t input_length, bool ignore_ws = true); |
104 | | |
105 | | /** |
106 | | * Perform hex decoding |
107 | | * @param input some hex input |
108 | | * @param ignore_ws ignore whitespace on input; if false, throw an |
109 | | exception if whitespace is encountered |
110 | | * @return decoded hex output |
111 | | */ |
112 | | std::vector<uint8_t> BOTAN_PUBLIC_API(3, 0) hex_decode(std::string_view input, bool ignore_ws = true); |
113 | | |
114 | | /** |
115 | | * Perform hex decoding |
116 | | * @param input some hex input |
117 | | * @param input_length the length of input in bytes |
118 | | * @param ignore_ws ignore whitespace on input; if false, throw an |
119 | | exception if whitespace is encountered |
120 | | * @return decoded hex output |
121 | | */ |
122 | | secure_vector<uint8_t> BOTAN_PUBLIC_API(2, 0) |
123 | | hex_decode_locked(const char input[], size_t input_length, bool ignore_ws = true); |
124 | | |
125 | | /** |
126 | | * Perform hex decoding |
127 | | * @param input some hex input |
128 | | * @param ignore_ws ignore whitespace on input; if false, throw an |
129 | | exception if whitespace is encountered |
130 | | * @return decoded hex output |
131 | | */ |
132 | | secure_vector<uint8_t> BOTAN_PUBLIC_API(3, 0) hex_decode_locked(std::string_view input, bool ignore_ws = true); |
133 | | |
134 | | } // namespace Botan |
135 | | |
136 | | #endif |