2016-02-26 13:36:01 +11:00
|
|
|
/*
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
*
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
|
|
|
*
|
|
|
|
* Copyright:
|
2016-06-17 14:58:39 +10:00
|
|
|
* 2014-2016, Danny Robson <danny@nerdcruft.net>
|
2016-02-26 13:36:01 +11:00
|
|
|
*/
|
|
|
|
|
2014-05-09 14:44:40 +10:00
|
|
|
#include <iostream>
|
|
|
|
#include <cstdlib>
|
|
|
|
#include <cstring>
|
|
|
|
|
2016-06-20 16:50:38 +10:00
|
|
|
#include "io.hpp"
|
2017-01-05 20:21:20 +11:00
|
|
|
#include "stream.hpp"
|
2016-06-20 16:50:38 +10:00
|
|
|
|
2014-05-09 14:44:40 +10:00
|
|
|
#include "hash/adler.hpp"
|
|
|
|
#include "hash/bsdsum.cpp"
|
|
|
|
#include "hash/crc.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"
|
|
|
|
|
|
|
|
|
2016-06-17 14:58:39 +10:00
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
static
|
|
|
|
const char* NAMES[] = {
|
2014-05-09 14:44:40 +10:00
|
|
|
"adler32",
|
|
|
|
"bsdsum",
|
2017-02-13 17:13:46 +11:00
|
|
|
"crc",
|
2014-05-09 14:44:40 +10:00
|
|
|
"MD2",
|
|
|
|
"MD4",
|
|
|
|
"MD5",
|
|
|
|
"RIPEMD",
|
|
|
|
"SHA1",
|
|
|
|
"SHA256",
|
|
|
|
};
|
|
|
|
|
|
|
|
|
2016-06-17 14:58:39 +10:00
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
2016-06-20 16:50:38 +10:00
|
|
|
std::ostream&
|
|
|
|
print_digest (std::ostream &os, uint32_t t)
|
|
|
|
{
|
|
|
|
return os << std::hex << t << std::dec;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
template <size_t S>
|
|
|
|
std::ostream&
|
|
|
|
print_digest (std::ostream &os, std::array<uint8_t,S> digest)
|
2016-06-17 14:58:39 +10:00
|
|
|
{
|
2017-01-05 20:21:20 +11:00
|
|
|
util::stream::scoped::flags f (os);
|
2016-06-20 16:50:38 +10:00
|
|
|
os << std::hex;
|
2014-05-09 14:44:40 +10:00
|
|
|
|
2016-06-20 16:50:38 +10:00
|
|
|
for (auto c: digest)
|
|
|
|
os << +(c >> 4) << +(c & 0x0F);
|
2014-05-09 14:44:40 +10:00
|
|
|
|
2016-06-20 16:50:38 +10:00
|
|
|
return os;
|
|
|
|
}
|
2014-05-09 14:44:40 +10:00
|
|
|
|
|
|
|
|
2016-06-20 16:50:38 +10:00
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
2018-01-13 13:48:58 +11:00
|
|
|
static void
|
|
|
|
compute (const std::string &name, const util::view<const uint8_t*> data)
|
2016-06-20 16:50:38 +10:00
|
|
|
{
|
2018-01-13 13:48:58 +11:00
|
|
|
#define stream(TYPE, ...) do { \
|
|
|
|
if (name != #TYPE) \
|
|
|
|
break; \
|
|
|
|
\
|
|
|
|
print_digest ( \
|
|
|
|
std::cout, \
|
|
|
|
util::hash::TYPE{} (data) \
|
|
|
|
) << '\n'; \
|
|
|
|
return; \
|
2014-05-09 14:44:40 +10:00
|
|
|
} while (0);
|
|
|
|
|
2016-06-20 16:50:38 +10:00
|
|
|
stream (adler32);
|
2018-01-13 13:48:58 +11:00
|
|
|
//stream (bsdsum);
|
|
|
|
//stream (crc32);
|
2016-06-20 16:50:38 +10:00
|
|
|
|
2018-01-13 13:48:58 +11:00
|
|
|
//stream (MD2);
|
|
|
|
//stream (MD4);
|
|
|
|
//stream (MD5);
|
|
|
|
//stream (RIPEMD);
|
|
|
|
//stream (SHA1);
|
|
|
|
//stream (SHA256);
|
2014-05-09 14:44:40 +10:00
|
|
|
|
|
|
|
#undef stream
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2016-06-17 14:58:39 +10:00
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
2014-05-09 14:44:40 +10:00
|
|
|
enum {
|
|
|
|
ARG_CMD,
|
|
|
|
ARG_HASH,
|
|
|
|
ARG_INPUT,
|
2016-06-17 14:58:12 +10:00
|
|
|
|
|
|
|
NUM_ARGS
|
2014-05-09 14:44:40 +10:00
|
|
|
};
|
|
|
|
|
|
|
|
|
2016-06-17 14:58:12 +10:00
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
void
|
|
|
|
print_usage (int argc, char **argv)
|
|
|
|
{
|
|
|
|
(void)argc;
|
|
|
|
|
|
|
|
std::cerr << argv[ARG_CMD] << " [";
|
|
|
|
for (auto name: NAMES)
|
|
|
|
std::cerr << name << "|";
|
|
|
|
std::cerr << "] <input>\n";
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
//-----------------------------------------------------------------------------
|
2014-05-09 14:44:40 +10:00
|
|
|
int
|
2016-06-17 14:58:39 +10:00
|
|
|
main (int argc, char **argv)
|
|
|
|
{
|
2016-06-17 14:58:12 +10:00
|
|
|
if (argc != NUM_ARGS) {
|
|
|
|
print_usage (argc, argv);
|
2014-05-09 14:44:40 +10:00
|
|
|
return EXIT_FAILURE;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (strcmp (argv[ARG_INPUT], "-")) {
|
2018-01-13 13:48:58 +11:00
|
|
|
const util::mapped_file src (argv[ARG_INPUT]);
|
|
|
|
compute (argv[ARG_HASH], util::view{src});
|
2014-05-09 14:44:40 +10:00
|
|
|
|
2016-06-20 16:50:38 +10:00
|
|
|
return EXIT_SUCCESS;
|
|
|
|
} else {
|
|
|
|
//compute (argv[ARG_HASH], std::cin);
|
|
|
|
return EXIT_SUCCESS;
|
|
|
|
}
|
2014-05-09 14:44:40 +10:00
|
|
|
}
|