1
2 #include "../digestpp.hpp"
3 #include <numeric>
4 #include <iostream>
5 #include <iomanip>
6 #include <fstream>
7
8 using namespace std;
9 using namespace digestpp;
10
11
12 // Calculate BLAKE2b digest from a double quoted string and output it in hex format
example1()13 void example1()
14 {
15 cout << blake2b().absorb("The quick brown fox jumps over the lazy dog").hexdigest() << endl;
16 }
17
18 // Calculate BLAKE2b-256 digest from an std::string and output it in hex format
example2()19 void example2()
20 {
21 string str = "The quick brown fox jumps over the lazy dog";
22 cout << blake2b(256).absorb(str).hexdigest() << endl;
23 }
24
25 // Calculate SHA-512 digest of a vector and output it in hex format
example3()26 void example3()
27 {
28 vector<unsigned char> v(100);
29 iota(v.begin(), v.end(), 0);
30
31 cout << sha512().absorb(v.begin(), v.end()).hexdigest() << endl;
32 }
33
34 // Calculate SHA-512/256 digest of a C array and output it in hex format
example4()35 void example4()
36 {
37 unsigned char c[32];
38 iota(c, c + sizeof(c), 0);
39
40 cout << sha512(256).absorb(c, sizeof(c)).hexdigest() << endl;
41 }
42
43 // Calculate SHA-256 digest of a file and output it in hex format
example5()44 void example5()
45 {
46 ifstream file("filename", ios_base::in|ios_base::binary);
47 cout << sha256().absorb(file).hexdigest() << endl;
48 }
49
50 // Generate SHA3-224 digest using multiple calls to absorb()
example6()51 void example6()
52 {
53 cout << sha3(224).absorb("The quick brown fox ").absorb("jumps over the lazy dog").hexdigest() << endl;
54 }
55
56 // Output binary digest to a vector
example7()57 void example7()
58 {
59 vector<unsigned char> v;
60 sha3(256).absorb("The quick brown fox jumps over the lazy dog").digest(back_inserter(v));
61 }
62
63 // Output binary digest to a raw C array
example8()64 void example8()
65 {
66 unsigned char buf[32];
67 sha3(256).absorb("The quick brown fox jumps over the lazy dog").digest(buf, sizeof(buf));
68 }
69
70 // Output binary digest to a stream
example9()71 void example9()
72 {
73 string str = "The quick brown fox jumps over the lazy dog";
74 string output;
75 ostringstream os(output);
76 sha3(256).absorb(str).digest(ostream_iterator<char>(os, ""));
77 }
78
79 // Generate long output using SHAKE-256 extendable output function using multiple calls to squeeze()
example10()80 void example10()
81 {
82 vector<unsigned char> v;
83 shake256 xof;
84 xof.absorb("The quick brown fox jumps over the lazy dog");
85 xof.squeeze(1000, back_inserter(v));
86 xof.squeeze(1000, back_inserter(v));
87 xof.squeeze(1000, back_inserter(v));
88 cout << "Squeezed " << v.size() << " bytes." << endl;
89 }
90
91 // Generate 64-byte digest using customizable cSHAKE-256 algorithm and print it in hex format
example11()92 void example11()
93 {
94 cshake256 xof;
95 xof.set_customization("Customization");
96 cout << xof.absorb("The quick brown fox jumps over the lazy dog").hexsqueeze(64) << endl;
97 }
98
99
main()100 int main()
101 {
102 example1();
103 example2();
104 example3();
105 example4();
106 example5();
107 example6();
108 example7();
109 example8();
110 example9();
111 example10();
112 example11();
113 }