Skip to content

Commit 74ec2ad

Browse files
fixed a bug that caused it to send the wrong file size plus changed the way file hash gets sent from an actual hash to just file size for now for pure speed. Will see to change it in the future.
1 parent db79d30 commit 74ec2ad

1 file changed

Lines changed: 44 additions & 9 deletions

File tree

mainwindow.cpp

Lines changed: 44 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -90,16 +90,44 @@ QString fileChecksum(const QString &fileName, QCryptographicHash::Algorithm hash
9090
return "";
9191
}
9292

93+
qint64 fileSize = file.size();
9394
QCryptographicHash hash(hashAlgorithm);
94-
if (hash.addData(&file))
95-
{
96-
return hash.result().toBase64();
97-
}
98-
else
95+
96+
// 1. Always hash the exact file size first to prevent collisions
97+
// between files of different sizes that share the same sampled bytes.
98+
hash.addData(QByteArray::number(fileSize));
99+
100+
// Set our sample size to 1 Megabyte
101+
const qint64 sampleSize = 1024 * 1024;
102+
103+
// 2. If the file is 3MB or smaller, it's faster to just hash the whole thing normally.
104+
if (fileSize <= sampleSize * 3)
99105
{
100-
qDebug() << "Failed to add file data to hash engine.";
101-
return "";
106+
if (hash.addData(&file)) {
107+
return hash.result().toBase64();
108+
} else {
109+
qDebug() << "Failed to add file data to hash engine.";
110+
return "";
111+
}
102112
}
113+
114+
// 3. --- THE CHEAT CODE: Partial Hashing for Big Files ---
115+
116+
// Read the first 1MB
117+
file.seek(0);
118+
hash.addData(file.read(sampleSize));
119+
120+
// Read the middle 1MB
121+
qint64 middleOffset = (fileSize / 2) - (sampleSize / 2);
122+
file.seek(middleOffset);
123+
hash.addData(file.read(sampleSize));
124+
125+
// Read the last 1MB
126+
qint64 endOffset = fileSize - sampleSize;
127+
file.seek(endOffset);
128+
hash.addData(file.read(sampleSize));
129+
130+
return hash.result().toBase64();
103131
}
104132

105133
QByteArray MainWindow::sendEncryptionRequest()
@@ -658,7 +686,8 @@ MainWindow::MainWindow(QWidget *parent)
658686

659687
if (checkFile.exists())
660688
{
661-
QString localHash = fileChecksum(fullLocalPath, QCryptographicHash::Sha256);
689+
QFileInfo info(fullLocalPath);
690+
QString localHash = QString::number(info.size());
662691
if (localHash == remoteHash)
663692
{
664693
skipList.append(relPath); // It matches! Tell sender to skip it.
@@ -1546,11 +1575,15 @@ void MainWindow::sendDirectories()
15461575
QJsonArray folderPaths;
15471576
QVector<QString> filesToSend;
15481577

1578+
int current_file = 0;
1579+
15491580
// --- BATCHING SETUP ---
15501581
QJsonArray manifestArray;
15511582

15521583
while (itfolders.hasNext())
15531584
{
1585+
QCoreApplication::processEvents();
1586+
15541587
auto entity = itfolders.nextFileInfo();
15551588

15561589
if (entity.isDir())
@@ -1577,8 +1610,10 @@ void MainWindow::sendDirectories()
15771610
fileInfo["path"] = relativePath;
15781611

15791612
// NOTE: You can use fileChecksum here, or info.size() + info.lastModified() for even more speed!
1580-
fileInfo["hash"] = fileChecksum(filePath, QCryptographicHash::Sha256);
1613+
fileInfo["hash"] = QString::number(entity.size());
15811614
manifestArray.append(fileInfo);
1615+
1616+
ui->statusbar->showMessage("Computing Hash for: " + entity.fileName());
15821617
}
15831618
}
15841619

0 commit comments

Comments
 (0)