-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMurmurHash3CLI.cpp
78 lines (68 loc) · 2.41 KB
/
MurmurHash3CLI.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
/*
* MurmurHash3CLI - command line interface for MurmurHash3
* Copyright (C) 2011 Jaromir Capik <[email protected]>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*/
#include <string>
#include <cstdlib>
#include <sstream>
#include <istream>
#include <ostream>
#include <iomanip>
#include <iterator>
#include <iostream>
#include "MurmurHash3.h"
#define MURMURHASH3_OUTPUT_LENGTH 16
typedef uint8_t MurmurHash3_output_t[MURMURHASH3_OUTPUT_LENGTH];
typedef uint32_t MurmurHash3_seed_t;
using namespace std;
int main(int argc, char **argv)
{
if (argc != 2) {
cerr << "Usage : MurmurHash3CLI <32bit-decimal-seed>" << endl;
cerr << endl;
cerr << "Application reads the input data from the standard input" << endl;
cerr << "and prints the result 128-bit hex coded hash to the standard output." << endl;
cerr << "Seed can be either signed or unsigned." << endl;
exit(EXIT_FAILURE);
}
// convert the seed argument to int32_t/uint32_t with type checking
istringstream seed_arg(argv[1]);
MurmurHash3_seed_t seed;
if ((seed_arg >> seed) && (seed_arg.eof())) {
// conversion successful
// we could test the input for negative values, but we don't want to do that
// that allows us to be signed/unsigned independent
;
} else {
// conversion failed
cerr << "Seed invalid or out of range." << endl;
exit(EXIT_FAILURE);
}
// stdin -> string (copy until EOF)
cin >> noskipws;
istream_iterator<char> it(std::cin);
istream_iterator<char> end;
string input(it, end);
// Hash calculation
MurmurHash3_output_t output;
MurmurHash3_x64_128(input.c_str(), input.length(), seed, output);
// Write the hex coded hash to stdout
for (int i = 0; i < MURMURHASH3_OUTPUT_LENGTH; i++) {
cout << setfill('0') << setw(2) << hex << int(output[i]);
}
return EXIT_SUCCESS;
}