aboutsummaryrefslogtreecommitdiffstats
path: root/src/cli
diff options
context:
space:
mode:
authorJack Lloyd <[email protected]>2019-01-22 11:16:10 -0500
committerJack Lloyd <[email protected]>2019-01-22 11:16:10 -0500
commit8437eb4f00b3c68073be7a1ced82e355d15abe27 (patch)
tree3b75c6a84cca86c1c0f64ce7fcc2ca93e9423de6 /src/cli
parent1d979eea648ad5c6d948bd58eae158b7ab0c5193 (diff)
Move hash util to hash.cpp and add --format option
For example/documentation purposes, having distinct files makes the code easier to find. New --format option allows encoding as base64 or base58
Diffstat (limited to 'src/cli')
-rw-r--r--src/cli/hash.cpp118
-rw-r--r--src/cli/utils.cpp65
2 files changed, 118 insertions, 65 deletions
diff --git a/src/cli/hash.cpp b/src/cli/hash.cpp
new file mode 100644
index 000000000..f579936f6
--- /dev/null
+++ b/src/cli/hash.cpp
@@ -0,0 +1,118 @@
+/*
+* (C) 2009,2010,2014,2015,2019 Jack Lloyd
+*
+* Botan is released under the Simplified BSD License (see license.txt)
+*/
+
+#include "cli.h"
+
+#if defined(BOTAN_HAS_HASH) && defined(BOTAN_HAS_HEX_CODEC)
+ #include <botan/hash.h>
+ #include <botan/hex.h>
+ #include <botan/base64.h>
+#endif
+
+#if defined(BOTAN_HAS_BASE64_CODEC)
+ #include <botan/base64.h>
+#endif
+
+#if defined(BOTAN_HAS_BASE58_CODEC)
+ #include <botan/base58.h>
+#endif
+
+namespace Botan_CLI {
+
+#if defined(BOTAN_HAS_HASH)
+
+class Hash final : public Command
+ {
+ public:
+ Hash() : Command("hash --algo=SHA-256 --buf-size=4096 --no-fsname --format=hex *files") {}
+
+ std::string group() const override
+ {
+ return "hash";
+ }
+
+ std::string description() const override
+ {
+ return "Compute the message digest of given file(s)";
+ }
+
+ template<typename Alloc>
+ static std::string format_digest(const std::string& format,
+ const std::vector<uint8_t, Alloc>& vec)
+ {
+ if(format == "hex")
+ {
+ return Botan::hex_encode(vec);
+ }
+#if defined(BOTAN_HAS_BASE64_CODEC)
+ else if(format == "base64")
+ {
+ return Botan::base64_encode(vec);
+ }
+#endif
+#if defined(BOTAN_HAS_BASE64_CODEC)
+ else if(format == "base58")
+ {
+ return Botan::base58_encode(vec);
+ }
+ else if(format == "base58check")
+ {
+ return Botan::base58_check_encode(vec);
+ }
+#endif
+ else
+ {
+ throw CLI_Usage_Error("Unknown format for encoding digests");
+ }
+ }
+
+ void go() override
+ {
+ const std::string hash_algo = get_arg("algo");
+ const std::string format = get_arg("format");
+ const size_t buf_size = get_arg_sz("buf-size");
+ const bool no_fsname = flag_set("no-fsname");
+
+ std::unique_ptr<Botan::HashFunction> hash_fn(Botan::HashFunction::create(hash_algo));
+
+ if(!hash_fn)
+ {
+ throw CLI_Error_Unsupported("hashing", hash_algo);
+ }
+
+ std::vector<std::string> files = get_arg_list("files");
+ if(files.empty())
+ {
+ files.push_back("-");
+ } // read stdin if no arguments on command line
+
+ for(const std::string& fsname : files)
+ {
+ try
+ {
+ auto update_hash = [&](const uint8_t b[], size_t l) { hash_fn->update(b, l); };
+ read_file(fsname, update_hash, buf_size);
+
+ const std::string digest = format_digest(format, hash_fn->final());
+
+ if(no_fsname)
+ output() << digest << "\n";
+ else
+ output() << digest << " " << fsname << "\n";
+ }
+ catch(CLI_IO_Error& e)
+ {
+ error_output() << e.what() << "\n";
+ }
+ }
+ }
+ };
+
+BOTAN_REGISTER_COMMAND("hash", Hash);
+
+#endif
+
+}
diff --git a/src/cli/utils.cpp b/src/cli/utils.cpp
index 99471fba5..7851ecdc6 100644
--- a/src/cli/utils.cpp
+++ b/src/cli/utils.cpp
@@ -17,10 +17,6 @@
#include <iterator>
#include <iomanip>
-#if defined(BOTAN_HAS_HASH)
- #include <botan/hash.h>
-#endif
-
#if defined(BOTAN_HAS_MAC)
#include <botan/mac.h>
#endif
@@ -269,67 +265,6 @@ class Print_Cpuid final : public Command
BOTAN_REGISTER_COMMAND("cpuid", Print_Cpuid);
-#if defined(BOTAN_HAS_HASH)
-
-class Hash final : public Command
- {
- public:
- Hash() : Command("hash --algo=SHA-256 --buf-size=4096 --no-fsname *files") {}
-
- std::string group() const override
- {
- return "hash";
- }
-
- std::string description() const override
- {
- return "Compute the message digest of given file(s)";
- }
-
- void go() override
- {
- const std::string hash_algo = get_arg("algo");
- std::unique_ptr<Botan::HashFunction> hash_fn(Botan::HashFunction::create(hash_algo));
-
- if(!hash_fn)
- {
- throw CLI_Error_Unsupported("hashing", hash_algo);
- }
-
- const size_t buf_size = get_arg_sz("buf-size");
- const bool no_fsname = flag_set("no-fsname");
-
- std::vector<std::string> files = get_arg_list("files");
- if(files.empty())
- {
- files.push_back("-");
- } // read stdin if no arguments on command line
-
- for(const std::string& fsname : files)
- {
- try
- {
- auto update_hash = [&](const uint8_t b[], size_t l) { hash_fn->update(b, l); };
- read_file(fsname, update_hash, buf_size);
- const std::string digest = Botan::hex_encode(hash_fn->final());
-
- if(no_fsname)
- output() << digest << "\n";
- else
- output() << digest << " " << fsname << "\n";
- }
- catch(CLI_IO_Error& e)
- {
- error_output() << e.what() << "\n";
- }
- }
- }
- };
-
-BOTAN_REGISTER_COMMAND("hash", Hash);
-
-#endif
-
class RNG final : public Command
{
public: