-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.cpp
99 lines (81 loc) · 2.77 KB
/
main.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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
#include <iostream>
#include <fstream>
#include <iterator>
#include "keccak.h"
#include "utilities.h"
#include <vector>
#include <bitset>
#include <algorithm>
/**
* Note: binary_write and binary_read are taken from
* https://stackoverflow.com/questions/29623605/how-to-dump-stdvectorbool-in-a-binary-file
* with small edit
* author: https://stackoverflow.com/users/3378179/caduchon
*/
void binary_write(std::ofstream& fout, const std::vector<bool>& x) {
std::vector<bool>::size_type n = x.size();
for(std::vector<bool>::size_type i = 0; i < n;) {
unsigned char aggr = 0;
for(unsigned char mask = 1; mask > 0 && i < n; ++i, mask <<= 1)
if(x.at(i))
aggr |= mask;
fout.write((const char*)&aggr, sizeof(unsigned char));
}
}
void binary_read(std::ifstream& fin, std::vector<bool>& x, std::vector<bool>::size_type n) {
x.resize(n);
for(std::vector<bool>::size_type i = 0; i < n;)
{
unsigned char aggr;
fin.read((char*)&aggr, sizeof(unsigned char));
for(unsigned char mask = 1; mask > 0 && i < n; ++i, mask <<= 1)
x.at(i) = aggr & mask;
}
}
// this function return the file size
int get_file_size_in_byte(const std::string file_name) {
int size_f = 0;
std::ifstream inf(file_name);
inf.seekg(0, std::ios::end);
int file_size = inf.tellg();
inf.seekg(0, std::ios::beg);
std::cout << "file size = " << file_size << std::endl;
return file_size * 8;
}
int main(int argc, char *argv[]) {
if(argc != 3) {
std::cout << "incorrect number of arguments" << '\n';
return 0;
}
std::cout << argv[0] << '\n';
int N = std::stoi(argv[1]);
N *= 8;
std::string file_name = std::string(argv[2]);
auto file_size = get_file_size_in_byte(file_name);
std::vector<bool> input_vector(file_size);
std::ifstream in;
in.open(file_name, std::ios::binary);
std::vector<bool>::size_type n = input_vector.size();
std::cout << "reading the file" << std::endl;
binary_read(in, input_vector, n);
in.close();
std::cout << "applying shake128" << std::endl;
auto res = shake128(input_vector, N);
std::ofstream out;
out.open("digest.bin");
std::cout << "writing the result in digest.bin" << std::endl;
binary_write(out, res);
out.close();
in.open("digest.bin", std::ios::binary);
auto vsize = get_file_size_in_byte("digest.bin");
std::vector<bool> ver(vsize);
binary_read(in, ver, vsize);
std::cout << "uncomment the last lines to display the result" << std::endl;
auto str = vec_of_bool_string(ver);
auto ss = reverse_string(str);
// std::cout << vsize << std::endl;
std::string display = ss; //std::string(ss.begin(), ss.begin() + N/4);
std::transform(display.begin(), display.end(), display.begin(), ::tolower);
std::cout << display << std::endl;
return 0;
}