CMSDK 2.0.1
Cross-platform C++ base library and SDK for the Psyclone AIOS platform
Loading...
Searching...
No Matches
digest.cpp
Go to the documentation of this file.
1
6// //////////////////////////////////////////////////////////
7// digest.cpp
8// Copyright (c) 2014,2015 Stephan Brumme. All rights reserved.
9// see http://create.stephan-brumme.com/disclaimer.html
10//
11
12// g++ -O3 digest.cpp crc32.cpp md5.cpp sha1.cpp sha256.cpp keccak.cpp sha3.cpp -o digest
13
14#include "crc32.h"
15#include "md5.h"
16#include "sha1.h"
17#include "sha256.h"
18#include "keccak.h"
19#include "sha3.h"
20
21#include <iostream>
22#include <fstream>
23
24
25int main(int argc, char** argv)
26{
27 // syntax check
28 if (argc < 2 || argc > 3)
29 {
30 std::cout << "./digest filename [--crc|--md5|--sha1|--sha256|--keccak|--sha3]" << std::endl;
31 return 1;
32 }
33
34 // parameters
35 std::string filename = argv[1];
36 std::string algorithm = argc == 3 ? argv[2] : "";
37 bool computeCrc32 = algorithm.empty() || algorithm == "--crc";
38 bool computeMd5 = algorithm.empty() || algorithm == "--md5";
39 bool computeSha1 = algorithm.empty() || algorithm == "--sha1";
40 bool computeSha2 = algorithm.empty() || algorithm == "--sha2" || algorithm == "--sha256";
41 bool computeKeccak = algorithm.empty() || algorithm == "--keccak";
42 bool computeSha3 = algorithm.empty() || algorithm == "--sha3";
43
44 CRC32 digestCrc32;
45 MD5 digestMd5;
46 SHA1 digestSha1;
47 SHA256 digestSha2;
48 Keccak digestKeccak(Keccak::Keccak256);
49 SHA3 digestSha3 (SHA3 ::Bits256);
50
51 // each cycle processes about 1 MByte (divisible by 144 => improves Keccak/SHA3 performance)
52 const size_t BufferSize = 144*7*1024;
53 char* buffer = new char[BufferSize];
54
55 // select input source: either file or standard-in
56 std::ifstream file;
57 std::istream* input = NULL;
58 // accept std::cin, syntax will be: "./digest - --sha3 < data"
59 if (filename == "-")
60 {
61 input = &std::cin;
62 }
63 else
64 {
65 // open file
66 file.open(filename.c_str(), std::ios::in | std::ios::binary);
67 if (!file)
68 {
69 std::cerr << "Can't open '" << filename << "'" << std::endl;
70 return 2;
71 }
72
73 input = &file;
74 }
75
76 // process file
77 while (*input)
78 {
79 input->read(buffer, BufferSize);
80 std::size_t numBytesRead = size_t(input->gcount());
81
82 if (computeCrc32)
83 digestCrc32 .add(buffer, numBytesRead);
84 if (computeMd5)
85 digestMd5 .add(buffer, numBytesRead);
86 if (computeSha1)
87 digestSha1 .add(buffer, numBytesRead);
88 if (computeSha2)
89 digestSha2 .add(buffer, numBytesRead);
90 if (computeKeccak)
91 digestKeccak.add(buffer, numBytesRead);
92 if (computeSha3)
93 digestSha3 .add(buffer, numBytesRead);
94 }
95
96 // clean up
97 file.close();
98 delete[] buffer;
99
100 // show results
101 if (computeCrc32)
102 std::cout << "CRC32: " << digestCrc32 .getHash() << std::endl;
103 if (computeMd5)
104 std::cout << "MD5: " << digestMd5 .getHash() << std::endl;
105 if (computeSha1)
106 std::cout << "SHA1: " << digestSha1 .getHash() << std::endl;
107 if (computeSha2)
108 std::cout << "SHA2/256: " << digestSha2 .getHash() << std::endl;
109 if (computeKeccak)
110 std::cout << "Keccak/256: " << digestKeccak.getHash() << std::endl;
111 if (computeSha3)
112 std::cout << "SHA3/256: " << digestSha3 .getHash() << std::endl;
113
114 return 0;
115}
Third-party (vendored): CRC32 checksum from Stephan Brumme's portable hashing library (create....
int main(int argc, char **argv)
Definition digest.cpp:25
Third-party (vendored): Keccak (pre-standard SHA-3) hash from Stephan Brumme's portable hashing libra...
Third-party (vendored): MD5 hash from Stephan Brumme's portable hashing library (create....
Third-party (vendored): SHA-1 hash from Stephan Brumme's portable hashing library (create....
Third-party (vendored): SHA-256 hash from Stephan Brumme's portable hashing library (create....
Third-party (vendored): SHA-3 hash from Stephan Brumme's portable hashing library (create....