]> git.sesse.net Git - freerainbowtables/blob - Client Applications/converti2/Public.cpp
e279c56a349a0c9b0ae61e4bf7f039115dc1f6ab
[freerainbowtables] / Client Applications / converti2 / Public.cpp
1 /*
2    RainbowCrack - a general propose implementation of Philippe Oechslin's faster time-memory trade-off technique.
3
4    Copyright (C) Zhu Shuanglei <shuanglei@hotmail.com>
5 */
6
7 #ifdef _WIN32
8         #pragma warning(disable : 4786)
9 #endif
10
11 #ifdef _WIN32
12
13 #else
14 #include <cstdio>
15 #include <cctype>
16 #include <ctime>
17 #include <cstring>
18 #include <cstdlib>
19 #include <csignal>
20 #include <unistd.h>
21
22 #endif
23
24 #include "Public.h"
25
26 #ifdef _WIN32
27         #include <windows.h>
28 #else
29         #include <sys/sysinfo.h>
30 #endif
31
32 //////////////////////////////////////////////////////////////////////
33
34 unsigned int GetFileLen(FILE* file)
35 {
36         unsigned int pos = ftell(file);
37         fseek(file, 0, SEEK_END);
38         unsigned int len = ftell(file);
39         fseek(file, pos, SEEK_SET);
40
41         return len;
42 }
43
44 string TrimString(string s)
45 {
46         while (s.size() > 0)
47         {
48                 if (s[0] == ' ' || s[0] == '\t')
49                         s = s.substr(1);
50                 else
51                         break;
52         }
53
54         while (s.size() > 0)
55         {
56                 if (s[s.size() - 1] == ' ' || s[s.size() - 1] == '\t')
57                         s = s.substr(0, s.size() - 1);
58                 else
59                         break;
60         }
61
62         return s;
63 }
64 bool GetHybridCharsets(string sCharset, vector<tCharset>& vCharset)
65 {
66         // Example: hybrid(mixalpha-numeric-all-space#1-6,numeric#1-4)
67         if(sCharset.substr(0, 6) != "hybrid") // Not hybrid charset
68                 return false;
69         size_t nEnd = sCharset.rfind(')');
70         size_t nStart = sCharset.rfind('(');
71         string sChar = sCharset.substr(nStart + 1, nEnd - nStart - 1);
72         vector<string> vParts;
73         SeperateString(sChar, ",", vParts);
74         for(int i = 0; i < vParts.size(); i++)
75         {
76                 tCharset stCharset;
77                 vector<string> vParts2;
78                 SeperateString(vParts[i], "#", vParts2);
79                 stCharset.sName = vParts2[0];
80                 vector<string> vParts3;
81                 SeperateString(vParts2[1], "-", vParts3);
82                 stCharset.nPlainLenMin = atoi(vParts3[0].c_str());
83                 stCharset.nPlainLenMax = atoi(vParts3[1].c_str());
84                 vCharset.push_back(stCharset);
85         }
86         return true;
87 }
88 bool ReadLinesFromFile(string sPathName, vector<string>& vLine)
89 {
90         vLine.clear();
91     FILE *file = fopen(sPathName.c_str(), "rb");
92         if (file != NULL)
93         {
94                 unsigned int len = GetFileLen(file);
95                 char* data = new char[len + 1];
96                 fread(data, 1, len, file);
97                 data[len] = '\0';
98                 string content = data;
99                 content += "\n";
100                 delete data;
101
102                 unsigned int i;
103                 for (i = 0; i < content.size(); i++)
104                 {
105                         if (content[i] == '\r')
106                                 content[i] = '\n';
107                 }
108
109                 int n;
110                 while ((n = content.find("\n", 0)) != -1)
111                 {
112                         string line = content.substr(0, n);
113                         line = TrimString(line);
114                         if (line != "")
115                                 vLine.push_back(line);
116                         content = content.substr(n + 1);
117                 }
118
119                 fclose(file);
120         }
121         else
122                 return false;
123
124         return true;
125 }
126
127 bool SeperateString(string s, string sSeperator, vector<string>& vPart)
128 {
129         vPart.clear();
130
131         unsigned int i;
132         for (i = 0; i < sSeperator.size(); i++)
133         {
134                 int n = s.find(sSeperator[i]);
135                 if (n != -1)
136                 {
137                         vPart.push_back(s.substr(0, n));
138                         s = s.substr(n + 1);
139                 }
140                 else
141                         return false;
142         }
143         vPart.push_back(s);
144
145         return true;
146 }
147
148 string uint64tostr(uint64 n)
149 {
150         char str[32];
151
152 #ifdef _WIN32
153         sprintf(str, "%I64u", n);
154 #else
155         sprintf(str, "%llu", n);
156 #endif
157
158         return str;
159 }
160
161 string uint64tohexstr(uint64 n)
162 {
163         char str[32];
164
165 #ifdef _WIN32
166         sprintf(str, "%016I64x", n);
167 #else
168         sprintf(str, "%016llx", n);
169 #endif
170
171         return str;
172 }
173
174 string HexToStr(const unsigned char* pData, int nLen)
175 {
176         string sRet;
177         int i;
178         for (i = 0; i < nLen; i++)
179         {
180                 char szByte[3];
181                 sprintf(szByte, "%02x", pData[i]);
182                 sRet += szByte;
183         }
184
185         return sRet;
186 }
187
188 unsigned int GetAvailPhysMemorySize()
189 {
190 #ifdef _WIN32
191                 MEMORYSTATUS ms;
192                 GlobalMemoryStatus(&ms);
193                 return ms.dwAvailPhys;
194 #else
195         struct sysinfo info;
196         sysinfo(&info);                 // This function is Linux-specific
197         return info.freeram;
198 #endif
199 }
200
201 void ParseHash(string sHash, unsigned char* pHash, int& nHashLen)
202 {
203         int i;
204         for (i = 0; i < sHash.size() / 2; i++)
205         {
206                 string sSub = sHash.substr(i * 2, 2);
207                 int nValue;
208                 sscanf(sSub.c_str(), "%02x", &nValue);
209                 pHash[i] = (unsigned char)nValue;
210         }
211
212         nHashLen = sHash.size() / 2;
213 }
214
215 void Logo()
216 {
217         printf("RainbowCrack (improved) 2.0 - Making a Faster Cryptanalytic Time-Memory Trade-Off\n");
218         printf("by Martin Westergaard <martinwj2005@gmail.com>\n");
219         printf("http://www.freerainbowtables.com/\n");
220         printf("original code by Zhu Shuanglei <shuanglei@hotmail.com>\n");
221         printf("http://www.antsight.com/zsl/rainbowcrack/\n\n");
222 }