Skip to content

Commit 98a4342

Browse files
Added ecryption to the new features that used to expose the file name / path in the check message to make sure a file does not get overwritten.
1 parent 74ec2ad commit 98a4342

2 files changed

Lines changed: 85 additions & 17 deletions

File tree

filemessage.ui

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
<x>0</x>
88
<y>0</y>
99
<width>594</width>
10-
<height>119</height>
10+
<height>134</height>
1111
</rect>
1212
</property>
1313
<property name="sizePolicy">
@@ -57,16 +57,6 @@
5757
<string/>
5858
</property>
5959
<layout class="QGridLayout" name="gridLayout">
60-
<item row="0" column="1">
61-
<widget class="QLabel" name="statusLable">
62-
<property name="text">
63-
<string>Transfering</string>
64-
</property>
65-
<property name="alignment">
66-
<set>Qt::AlignmentFlag::AlignCenter</set>
67-
</property>
68-
</widget>
69-
</item>
7060
<item row="1" column="0">
7161
<widget class="QLabel" name="fromLable">
7262
<property name="text">
@@ -97,6 +87,19 @@
9787
</property>
9888
</widget>
9989
</item>
90+
<item row="0" column="0" colspan="3">
91+
<widget class="QLabel" name="statusLable">
92+
<property name="text">
93+
<string>Transfering</string>
94+
</property>
95+
<property name="alignment">
96+
<set>Qt::AlignmentFlag::AlignCenter</set>
97+
</property>
98+
<property name="wordWrap">
99+
<bool>true</bool>
100+
</property>
101+
</widget>
102+
</item>
100103
</layout>
101104
</widget>
102105
</item>

mainwindow.cpp

Lines changed: 71 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -339,8 +339,8 @@ MainWindow::MainWindow(QWidget *parent)
339339
"Transferring: (" + formatByteSpeed(current_file_size) +
340340
" / " + formatByteSpeed(current_total_file_size) +
341341
") @ " + formatByteSpeed(bytesReceivedThisSecond * 10) + "/s" +
342-
(filesToRecieve > 0? " Files Left: " + QString::number(filesToRecieve) : "")
343-
);
342+
(filesToRecieve > 0? " Files Left: " + QString::number(filesToRecieve) : "") +
343+
(fileQueue.length() > 0? " Files Left: " + QString::number(fileQueue.length()) : ""));
344344
bytesReceivedThisSecond = 0;
345345
});
346346

@@ -681,6 +681,15 @@ MainWindow::MainWindow(QWidget *parent)
681681
QString relPath = fileObj["path"].toString();
682682
QString remoteHash = fileObj["hash"].toString();
683683

684+
if (ui->ActivateEncryption->isChecked())
685+
{
686+
if (recCipher == nullptr) return;
687+
688+
QByteArray relpathArray = QByteArray::fromBase64(relPath.toUtf8());
689+
recCipher->process(relpathArray);
690+
relPath = QString::fromUtf8(relpathArray);
691+
}
692+
684693
QString fullLocalPath = rootPath + relPath;
685694
QFileInfo checkFile(fullLocalPath);
686695

@@ -690,6 +699,15 @@ MainWindow::MainWindow(QWidget *parent)
690699
QString localHash = QString::number(info.size());
691700
if (localHash == remoteHash)
692701
{
702+
if (ui->ActivateEncryption->isChecked())
703+
{
704+
if (sendCipher == nullptr) return;
705+
706+
QByteArray relPathArray = relPath.toUtf8();
707+
sendCipher->process(relPathArray);
708+
relPath = relPathArray.toBase64();
709+
}
710+
693711
skipList.append(relPath); // It matches! Tell sender to skip it.
694712
filesToRecieve--; // Lower the expected file count
695713
}
@@ -859,6 +877,19 @@ MainWindow::MainWindow(QWidget *parent)
859877
QString filePath = obj["file_path"].toString();
860878
QString remoteHash = obj["hash"].toString();
861879

