X-Git-Url: http://git.tdb.fi/?p=libs%2Fcrypto.git;a=blobdiff_plain;f=examples%2Fchecksum.cpp;fp=examples%2Fchecksum.cpp;h=f5ef853b28c03300b8a67e3a2d9918797dfe134f;hp=0000000000000000000000000000000000000000;hb=d599dd9a681d815dd6627b358ee36b5a6354f6c0;hpb=5e5fd3c47ef3d8bfd29a31311e1d933c6138ed44 diff --git a/examples/checksum.cpp b/examples/checksum.cpp new file mode 100644 index 0000000..f5ef853 --- /dev/null +++ b/examples/checksum.cpp @@ -0,0 +1,75 @@ +#include +#include +#include +#include +#include +#include +#include +#include + +using namespace std; +using namespace Msp; + +class Checksum: public Msp::RegisteredApplication +{ +private: + bool do_md5; + bool do_sha256; + bool do_sha512; + list files; + +public: + Checksum(int, char **); + + virtual int main(); +private: + template + 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::const_iterator i=files.begin(); i!=files.end(); ++i) + { + if(do_md5) + process_file(*i); + if(do_sha256) + process_file(*i); + if(do_sha512) + process_file(*i); + } + return 0; +} + +template +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); +}