aboutsummaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
authorJack Lloyd <[email protected]>2019-09-23 11:33:53 -0400
committerJack Lloyd <[email protected]>2019-09-23 11:33:53 -0400
commit9b659f3269393423baaf6546c1130c28b4ae3c38 (patch)
tree3e5beeef77e9c96919960688cc83301919b8d979 /src
parent7c3122ec4275630d78fbdcb03882b7e1fee45c81 (diff)
Add base32 encoding/decoding util to the cli
Diffstat (limited to 'src')
-rw-r--r--src/cli/codec.cpp66
-rwxr-xr-xsrc/scripts/test_cli.py5
2 files changed, 70 insertions, 1 deletions
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 <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
@@ -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<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)
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,