880+
if (ui->ActivateEncryption->isChecked())
881+
{
882+
if (recCipher == nullptr) return;
883+
QByteArray filePathArray = QByteArray::fromBase64(filePath.toUtf8());
884+
QByteArray remoteHashArray = QByteArray::fromBase64(remoteHash.toUtf8());
885+
886+
recCipher->process(filePathArray);
887+
recCipher->process(remoteHashArray);
888+
889+
filePath = QString::fromUtf8(filePathArray);
890+
remoteHash = QString::fromUtf8(remoteHashArray);
891+
}
892+
862893
// Determine where the file WOULD be saved based on current state
863894
QString fullLocalPath;
864895
if (sendingFolder) {
@@ -944,10 +975,20 @@ MainWindow::MainWindow(QWidget *parent)
944975
{
945976
QJsonArray skipList = obj["skip_list"].toArray();
946977

947-
// Convert the JSON array to a quick lookup list
948978
QStringList filesToSkip;
949979
for (int i = 0; i < skipList.size(); ++i) {
950-
filesToSkip.append(skipList[i].toString());
980+
QString fileToSkip = skipList[i].toString();
981+
982+
if (ui->ActivateEncryption->isChecked())
983+
{
984+
if (recCipher == nullptr) return;
985+
QByteArray fileToSkipArray = QByteArray::fromBase64(fileToSkip.toUtf8());
986+
recCipher->process(fileToSkipArray);
987+
988+
fileToSkip = QString::fromUtf8(fileToSkipArray);
989+
}
990+
991+
filesToSkip.append(fileToSkip);
951992
}
952993

953994
// Filter our fileQueue
@@ -1323,10 +1364,26 @@ void MainWindow::sendFile(bool runCheck = true)
13231364
{
13241365
QJsonObject jsonMessage;
13251366
jsonMessage["type"] = "file_check";
1326-
jsonMessage["file_path"] = fileName;
1327-
jsonMessage["hash"] = fileChecksum(selectedFilePath, QCryptographicHash::Sha256);
13281367
jsonMessage["to"] = pairPartnerId;
13291368

1369+
if (ui->ActivateEncryption->isChecked())
1370+
{
1371+
QByteArray fileNameArray = fileName.toUtf8();
1372+
QByteArray hashArray = fileChecksum(selectedFilePath, QCryptographicHash::Sha256).toUtf8();
1373+
1374+
if (sendCipher == nullptr) return;
1375+
sendCipher->process(fileNameArray);
1376+
sendCipher->process(hashArray);
1377+
1378+
jsonMessage["file_path"] = QString(fileNameArray.toBase64());
1379+
jsonMessage["hash"] = QString(hashArray.toBase64());
1380+
}
1381+
else
1382+
{
1383+
jsonMessage["file_path"] = fileName;
1384+
jsonMessage["hash"] = fileChecksum(selectedFilePath, QCryptographicHash::Sha256);
1385+
}
1386+
13301387
QJsonDocument doc(jsonMessage);
13311388
QString jsonString = doc.toJson(QJsonDocument::Compact) + '\n';
13321389
socket->write(jsonString.toUtf8());
@@ -1606,6 +1663,14 @@ void MainWindow::sendDirectories()
16061663
// Calculate the hash (or metadata) right now
16071664
QString relativePath = QString(filePath).replace(selectedFolderPath, "");
16081665

1666+
if (ui->ActivateEncryption->isChecked())
1667+
{
1668+
if (sendCipher == nullptr) return;
1669+
QByteArray relativePathArray = relativePath.toUtf8();
1670+
sendCipher->process(relativePathArray);
1671+
relativePath = relativePathArray.toBase64();
1672+
}
1673+
16091674
QJsonObject fileInfo;
16101675
fileInfo["path"] = relativePath;
16111676

0 commit comments

Comments
 (0)