forked from defuse/crackstation-hashdb
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsearch.h
80 lines (60 loc) · 2.98 KB
/
search.h
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
78
79
80
#ifndef CRACKSTATION_SEARCH_H
#define CRACKSTATION_SEARCH_H
#include <fstream>
#include <ostream>
#include <string>
#include <type_traits>
#include <vector>
#include "filearray.h"
#include "hashlib.h"
class Match {
public:
Match( const HashLib::Hash & hash, const std::string & word, bool fullMatch );
// Helper overloads
template<class T1, class T2, class T3>
inline static std::vector<Match> getMatches( const std::string & wordlist, T1 & index, const T2 & hashAlgorithm, const T3 & hash );
template<class T1, class T2>
inline static std::vector<Match> getMatches( std::ifstream & wordlist, const std::string & index, const T1 & hashAlgorithm, const T2 & hash );
template<class T1>
inline static std::vector<Match> getMatches( std::ifstream & wordlist, FileArray & index, const std::string & hashAlgorithm, const T1 & hash );
template<class T1>
inline static std::vector<Match> getMatches( std::ifstream & wordlist, FileArray & index, const std::unique_ptr<HashLib> & hashAlgorithm, const T1 & hash );
inline static std::vector<Match> getMatches( std::ifstream & wordlist, FileArray & index, HashLib * hashAlgorithm, const std::string & hash );
static std::vector<Match> getMatches( std::ifstream & wordlist, FileArray & index, HashLib * hashAlgorithm, const HashLib::Hash & hash );
std::string toString() const;
const HashLib::Hash & getHash() const;
const std::string & getWord() const;
bool isFullMatch() const;
friend std::ostream & operator<<( std::ostream & rhs, const Match & lhs );
private:
HashLib::Hash hash;
std::string word;
bool fullMatch;
};
template<class T1, class T2, class T3>
std::vector<Match> Match::getMatches( const std::string & wordlist, T1 & index, const T2 & hashAlgorithm, const T3 & hash ) {
std::ifstream wordlistFile( wordlist, std::ios::in );
if ( !wordlistFile.good() )
throw std::invalid_argument( "File \"" + wordlist + "\" does not exist!" );
return getMatches( wordlistFile, index, hashAlgorithm, hash );
}
template<class T1, class T2>
std::vector<Match> Match::getMatches( std::ifstream & wordlist, const std::string & index, const T1 & hashAlgorithm, const T2 & hash ) {
FileArray fileArray( index );
return getMatches( wordlist, fileArray, hashAlgorithm, hash );
}
template<class T1>
std::vector<Match> Match::getMatches( std::ifstream & wordlist, FileArray & index, const std::string & hashAlgorithm, const T1 & hash ) {
const std::unique_ptr<HashLib> hasher( HashLib::getHasher( hashAlgorithm ) );
return getMatches( wordlist, index, hasher, hash );
}
template<class T1>
std::vector<Match> Match::getMatches( std::ifstream & wordlist, FileArray & index, const std::unique_ptr<HashLib> & hashAlgorithm, const T1 & hash ) {
return getMatches( wordlist, index, hashAlgorithm.get(), hash );
}
std::vector<Match> Match::getMatches( std::ifstream & wordlist, FileArray & index, HashLib * hashAlgorithm, const std::string & hash ) {
HashLib::Hash realHash;
realHash.fromString( hash );
return getMatches( wordlist, index, hashAlgorithm, realHash );
}
#endif