summaryrefslogtreecommitdiffstats
path: root/comm/third_party/botan/src/cli/codec.cpp
blob: 48388d1a7a967753f1816c6ed6d46e42e8926f0b (plain)
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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
/*
* (C) 2019 Jack Lloyd
*
* Botan is released under the Simplified BSD License (see license.txt)
*/

#include "cli.h"

#if defined(BOTAN_HAS_HEX_CODEC)
   #include <botan/hex.h>
#endif

#if defined(BOTAN_HAS_BASE32_CODEC)
   #include <botan/base32.h>
#endif

#if defined(BOTAN_HAS_BASE58_CODEC)
   #include <botan/base58.h>
#endif

#if defined(BOTAN_HAS_BASE64_CODEC)
   #include <botan/base64.h>
#endif

namespace Botan_CLI {

#if defined(BOTAN_HAS_HEX_CODEC)

class Hex_Encode final : public Command
   {
   public:
      Hex_Encode() : Command("hex_enc file") {}

      std::string group() const override
         {
         return "codec";
         }

      std::string description() const override
         {
         return "Hex encode a given file";
         }

      void go() override
         {
         auto hex_enc_f = [&](const uint8_t b[], size_t l) { output() << Botan::hex_encode(b, l); };
         this->read_file(get_arg("file"), hex_enc_f, 2);
         }
   };

BOTAN_REGISTER_COMMAND("hex_enc", Hex_Encode);

class Hex_Decode final : public Command
   {
   public:
      Hex_Decode() : Command("hex_dec file") {}

      std::string group() const override
         {
         return "codec";
         }

      std::string description() const override
         {
         return "Hex decode a given file";
         }

      void go() override
         {
         auto hex_dec_f = [&](const uint8_t b[], size_t l)
            {
            std::vector<uint8_t> bin = Botan::hex_decode(reinterpret_cast<const char*>(b), l);
            output().write(reinterpret_cast<const char*>(bin.data()), bin.size());
            };

         this->read_file(get_arg("file"), hex_dec_f, 2);
         }
   };

BOTAN_REGISTER_COMMAND("hex_dec", Hex_Decode);

#endif

#if defined(BOTAN_HAS_BASE58_CODEC)

class Base58_Encode final : public Command
   {
   public:
      Base58_Encode() : Command("base58_enc --check file") {}

      std::string group() const override
         {
         return "codec";
         }

      std::string description() const override
         {
         return "Encode given file to Base58";
         }

      void go() override
         {
         auto data = slurp_file(get_arg("file"));

         if(flag_set("check"))
            output() << Botan::base58_check_encode(data);
         else
            output() << Botan::base58_encode(data);
         }
   };

BOTAN_REGISTER_COMMAND("base58_enc", Base58_Encode);

class Base58_Decode final : public Command
   {
   public:
      Base58_Decode() : Command("base58_dec --check file") {}

      std::string group() const override
         {
         return "codec";
         }

      std::string description() const override
         {
         return "Decode Base58 encoded file";
         }

      void go() override
         {
         auto data = slurp_file_as_str(get_arg("file"));

         std::vector<uint8_t> bin;

         if(flag_set("check"))
            bin = Botan::base58_check_decode(data);
         else
            bin = Botan::base58_decode(data);

         output().write(reinterpret_cast<const char*>(bin.data()), bin.size());
         }
   };

BOTAN_REGISTER_COMMAND("base58_dec", Base58_Decode);

#endif // base58

#if defined(BOTAN_HAS_BASE32_CODEC)

class Base32_Encode final : public Command
   {
   public:
      Base32_Encode() : Command("base32_enc file") {}

      std::string group() const override
         {
         return "codec";
         }

      std::string description() const override
         {
         return "Encode given file to Base32";
         }

      void go() override
         {
         auto onData = [&](const uint8_t b[], size_t l)
            {
            output() << Botan::base32_encode(b, l);
            };
         this->read_file(get_arg("file"), onData, 768);
         }
   };

BOTAN_REGISTER_COMMAND("base32_enc", Base32_Encode);

class Base32_Decode final : public Command
   {
   public:
      Base32_Decode() : Command("base32_dec file") {}

      std::string group() const override
         {
         return "codec";
         }

      std::string description() const override
         {
         return "Decode Base32 encoded file";
         }

      void go() override
         {
         auto write_bin = [&](const uint8_t b[], size_t l)
            {
            Botan::secure_vector<uint8_t> bin = Botan::base32_decode(reinterpret_cast<const char*>(b), l);
            output().write(reinterpret_cast<const char*>(bin.data()), bin.size());
            };

         this->read_file(get_arg("file"), write_bin, 1024);
         }
   };

BOTAN_REGISTER_COMMAND("base32_dec", Base32_Decode);

#endif // base32

#if defined(BOTAN_HAS_BASE64_CODEC)

class Base64_Encode final : public Command
   {
   public:
      Base64_Encode() : Command("base64_enc file") {}

      std::string group() const override
         {
         return "codec";
         }

      std::string description() const override
         {
         return "Encode given file to Base64";
         }

      void go() override
         {
         auto onData = [&](const uint8_t b[], size_t l)
            {
            output() << Botan::base64_encode(b, l);
            };
         this->read_file(get_arg("file"), onData, 768);
         }
   };

BOTAN_REGISTER_COMMAND("base64_enc", Base64_Encode);

class Base64_Decode final : public Command
   {
   public:
      Base64_Decode() : Command("base64_dec file") {}

      std::string group() const override
         {
         return "codec";
         }

      std::string description() const override
         {
         return "Decode Base64 encoded file";
         }

      void go() override
         {
         auto write_bin = [&](const uint8_t b[], size_t l)
            {
            Botan::secure_vector<uint8_t> bin = Botan::base64_decode(reinterpret_cast<const char*>(b), l);
            output().write(reinterpret_cast<const char*>(bin.data()), bin.size());
            };

         this->read_file(get_arg("file"), write_bin, 1024);
         }
   };

BOTAN_REGISTER_COMMAND("base64_dec", Base64_Decode);

#endif // base64

}