-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathshake256.cpp
More file actions
46 lines (38 loc) · 1.13 KB
/
shake256.cpp
File metadata and controls
46 lines (38 loc) · 1.13 KB
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
#include "sha3/shake256.hpp"
#include "example_helper.hpp"
#include <cstddef>
#include <cstdint>
#include <cstdlib>
#include <iostream>
#include <numeric>
#include <span>
#include <vector>
// Compile it using
//
// g++ -std=c++20 -Wall -O3 -march=native -I include examples/shake256.cpp
int
main()
{
constexpr size_t msg_len = 32;
constexpr size_t out_len = 40;
std::vector<uint8_t> msg(msg_len, 0);
std::iota(msg.begin(), msg.end(), 0);
std::vector<uint8_t> out(out_len, 0);
auto out_span = std::span(out);
// Create SHAKE256 hasher
shake256::shake256_t hasher;
// Absorb message bytes into sponge state
hasher.absorb(msg);
// Finalize sponge state
hasher.finalize();
// Squeeze total `out_len` -bytes out of sponge, a single byte at a time.
// One can request arbitrary many bytes of output, by calling `squeeze` arbitrary
// many times, after it has been finalized.
for (size_t i = 0; i < out_len; i++) {
hasher.squeeze(out_span.subspan(i, 1));
}
std::cout << "SHAKE256\n\n";
std::cout << "Message : " << to_hex(msg) << "\n";
std::cout << "Output : " << to_hex(out) << "\n";
return EXIT_SUCCESS;
}