tools: add convenience hash computing tool

This commit is contained in:
Danny Robson 2014-05-09 14:44:40 +10:00
parent b4a07538df
commit 246b68c540
3 changed files with 117 additions and 16 deletions

View File

@ -183,20 +183,10 @@ libutil_la_LIBADD = $(BOOST_SYSTEM_LIB)
###############################################################################
## Utility programs
LDADD = $(top_builddir)/.libs/libutil.a
AM_DEFAULT_SOURCE_EXT = .cpp
LDADD = $(top_builddir)/libutil.la
bin_PROGRAMS = \
tools/hash \
tools/json-clean \
tools/json-validate \
tools/json-schema
tools_json_clean_SOURCES = tools/json-clean.cpp
tools_json_clean_DEPENDENCIES = $(top_builddir)/libutil.la
tools_json_clean_LDFLAGS = $(AM_LDFLAGS)
tools_json_schema_SOURCES = tools/json-schema.cpp
tools_json_schema_DEPENDENCIES = $(top_builddir)/libutil.la
tools_json_schema_LDFLAGS = $(AM_LDFLAGS)
tools_json_validate_SOURCES = tools/json-validate.cpp
tools_json_validate_DEPENDENCIES = $(top_builddir)/libutil.la
tools_json_validate_LDFLAGS = $(AM_LDFLAGS)

7
tools/.gitignore vendored
View File

@ -1,3 +1,4 @@
json-clean
json-schema
json-validate
/hash
/json-clean
/json-schema
/json-validate

110
tools/hash.cpp Normal file
View File

@ -0,0 +1,110 @@
#include <iostream>
#include <fstream>
#include <vector>
#include <iterator>
#include <cstdlib>
#include <cstring>
#include "hash/adler.hpp"
#include "hash/bsdsum.cpp"
#include "hash/crc.hpp"
#include "hash/fletcher.hpp"
#include "hash/md2.hpp"
#include "hash/md4.hpp"
#include "hash/md5.hpp"
#include "hash/ripemd.hpp"
#include "hash/sha1.hpp"
#include "hash/sha2.hpp"
static const char* NAMES[] = {
"adler32",
"bsdsum",
"crc32",
"fletcher"
"MD2",
"MD4",
"MD5",
"RIPEMD",
"SHA1",
"SHA256",
};
static void
compute (std::istream &is, const char *name) {
std::vector<uint8_t> data {
std::istream_iterator<uint8_t> (is),
std::istream_iterator<uint8_t> ( )
};
std::cerr << "got: ";
std::copy (data.begin (), data.end (), std::ostream_iterator<uint8_t> (std::cerr));
std::cerr << "\n";
#define simple(FUNC) do { \
if (std::strcmp (name, #FUNC)) \
break; \
\
std::cout << std::hex << FUNC (data.data (), data.size ()) << "\n"; \
} while (0)
simple (adler32);
simple (bsdsum);
simple (crc32);
#undef simple
#define stream(TYPE) do { \
if (std::strcmp (name, #TYPE)) \
break; \
\
util::hash::TYPE accum; \
accum.update (data.data (), data.size ()); \
accum.finish (); \
\
std::cout << std::hex; \
for (auto c: accum.digest ()) \
std::cout << unsigned (c >> 4) << unsigned(c & 0x0F); \
std::cout << std::dec << "\n"; \
return; \
} while (0);
stream (MD2);
stream (MD4);
stream (MD5);
stream (RIPEMD);
stream (SHA1);
stream (SHA256);
#undef stream
}
enum {
ARG_CMD,
ARG_HASH,
ARG_INPUT,
};
int
main (int argc, char **argv) {
if (argc < ARG_INPUT) {
std::cerr << argv[ARG_CMD] << " [";
for (auto name: NAMES)
std::cerr << name << "|";
std::cerr << "] <input>\n";
return EXIT_FAILURE;
}
if (strcmp (argv[ARG_INPUT], "-")) {
std::cerr << "stdin only currently\n";
return EXIT_FAILURE;
}
compute (std::cin, argv[ARG_HASH]);
return EXIT_SUCCESS;
}