]> git.sesse.net Git - freerainbowtables/blob - Common/rt api/ChainWalkSet.cpp
a081eb8279c4d386f28008974a1a6feec3d208ea
[freerainbowtables] / Common / rt api / ChainWalkSet.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 "ChainWalkSet.h"
12
13 CChainWalkSet::CChainWalkSet()
14 {
15         m_sHashRoutineName   = "";
16         m_sPlainCharsetName  = "";
17         m_nPlainLenMin       = 0;
18         m_nPlainLenMax       = 0;
19         m_nRainbowTableIndex = 0;
20         m_nRainbowChainLen   = 0;
21 }
22
23 CChainWalkSet::~CChainWalkSet()
24 {
25         DiscardAll();
26 }
27
28 void CChainWalkSet::DiscardAll()
29 {
30         //printf("debug: discarding all walk...\n");
31
32         list<ChainWalk>::iterator it;
33         for (it = m_lChainWalk.begin(); it != m_lChainWalk.end(); it++)
34                 delete it->pIndexE;
35         m_lChainWalk.clear();
36 }
37
38 uint64* CChainWalkSet::RequestWalk(unsigned char* pHash, int nHashLen,
39                                                                    string sHashRoutineName,
40                                                                    string sPlainCharsetName, int nPlainLenMin, int nPlainLenMax, 
41                                                                    int nRainbowTableIndex, 
42                                                                    int nRainbowChainLen,
43                                                                    bool& fNewlyGenerated)
44 {
45         if (   m_sHashRoutineName   != sHashRoutineName
46                 || m_sPlainCharsetName  != sPlainCharsetName
47                 || m_nPlainLenMin       != nPlainLenMin
48                 || m_nPlainLenMax       != nPlainLenMax
49                 || m_nRainbowTableIndex != nRainbowTableIndex
50                 || m_nRainbowChainLen   != nRainbowChainLen)
51         {
52                 DiscardAll();
53
54                 m_sHashRoutineName   = sHashRoutineName;
55                 m_sPlainCharsetName  = sPlainCharsetName;
56                 m_nPlainLenMin       = nPlainLenMin;
57                 m_nPlainLenMax       = nPlainLenMax;
58                 m_nRainbowTableIndex = nRainbowTableIndex;
59                 m_nRainbowChainLen   = nRainbowChainLen;
60
61                 ChainWalk cw;
62                 memcpy(cw.Hash, pHash, nHashLen);
63                 cw.pIndexE = new uint64[nRainbowChainLen - 1];
64                 m_lChainWalk.push_back(cw);
65
66                 fNewlyGenerated = true;
67                 return cw.pIndexE;
68         }
69
70         list<ChainWalk>::iterator it;
71         for (it = m_lChainWalk.begin(); it != m_lChainWalk.end(); it++)
72         {
73                 if (memcmp(it->Hash, pHash, nHashLen) == 0)
74                 {
75                         fNewlyGenerated = false;
76                         return it->pIndexE;
77                 }
78         }
79
80         ChainWalk cw;
81         memcpy(cw.Hash, pHash, nHashLen);
82         cw.pIndexE = new uint64[nRainbowChainLen - 1];
83         m_lChainWalk.push_back(cw);
84
85         fNewlyGenerated = true;
86         return cw.pIndexE;
87 }
88
89 void CChainWalkSet::DiscardWalk(uint64* pIndexE)
90 {
91         list<ChainWalk>::iterator it;
92         for (it = m_lChainWalk.begin(); it != m_lChainWalk.end(); it++)
93         {
94                 if (it->pIndexE == pIndexE)
95                 {
96                         delete it->pIndexE;
97                         m_lChainWalk.erase(it);
98                         return;
99                 }
100         }
101
102         printf("debug: pIndexE not found\n");
103 }