aboutsummaryrefslogtreecommitdiffstats
path: root/src/cli/utils.cpp
diff options
context:
space:
mode:
authorRené Korthaus <[email protected]>2017-04-13 10:57:12 +0200
committerRené Korthaus <[email protected]>2017-04-13 10:57:18 +0200
commitff5e2744fa8f5b13178d6526387ba47b6c936403 (patch)
treefb166c84aafc1054da2293f6fc9090dc3a66030d /src/cli/utils.cpp
parentb8b50eaf392a5da53d59f28919af0dcfd16b6f4d (diff)
Add HMAC CLI command
Diffstat (limited to 'src/cli/utils.cpp')
-rw-r--r--src/cli/utils.cpp48
1 files changed, 48 insertions, 0 deletions
diff --git a/src/cli/utils.cpp b/src/cli/utils.cpp
index 76501add4..33651c7d7 100644
--- a/src/cli/utils.cpp
+++ b/src/cli/utils.cpp
@@ -1,5 +1,6 @@
/*
* (C) 2009,2010,2014,2015 Jack Lloyd
+* (C) 2017 René Korthaus, Rohde & Schwarz Cybersecurity
*
* Botan is released under the Simplified BSD License (see license.txt)
*/
@@ -36,6 +37,10 @@
#include <botan/bcrypt.h>
#endif
+#if defined(BOTAN_HAS_HMAC)
+ #include <botan/hmac.h>
+#endif
+
namespace Botan_CLI {
class Config_Info final : public Command
@@ -309,4 +314,47 @@ BOTAN_REGISTER_COMMAND("check_bcrypt", Check_Bcrypt);
#endif // bcrypt
+#if defined(BOTAN_HAS_HMAC)
+
+class HMAC final : public Command
+ {
+ public:
+ HMAC() : Command("hmac --hash=SHA-256 --buf-size=4096 key *files") {}
+
+ void go() override
+ {
+ const std::string hash_algo = get_arg("hash");
+ std::unique_ptr<Botan::MessageAuthenticationCode> hmac(Botan::MessageAuthenticationCode::create("HMAC(" + hash_algo + ")"));
+
+ if(!hmac)
+ throw CLI_Error_Unsupported("HMAC", hash_algo);
+
+ hmac->set_key(slurp_file(get_arg("key")));
+
+ const size_t buf_size = get_arg_sz("buf-size");
+
+ 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_hmac = [&](const uint8_t b[], size_t l) { hmac->update(b, l); };
+ read_file(fsname, update_hmac, buf_size);
+ output() << Botan::hex_encode(hmac->final()) << " " << fsname << "\n";
+ }
+ catch(CLI_IO_Error& e)
+ {
+ error_output() << e.what() << "\n";
+ }
+ }
+ }
+ };
+
+BOTAN_REGISTER_COMMAND("hmac", HMAC);
+
+#endif // hmac
+
}