-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.cpp
132 lines (117 loc) · 2.92 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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
//#define MEMLEAK
#include "sha256.hpp"
#include <iostream>
#include <sstream>
//#include <vector>
#include <fstream>
#include <exception>
///timer here
#ifdef _WIN32
#include <windows.h>
#include <conio.h>
#else
#include <sys/time.h>
#include <ctime>
#endif // _WIN32
using namespace std;
uint64_t GetTimeMs64(){
#ifdef _WIN32
FILETIME ft;
LARGE_INTEGER li;
GetSystemTimeAsFileTime(&ft);
li.LowPart = ft.dwLowDateTime;
li.HighPart = ft.dwHighDateTime;
uint64_t ret = li.QuadPart;
ret -= 116444736000000000LL;
ret /= 10000;
return ret;
#else
struct timeval tv;
gettimeofday(&tv, NULL);
uint64_t ret = tv.tv_usec;
ret /= 1000;
ret += (tv.tv_sec * 1000);
return ret;
#endif // _WIN32
}
void show_percent(u32 x){
u32 y, f = 0;
cout << '[';
f = x / 5;
for(y = 0; y < f; y++){
cout << '.';
}
f = 20 - f;
for(y = 0; y < f; y++){
cout << ' ';
}
cout << ']'<< ' ' << x << '%' << flush;
cout << '\r';
}
int main(){
#ifdef MEMLEAK
_CrtSetDbgFlag(_CRTDBG_ALLOC_MEM_DF | _CRTDBG_LEAK_CHECK_DF );
_CrtSetReportMode(_CRT_WARN, _CRTDBG_MODE_DEBUG);
std::cout << "Checking for memory leaks..." << std::endl;
#endif //MEMLEAK
u64 start_time, end_time;
string input;
sha256 sh;/*
while(true){
cout << "Enter text:";
getline(cin, input);
if(input == ":q" || input == ":e" || input == ".q" || input == ".e" || input == ":exit" || input == ".exit" || input == ".quit" || input == ":quit"){
break;
}
cout << sh.hash(input) << endl;
}*/
cout << "Enter test file:";
getline(cin, input);
ifstream filex;
//char inputMain[10];
u64 sizex;
//char * inp = new char[1];
char * inp = nullptr;
//u32 ipl = 0;
cout << "Opening file test: " << input << endl;
filex.open(input.c_str(), ios::in | ios::binary | ios::ate);
if(filex.is_open()){
sizex = (u64)filex.tellg();
inp = new (nothrow) char[sizex];
if(inp == 0){
cout << "Allocation Error!" << endl;
goto DEAD;
}
filex.seekg(0, ios::beg);
filex.read(inp, sizex);
filex.close();
cout << "Starting to read test file..." << endl;
try{
start_time = GetTimeMs64();
sh.hash_update(inp, sizex, show_percent);
sh.hash_finalize();
end_time = GetTimeMs64();
//for(u32 cl=0; cl < 27; cl++){
//cout << ' ';
//}
//cout << '\r';
cout << "The hash is done: " << sh.hash_result() << endl;
cout << "Took " << (end_time - start_time) << " ms." << endl;
}catch(exception& e){
cerr << "An error happened. Error type: " << e.what() << endl;
goto DEAD;
}
}else{
cout << "The test cannot open the test file: " << input << endl;
}
DEAD:
delete [] inp;
cout << "Press any key to exit";
//use _getch instead of getch
#ifdef _WIN32
_getch();
#else
getchar();
#endif
return 0;
}