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