forked from defuse/crackstation-hashdb
-
Notifications
You must be signed in to change notification settings - Fork 0
/
search.cpp
77 lines (58 loc) · 1.76 KB
/
search.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
#include "search.h"
Match::Match( const HashLib::Hash & hash, const std::string & word, bool fullMatch ) :
hash( hash ),
word( word ),
fullMatch( fullMatch ) {}
std::vector<Match> Match::getMatches( std::ifstream & wordlist, FileArray & index, HashLib * hashAlgorithm, const HashLib::Hash & hash ) {
std::vector<Match> matches;
HashLib::Hash comparisionHash;
FileArray::IndexEntry searchElement;
FileArray::IndexEntry centerElement;
size_t lower = 0;
size_t upper = index.getSize() - 1;
size_t middle = 0;
std::string line;
const size_t hashSize( hash.getLength() );
searchElement.setHash( hash );
while ( lower < upper ) {
middle = lower + (upper - lower) / 2;
index.readEntry( centerElement, middle );
if ( centerElement < searchElement )
lower = middle + 1;
else
upper = middle;
}
while ( lower < index.getSize() ) {
index.readEntry( centerElement, lower++ );
if ( centerElement != searchElement )
break;
wordlist.seekg( centerElement.getOffset() );
getline( wordlist, line );
comparisionHash = hashAlgorithm->hash( line );
if ( hashSize == comparisionHash.getLength() ) {
if ( hash == comparisionHash )
matches.push_back( Match( hash, line, true ) );
} else {
if ( hash.partialMatch( comparisionHash ) )
matches.push_back( Match( comparisionHash, line, false ) );
}
}
matches.shrink_to_fit();
return matches;
}
std::string Match::toString() const {
return hash.toString() + ": " + word + (fullMatch ? "" : " [partial]");
}
const HashLib::Hash & Match::getHash() const {
return hash;
}
const std::string & Match::getWord() const {
return word;
}
bool Match::isFullMatch() const {
return fullMatch;
}
std::ostream & operator<<( std::ostream & rhs, const Match & lhs ) {
rhs << lhs.toString();
return rhs;
}