]> git.sesse.net Git - freerainbowtables/blob - BOINC software/BOINC client apps/index_calculator/index_calculator.cpp
initial
[freerainbowtables] / BOINC software / BOINC client apps / index_calculator / index_calculator.cpp
1 // This file is part of BOINC.
2 // http://boinc.berkeley.edu
3 // Copyright (C) 2008 University of California
4 //
5 // BOINC is free software; you can redistribute it and/or modify it
6 // under the terms of the GNU Lesser General Public License
7 // as published by the Free Software Foundation,
8 // either version 3 of the License, or (at your option) any later version.
9 //
10 // BOINC is distributed in the hope that it will be useful,
11 // but WITHOUT ANY WARRANTY; without even the implied warranty of
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
13 // See the GNU Lesser General Public License for more details.
14 //
15 // You should have received a copy of the GNU Lesser General Public License
16 // along with BOINC.  If not, see <http://www.gnu.org/licenses/>.
17
18
19 #ifdef _WIN32
20 #include "boinc_win.h"
21 #else
22 #include "config.h"
23 #include <cstdio>
24 #include <cctype>
25 #include <ctime>
26 #include <cstring>
27 #include <cstdlib>
28 #include <csignal>
29 #include <unistd.h>
30 #endif
31
32 #include <string>
33 #include <fstream>
34
35 #include "str_util.h"
36 #include "util.h"
37 #include "filesys.h"
38 #include "boinc_api.h"
39 #include "Public.h"
40 // Rainbowcrack code
41 #include "ChainWalkSet.h"
42 #include "ChainWalkContext.h"
43 typedef unsigned int uint32;
44 //typedef unsigned __int64 uint64;
45
46
47 using std::string;
48
49 int main(int argc, char **argv) {
50     int i;
51     int c, nchars = 0, retval, n;
52     double fsize, fd;
53     char output_path[512], chkpt_path[512];
54     FILE* state, *infile;       
55     retval = boinc_init();
56     if (retval) {
57         fprintf(stderr, "boinc_init returned %d\n", retval);
58         exit(retval);
59     }
60         
61
62     // get size of input file (used to compute fraction done)
63     //
64     //file_size(input_path, fsize);
65
66     // See if there's a valid checkpoint file.
67     // If so seek input file and truncate output file
68     //
69
70
71         if(argc < 8)
72         {
73                 std::cerr << "Not enough parameters";
74                 return -1;
75         }
76         string sHashRoutineName, sCharsetName, sHash;
77         uint32 nRainbowChainCount, nPlainLenMin, nPlainLenMax, nRainbowTableIndex, nRainbowChainLen;
78         uint64 nChainStart;
79         sHashRoutineName = argv[1];
80         sCharsetName = argv[2];
81         nPlainLenMin = atoi(argv[3]);
82         nPlainLenMax = atoi(argv[4]);
83         nRainbowTableIndex = atoi(argv[5]);
84         nRainbowChainLen = atoi(argv[6]);
85         sHash = argv[7];
86         //std::cout << "Starting ChainGenerator" << std::endl;
87         // Setup CChainWalkContext
88         //std::cout << "ChainGenerator started." << std::endl;
89
90         if (!CChainWalkContext::SetHashRoutine(sHashRoutineName))
91         {
92                 std::cerr << "hash routine " << sHashRoutineName << " not supported" << std::endl;
93                 return 1;
94         }
95         //std::cout << "Hash routine validated" << std::endl;
96
97         if (!CChainWalkContext::SetPlainCharset(sCharsetName, nPlainLenMin, nPlainLenMax))
98         {       
99                 std::cerr << "charset " << sCharsetName << " not supported" << std::endl;
100                 return 2;
101         }
102         //std::cout << "Plain charset validated" << std::endl;
103
104         if (!CChainWalkContext::SetRainbowTableIndex(nRainbowTableIndex))
105         {
106                 std::cerr << "invalid rainbow table index " << nRainbowTableIndex << std::endl;
107                 return 3;
108         }
109         //std::cout << "Rainbowtable index validated" << std::endl;
110 /*
111         if(sHashRoutineName == "mscache")// || sHashRoutineName == "lmchall" || sHashRoutineName == "halflmchall")
112         {
113                 // Convert username to unicode
114                 const char *szSalt = sSalt.c_str();
115                 int salt_length = strlen(szSalt);
116                 unsigned char cur_salt[256];
117                 for (int i=0; i<salt_length; i++)
118                 {
119                         cur_salt[i*2] = szSalt[i];
120                         cur_salt[i*2+1] = 0x00;
121                 }
122                 CChainWalkContext::SetSalt(cur_salt, salt_length*2);
123         }*/
124         else if(sHashRoutineName == "halflmchall")
125         { // The salt is hardcoded into the hash routine
126         //      CChainWalkContext::SetSalt((unsigned char*)&salt, 8);
127         }
128         /*
129         else if(sHashRoutineName == "oracle")
130         {
131                 CChainWalkContext::SetSalt((unsigned char *)sSalt.c_str(), sSalt.length());
132         }
133         */
134         //std::cout << "Opening chain file" << std::endl;
135
136         
137         // Open file
138     boinc_resolve_filename("result", output_path, sizeof(output_path));
139         FILE *outfile = boinc_fopen(output_path, "wb");
140         
141         if (outfile == NULL)
142         {
143                 std::cerr << "failed to create " << output_path << std::endl;
144                 return 4;
145         }
146         unsigned char TargetHash[255];
147         int nHashLen = 0;
148         ParseHash(sHash, TargetHash, nHashLen);
149         CChainWalkSet m_cws;
150         CChainWalkContext cwc;
151
152         bool fNewlyGenerated;
153         uint64* pStartPosIndexE = m_cws.RequestWalk(TargetHash,
154                                                                                                 nHashLen,
155                                                                                                 sHashRoutineName,
156                                                                                                 sCharsetName,
157                                                                                                 nPlainLenMin,
158                                                                                                 nPlainLenMax,
159                                                                                                 nRainbowTableIndex,
160                                                                                                 nRainbowChainLen,
161                                                                                                 fNewlyGenerated);
162         int nPos;
163         double nTargetChains = pow((double)nRainbowChainLen - 2, 2) / 2 - (nRainbowChainLen - 2);
164         if (fNewlyGenerated)
165         {
166                 time_t tStart = time(NULL);
167                 for (nPos = nRainbowChainLen - 2; nPos >= 0; nPos--)
168                 {
169                         if(time(NULL) - tStart > 1)
170                         {
171                                 time(&tStart);
172                                 double nCurrentChains = pow(((double)nRainbowChainLen - 2 - (double)nPos), 2) / 2 - (nRainbowChainLen - 2);
173                                 double fResult = ((double)((double)(nCurrentChains) / nTargetChains));
174                                 if(fResult < 0)
175                                         fResult = 0;
176                                 boinc_fraction_done(fResult);
177                         }
178                         
179                         cwc.SetHash(TargetHash);
180                         cwc.HashToIndex(nPos);
181                         int i;
182                         for (i = nPos + 1; i <= nRainbowChainLen - 2; i++)
183                         {
184                                 cwc.IndexToPlain();
185                                 cwc.PlainToHash();
186                                 cwc.HashToIndex(i);
187                         }
188                         uint64 index = cwc.GetIndex();
189                         if(fwrite(&index, 8, 1, outfile) != 1)
190                         {
191                                 fprintf(stderr, "unable to write to outfile");
192                                 return 9;
193                         }
194                                 //nChainWalkStep += nRainbowChainLen - 2 - nPos;
195                 }
196 //                      printf("ok\n");                 
197                 double nCurrentChains = pow(((double)nRainbowChainLen - 2 - (double)nPos), 2) / 2 - (nRainbowChainLen - 2);
198                 double fResult = ((double)((double)(nCurrentChains) / nTargetChains));
199                 boinc_fraction_done(fResult);
200         }
201         
202         
203         fclose(outfile);
204     
205         // main loop - read characters, convert to UC, write
206     //
207
208     boinc_fraction_done(1);
209     boinc_finish(0);
210 }
211
212 #ifdef _WIN32
213 int WINAPI WinMain(HINSTANCE hInst, HINSTANCE hPrevInst, LPSTR Args, int WinMode) {
214     LPSTR command_line;
215     char* argv[100];
216     int argc;
217
218     command_line = GetCommandLine();
219     argc = parse_command_line( command_line, argv );
220     return main(argc, argv);
221 }
222 #endif
223
224 const char *BOINC_RCSID_33ac47a071 = "$Id: upper_case.C 12135 2007-02-21 20:04:14Z davea $";
225