From 9b659f3269393423baaf6546c1130c28b4ae3c38 Mon Sep 17 00:00:00 2001 From: Jack Lloyd Date: Mon, 23 Sep 2019 11:33:53 -0400 Subject: Add base32 encoding/decoding util to the cli --- src/cli/codec.cpp | 66 ++++++++++++++++++++++++++++++++++++++++++++++++- src/scripts/test_cli.py | 5 ++++ 2 files changed, 70 insertions(+), 1 deletion(-) (limited to 'src') diff --git a/src/cli/codec.cpp b/src/cli/codec.cpp index 0d3df336d..48388d1a7 100644 --- a/src/cli/codec.cpp +++ b/src/cli/codec.cpp @@ -10,6 +10,10 @@ #include #endif +#if defined(BOTAN_HAS_BASE32_CODEC) + #include +#endif + #if defined(BOTAN_HAS_BASE58_CODEC) #include #endif @@ -139,7 +143,67 @@ class Base58_Decode final : public Command BOTAN_REGISTER_COMMAND("base58_dec", Base58_Decode); -#endif // base64 +#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 bin = Botan::base32_decode(reinterpret_cast(b), l); + output().write(reinterpret_cast(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) diff --git a/src/scripts/test_cli.py b/src/scripts/test_cli.py index 7d9b443cf..df5e8b9b7 100755 --- a/src/scripts/test_cli.py +++ b/src/scripts/test_cli.py @@ -189,6 +189,10 @@ def cli_base64_tests(_tmp_dir): test_cli("base64_enc", "-", "YmVlcyE=", "bees!") test_cli("base64_dec", "-", "bees!", "YmVlcyE=") +def cli_base32_tests(_tmp_dir): + test_cli("base32_enc", "-", "MJSWK4ZB", "bees!") + test_cli("base32_dec", "-", "bees!", "MJSWK4ZB") + def cli_base58_tests(_tmp_dir): test_cli("base58_enc", "-", "C6sRAr4", "bees!") test_cli("base58_dec", "-", "bees!", "C6sRAr4") @@ -1074,6 +1078,7 @@ def main(args=None): test_fns = [ cli_argon2_tests, cli_asn1_tests, + cli_base32_tests, cli_base58_tests, cli_base64_tests, cli_bcrypt_tests, -- cgit v1.2.3