-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcommon_key.c
250 lines (198 loc) · 6.07 KB
/
common_key.c
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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <stdbool.h>
#include <string.h>
#include "util.h"
//#include "gmpecc.h"
#include "base58/libbase58.h"
#include "rmd160/rmd160.h"
#include "sha256/sha256.h"
long long H_SIZE = 0x3FFFFFF; // Size of Hash to keep the data
int size_chunk = 0x3FFFFF; // size of DB
// names of files
char NAME_FILE_PK[255] = "Private_key.txt\0";
char NAME_FILE_MEL[255] = "mel.txt\0";
// simple hash to keep the data
int hash(char* s){
long long hash_res = 0;
int p = 31;
for(int i=0; i<strlen(s);i++){
int val = (s[i]-'0');
if ((val>10) || (val<0)){
val = s[i] - 'a' + 10;
}
hash_res *= p;
hash_res %= H_SIZE;
hash_res += val;
hash_res %= H_SIZE;
//fprintf(stderr,"r=%u\n",hash_res);
}
return (int)hash_res;
}
// simple hash to keep the data
int hash_second(char* s){
long long hash_res = 0;
int p = 37;
for(int i=0; i<strlen(s);i++){
int val = (s[i]-'0');
if ((val>10) || (val<0)){
val = s[i] - 'a' + 10;
}
hash_res *= p;
hash_res %= H_SIZE;
hash_res += val;
hash_res %= H_SIZE;
//fprintf(stderr,"r=%u\n",hash_res);
}
return (int)hash_res;
}
// simple hash to keep the data
int hash_general(char* s, int p){
long long hash_res = 0;
for(int i=0; i<strlen(s);i++){
int val = (s[i]-'0');
if ((val>10) || (val<0)){
val = s[i] - 'a' + 10;
}
hash_res *= p;
hash_res %= H_SIZE;
hash_res += val;
hash_res %= H_SIZE;
//fprintf(stderr,"r=%u\n",hash_res);
}
return (int)hash_res;
}
/*
* >>>make search
* 1) search.exe -- look for common in "Private_key.txt" and "mel.txt" - not more then 1000
* 2) search.exe <file1> <file2> <max keys>.
Example:
>>>search.exe Private_key.txt mel.txt 10000
*
*/
int main(int argc, char **argv) {
if (argc != 4 && argc != 1) {
fprintf(stderr, "Invalid number of parameters. Usage: %s <file1> <file2> <max_number_of_keys>\n", argv[0]);
}
int resulting_keys = 1000;
if (argc == 4){
int rs = sscanf(argv[3], "%d", &resulting_keys);
if(rs!=1){
fprintf(stderr, "Invalid integerkey %s", argv[3]);
return -5;
}
int set_file_pk = snprintf(NAME_FILE_PK, 255, "%s\n", argv[1]);
if(set_file_pk<=0){
fprintf(stderr, "Invalid first file name", argv[1]);
return -1;
}
int set_file_mel = snprintf(NAME_FILE_MEL, 255, "%s\n", argv[2]);
if(set_file_mel<=0){
fprintf(stderr, "Invalid second file name", argv[2]);
return -2;
}
}
int* answers = (int*) malloc(resulting_keys * sizeof(answers[0]));
int answers_count = 0;
FILE * f_pk = fopen(NAME_FILE_PK,"rt");
if(!f_pk){
fprintf(stderr, "Invalid first file", argv[1]);
return -3;
}
int pk_count = 0;
int loop_count = 0;
int* hash_database = (int*) calloc(H_SIZE,sizeof(hash_database[0]));
int* hash_database2 = (int*) calloc(H_SIZE,sizeof(hash_database2[0]));
while(f_pk){
char str_row[250];
char* res_f = fgets(str_row,250,f_pk);
if(!res_f){
break;
}
char priv_key[70];
char pub_key[70];
int ret_pk = sscanf(str_row,"P: %s C: %s", priv_key, pub_key);
if(ret_pk!=2){
continue;
}
//fprintf(stderr,"Key %s\n", pub_key);
pk_count++;
int hash_val = hash(pub_key);
int hash_val2 = hash_second(pub_key);
hash_database[hash_val] = pk_count;
hash_database2[hash_val] = pk_count;
if(pk_count>size_chunk){
FILE * f_mel = fopen(NAME_FILE_MEL,"rt");
if(!f_mel){
fprintf(stderr, "Invalid second file", argv[2]);
return -4;
}
while(f_mel){
char str_row2[250];
char* res_g = fgets(str_row2,250,f_mel);
if(!res_g){
break;
}
char pub_key2[70];
char diff[70];
int ret_mel = sscanf(str_row2,"%s # + %s", pub_key2, diff);
// fprintf(stderr,"Key 2:%s\n", pub_key2);
if(ret_mel!=2){
continue;
}
int hash_mel = hash(pub_key2);
int num_hash = hash_database[hash_mel];
int hash_mel2 = hash_second(pub_key2);
int num_hash2 = hash_database[hash_mel];
if(num_hash>0 && num_hash2==num_hash){
fprintf(stderr,"Public key %s gives hash %d of index %d", pub_key2, hash_mel, num_hash);
answers[answers_count++] = num_hash-1 + loop_count;
}
}
fclose(f_mel);
loop_count += size_chunk;
pk_count = 0;
}
}
FILE * f_mel = fopen(NAME_FILE_MEL,"rt");
if(!f_mel){
fprintf(stderr, "Invalid second file", argv[2]);
return -4;
}
while(f_mel){
char str_row2[250];
char* res_g = fgets(str_row2,250,f_mel);
if(!res_g){
break;
}
char pub_key2[70];
char diff[70];
int ret_mel = sscanf(str_row2,"%s # + %s", pub_key2, diff);
// fprintf(stderr,"Key 2:%s\n", pub_key2);
if(ret_mel!=2){
continue;
}
int hash_mel = hash(pub_key2);
int num_hash = hash_database[hash_mel];
if(num_hash>0){
fprintf(stderr,"Public key %s gives hash %d of index %d", pub_key2, hash_mel, num_hash);
answers[answers_count++] = num_hash - 1 + loop_count;
}
}
fclose(f_mel);
loop_count += size_chunk;
pk_count = 0;
fclose(f_pk);
free(hash_database);
free(hash_database2);
if(answers_count>0){
fprintf(stdout,"\nAnswer's indice:\n");
for(int j=0;j<answers_count;j++){
fprintf(stdout,"Index %d from %s is used\n", answers[j], NAME_FILE_PK);
}
}
else{
fprintf(stdout," No keys are found.");
}
}