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