]> git.tdb.fi Git - libs/crypto.git/commitdiff
Add an example program demonstrating hash API
authorMikko Rasa <tdb@tdb.fi>
Sat, 16 Apr 2016 13:51:46 +0000 (16:51 +0300)
committerMikko Rasa <tdb@tdb.fi>
Sat, 16 Apr 2016 13:51:46 +0000 (16:51 +0300)
Also useful for verifying the implementations.

.gitignore
Build
examples/checksum.cpp [new file with mode: 0644]

index 536c16b725f0e0cc4c2e2d7df832d8a2b5400212..87f978a1c5d5cf28dee098547028a427851f9f30 100644 (file)
@@ -1,5 +1,6 @@
 .config
 temp
+/checksum
 /libmspcrypto.a
 /libmspcrypto.so
 /mspcrypto.pc
diff --git a/Build b/Build
index 003e9e32f2de440543802c18654ce54efa76c627..9b96d9e120023ba37f317dc7aa46eccd25200ec4 100644 (file)
--- a/Build
+++ b/Build
@@ -14,4 +14,10 @@ package "mspcrypto"
                        map "source" "include/msp/crypto";
                };
        };
+
+       program "checksum"
+       {
+               source "examples/checksum.cpp";
+               use "mspcrypto";
+       };
 };
diff --git a/examples/checksum.cpp b/examples/checksum.cpp
new file mode 100644 (file)
index 0000000..f5ef853
--- /dev/null
@@ -0,0 +1,75 @@
+#include <list>
+#include <string>
+#include <msp/core/application.h>
+#include <msp/core/getopt.h>
+#include <msp/crypto/md5.h>
+#include <msp/crypto/sha2.h>
+#include <msp/io/file.h>
+#include <msp/io/print.h>
+
+using namespace std;
+using namespace Msp;
+
+class Checksum: public Msp::RegisteredApplication<Checksum>
+{
+private:
+       bool do_md5;
+       bool do_sha256;
+       bool do_sha512;
+       list<string> files;
+
+public:
+       Checksum(int, char **);
+
+       virtual int main();
+private:
+       template<typename H>
+       void process_file(const std::string &);
+};
+
+Checksum::Checksum(int argc, char **argv):
+       do_md5(false),
+       do_sha256(false),
+       do_sha512(false)
+{
+
+       GetOpt getopt;
+       getopt.add_option("md5", do_md5, GetOpt::NO_ARG);
+       getopt.add_option("sha256", do_sha256, GetOpt::NO_ARG);
+       getopt.add_option("sha512", do_sha512, GetOpt::NO_ARG);
+       getopt.add_argument("file", files, GetOpt::REQUIRED_ARG);
+       getopt(argc, argv);
+
+       if(!do_md5 && !do_sha256 && !do_sha512)
+               do_md5 = true;
+}
+
+int Checksum::main()
+{
+       for(list<string>::const_iterator i=files.begin(); i!=files.end(); ++i)
+       {
+               if(do_md5)
+                       process_file<Crypto::MD5>(*i);
+               if(do_sha256)
+                       process_file<Crypto::SHA256>(*i);
+               if(do_sha512)
+                       process_file<Crypto::SHA512>(*i);
+       }
+       return 0;
+}
+
+template<typename H>
+void Checksum::process_file(const string &fn)
+{
+       IO::BufferedFile infile(fn);
+       H hash;
+       while(1)
+       {
+               char buffer[16384];
+               unsigned len = infile.read(buffer, sizeof(buffer));
+               if(!len)
+                       break;
+               hash.update(buffer, len);
+       }
+       IO::print("%s  %s\n", hash.get_hexdigest(), fn);
+}