Commit fefce76c authored by Elvis Angelaccio's avatar Elvis Angelaccio

Improve checksums calculation

Summary:
1. Compute also the MD5 in another thread (useful if the archive is huge)
2. Drop the member QByteArray. If the archive is huge this could be bad on
   systems with low amount of RAM. QCryptographicHash accepts a QIODevice, so we
   can just read the file three times.

Differential Revision: D2123
parent ffd58b00
......@@ -99,53 +99,41 @@ PropertiesDialog::PropertiesDialog(QWidget *parent, Archive *archive)
QIcon icon = QIcon::fromTheme(archive->mimeType().iconName());
m_ui->lblIcon->setPixmap(icon.pixmap(IconSize(KIconLoader::Desktop), IconSize(KIconLoader::Desktop)));
m_ui->lblMD5->setText(i18n("Calculating..."));
m_ui->lblSHA1->setText(i18n("Calculating..."));
m_ui->lblSHA256->setText(i18n("Calculating..."));
QFile file(archive->fileName());
if (file.open(QIODevice::ReadOnly)) {
m_byteArray = file.readAll();
file.close();
QCryptographicHash hashMD5(QCryptographicHash::Md5);
hashMD5.addData(m_byteArray);
m_ui->lblMD5->setText(QLatin1String(hashMD5.result().toHex()));
// The two SHA hashes take some time to calculate, so run them in another thread.
QFutureWatcher<QString> *watchCalcSha1 = new QFutureWatcher<QString>;
connect(watchCalcSha1, SIGNAL(finished()), this, SLOT(slotShowSha1()));
m_futureCalcSha1 = QtConcurrent::run(this, &PropertiesDialog::calcHash, QCryptographicHash::Sha1);
watchCalcSha1->setFuture(m_futureCalcSha1);
QFutureWatcher<QString> *watchCalcSha256 = new QFutureWatcher<QString>;
connect(watchCalcSha256, SIGNAL(finished()), this, SLOT(slotShowSha256()));
m_futureCalcSha256 = QtConcurrent::run(this, &PropertiesDialog::calcHash, QCryptographicHash::Sha256);
watchCalcSha256->setFuture(m_futureCalcSha256);
} else {
m_ui->lblMD5->setText(QString());
m_ui->lblSHA1->setText(QString());
m_ui->lblSHA256->setText(QString());
}
showChecksum(QCryptographicHash::Md5, archive->fileName(), m_ui->lblMD5);
showChecksum(QCryptographicHash::Sha1, archive->fileName(), m_ui->lblSHA1);
showChecksum(QCryptographicHash::Sha256, archive->fileName(), m_ui->lblSHA256);
connect(m_ui->buttonBox, &QDialogButtonBox::accepted, this, &QDialog::accept);
}
QString PropertiesDialog::calcHash(QCryptographicHash::Algorithm algorithm)
QString PropertiesDialog::calcHash(QCryptographicHash::Algorithm algorithm, const QString &path)
{
QCryptographicHash hash(algorithm);
hash.addData(m_byteArray);
QFile file(path);
if (!file.open(QIODevice::ReadOnly)) {
return QString();
}
return QLatin1String(hash.result().toHex());
}
QCryptographicHash hash(algorithm);
hash.addData(&file);
void PropertiesDialog::slotShowSha1()
{
m_ui->lblSHA1->setText(m_futureCalcSha1.result());
return QString::fromLatin1(hash.result().toHex());
}
void PropertiesDialog::slotShowSha256()
void PropertiesDialog::showChecksum(QCryptographicHash::Algorithm algorithm, const QString &fileName, QLabel *label)
{
m_ui->lblSHA256->setText(m_futureCalcSha256.result());
// Calculate checksum in another thread.
auto futureWatcher = new QFutureWatcher<QString>(this);
connect(futureWatcher, &QFutureWatcher<QString>::finished, this, [=]() {
label->setText(futureWatcher->result());
futureWatcher->deleteLater();
});
auto future = QtConcurrent::run(this, &PropertiesDialog::calcHash, algorithm, fileName);
futureWatcher->setFuture(future);
}
}
......@@ -32,7 +32,8 @@
#include <QCryptographicHash>
#include <QDialog>
#include <QFuture>
class QLabel;
namespace Kerfuffle
{
......@@ -43,17 +44,11 @@ class KERFUFFLE_EXPORT PropertiesDialog : public QDialog
public:
explicit PropertiesDialog(QWidget *parent, Archive *archive);
private slots:
void slotShowSha1();
void slotShowSha256();
private:
QString calcHash(QCryptographicHash::Algorithm algorithm);
QString calcHash(QCryptographicHash::Algorithm algorithm, const QString &path);
void showChecksum(QCryptographicHash::Algorithm algorithm, const QString &fileName, QLabel *label);
class PropertiesDialogUI *m_ui;
QByteArray m_byteArray;
QFuture<QString> m_futureCalcSha1;
QFuture<QString> m_futureCalcSha256;
};
}
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment