-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscanner.cpp
More file actions
82 lines (70 loc) · 2.58 KB
/
Copy pathscanner.cpp
File metadata and controls
82 lines (70 loc) · 2.58 KB
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
81
82
#include "scanner.h"
#include "bip39_sequence.h"
#include "bip39_checksum.h"
#include "bip39_wordlist_std.h"
#include <QDirIterator>
#include <QFile>
#include <QFileInfo>
#include <QDir>
#include <QTextStream>
#include <QDateTime>
#include <QDebug>
Scanner::Scanner() {}
void Scanner::setWordlist(const std::vector<QString> &words) {
wordlist = words;
wordSet = Bip39Sequence::buildWordSet(words);
}
bool Scanner::containsBip39Word(const QString &text) {
for (const auto &word : wordlist) {
if (text.contains(word, Qt::CaseInsensitive))
return true;
}
return false;
}
void Scanner::scanDirectory(const QString &directoryPath,
const QString &outputPath,
std::function<void(const QString&)> onMatch,
std::function<void(int, int, const QString&)> onProgress,
std::function<bool()> shouldStop) {
QDirIterator it(directoryPath, QStringList() << "*.txt" << "*.ini",
QDir::Files, QDirIterator::Subdirectories);
QStringList allFiles;
while (it.hasNext()) {
allFiles << it.next();
}
int total = allFiles.size();
int current = 0;
QDir().mkpath(outputPath);
for (const auto &filePath : allFiles) {
if (shouldStop()) break;
current++;
onProgress(current, total, filePath); // Report progress and current file
QFile file(filePath);
if (!file.open(QIODevice::ReadOnly | QIODevice::Text))
continue;
QTextStream in(&file);
QString content = in.readAll();
// Quick pre-filter: any BIP-39 word present at all?
if (!containsBip39Word(content))
continue;
// Full filter: at least one valid-length sequence must pass checksum.
bool validated = false;
for (const Bip39Sequence::Match &seq : Bip39Sequence::extract(content, wordSet)) {
std::vector<std::string> stdWords;
stdWords.reserve(seq.words.size());
for (const QString &w : seq.words)
stdWords.push_back(w.toStdString());
if (BIP39Checksum::validate(stdWords, Bip39WordsStd::wordlist)) {
validated = true;
break;
}
}
if (validated) {
QString timeStamp = QDateTime::currentDateTime().toString("yyyyMMdd_HHmmss");
QFileInfo fi(filePath);
QString destPath = outputPath + "/" + timeStamp + "_" + fi.fileName();
QFile::copy(filePath, destPath);
onMatch(filePath);
}
}
}