Skip to content

Commit

Permalink
[Properties] Add SHA512 algorithm to checksums widget
Browse files Browse the repository at this point in the history
BUG: 423739
FIXED-IN: 5.73
  • Loading branch information
realGWM authored and Pointedstick committed Jul 13, 2020
1 parent 07043be commit 9cb68b5
Show file tree
Hide file tree
Showing 3 changed files with 92 additions and 8 deletions.
61 changes: 53 additions & 8 deletions src/widgets/checksumswidget.ui
Expand Up @@ -22,11 +22,11 @@
</widget>
</item>
<item>
<layout class="QHBoxLayout" name="horizontalLayout_4">
<layout class="QHBoxLayout" name="horizontalLayout">
<item>
<widget class="QLineEdit" name="lineEdit">
<property name="placeholderText">
<string extracomment="MD5, SHA1 and SHA256 are hashing algorithms">Expected checksum (MD5, SHA1 or SHA256)...</string>
<string extracomment="MD5, SHA1, SHA256 and SHA512 are hashing algorithms">Expected checksum (MD5, SHA1, SHA256 or SHA512)...</string>
</property>
</widget>
</item>
Expand Down Expand Up @@ -58,30 +58,37 @@
</item>
<item>
<widget class="QWidget" name="calculateWidget" native="true">
<layout class="QFormLayout" name="formLayout_2">
<layout class="QFormLayout" name="formLayout">
<item row="0" column="0">
<widget class="QLabel" name="label_2">
<widget class="QLabel" name="md5Label">
<property name="text">
<string extracomment="MD5 is the hashing algorithm">MD5:</string>
</property>
</widget>
</item>
<item row="1" column="0">
<widget class="QLabel" name="label_3">
<widget class="QLabel" name="sha1Label">
<property name="text">
<string extracomment="SHA1 is the hashing algorithm">SHA1:</string>
</property>
</widget>
</item>
<item row="2" column="0">
<widget class="QLabel" name="label_4">
<widget class="QLabel" name="sha256Label">
<property name="text">
<string extracomment="SHA256 is the hashing algorithm">SHA256:</string>
</property>
</widget>
</item>
<item row="3" column="0">
<widget class="QLabel" name="sha512Label">
<property name="text">
<string extracomment="SHA512 is the hashing algorithm">SHA512:</string>
</property>
</widget>
</item>
<item row="0" column="1">
<layout class="QHBoxLayout" name="horizontalLayout">
<layout class="QHBoxLayout" name="horizontalLayout_1">
<item>
<widget class="QPushButton" name="md5Button">
<property name="text">
Expand All @@ -90,7 +97,7 @@
</widget>
</item>
<item>
<spacer name="horizontalSpacer">
<spacer name="horizontalSpacer_1">
<property name="orientation">
<enum>Qt::Horizontal</enum>
</property>
Expand Down Expand Up @@ -194,6 +201,44 @@
</item>
</layout>
</item>
<item row="3" column="1">
<layout class="QHBoxLayout" name="horizontalLayout_4">
<item>
<widget class="QPushButton" name="sha512Button">
<property name="text">
<string>Calculate</string>
</property>
</widget>
</item>
<item>
<spacer name="horizontalSpacer_4">
<property name="orientation">
<enum>Qt::Horizontal</enum>
</property>
<property name="sizeHint" stdset="0">
<size>
<width>40</width>
<height>20</height>
</size>
</property>
</spacer>
</item>
<item>
<widget class="QPushButton" name="sha512CopyButton">
<property name="toolTip">
<string>Click to copy the checksum to the clipboard.</string>
</property>
<property name="text">
<string>Copy</string>
</property>
<property name="icon">
<iconset theme="edit-copy">
<normaloff>.</normaloff>.</iconset>
</property>
</widget>
</item>
</layout>
</item>
</layout>
</widget>
</item>
Expand Down
37 changes: 37 additions & 0 deletions src/widgets/kpropertiesdialog.cpp
Expand Up @@ -2767,6 +2767,7 @@ class KChecksumsPlugin::KChecksumsPluginPrivate
QString m_md5;
QString m_sha1;
QString m_sha256;
QString m_sha512;
};

KChecksumsPlugin::KChecksumsPlugin(KPropertiesDialog *dialog)
Expand All @@ -2778,6 +2779,7 @@ KChecksumsPlugin::KChecksumsPlugin(KPropertiesDialog *dialog)
d->m_ui.md5CopyButton->hide();
d->m_ui.sha1CopyButton->hide();
d->m_ui.sha256CopyButton->hide();
d->m_ui.sha512CopyButton->hide();

connect(d->m_ui.lineEdit, &QLineEdit::textChanged, this, [=](const QString &text) {
slotVerifyChecksum(text.toLower());
Expand All @@ -2786,6 +2788,7 @@ KChecksumsPlugin::KChecksumsPlugin(KPropertiesDialog *dialog)
connect(d->m_ui.md5Button, &QPushButton::clicked, this, &KChecksumsPlugin::slotShowMd5);
connect(d->m_ui.sha1Button, &QPushButton::clicked, this, &KChecksumsPlugin::slotShowSha1);
connect(d->m_ui.sha256Button, &QPushButton::clicked, this, &KChecksumsPlugin::slotShowSha256);
connect(d->m_ui.sha512Button, &QPushButton::clicked, this, &KChecksumsPlugin::slotShowSha512);

d->fileWatcher.addPath(properties->item().localPath());
connect(&d->fileWatcher, &QFileSystemWatcher::fileChanged, this, &KChecksumsPlugin::slotInvalidateCache);
Expand All @@ -2803,6 +2806,10 @@ KChecksumsPlugin::KChecksumsPlugin(KPropertiesDialog *dialog)
clipboard->setText(d->m_sha256);
});

connect(d->m_ui.sha512CopyButton, &QPushButton::clicked, this, [=]() {
clipboard->setText(d->m_sha512);
});

connect(d->m_ui.pasteButton, &QPushButton::clicked, this, [=]() {
d->m_ui.lineEdit->setText(clipboard->text());
});
Expand Down Expand Up @@ -2830,6 +2837,7 @@ void KChecksumsPlugin::slotInvalidateCache()
d->m_md5 = QString();
d->m_sha1 = QString();
d->m_sha256 = QString();
d->m_sha512 = QString();
}

void KChecksumsPlugin::slotShowMd5()
Expand Down Expand Up @@ -2865,6 +2873,17 @@ void KChecksumsPlugin::slotShowSha256()
showChecksum(QCryptographicHash::Sha256, label, d->m_ui.sha256CopyButton);
}

void KChecksumsPlugin::slotShowSha512()
{
auto label = new QLabel(i18nc("@action:button", "Calculating..."), &d->m_widget);
label->setTextInteractionFlags(Qt::TextSelectableByMouse | Qt::TextSelectableByKeyboard);

d->m_ui.calculateWidget->layout()->replaceWidget(d->m_ui.sha512Button, label);
d->m_ui.sha512Button->hide();

showChecksum(QCryptographicHash::Sha512, label, d->m_ui.sha512CopyButton);
}

void KChecksumsPlugin::slotVerifyChecksum(const QString &input)
{
auto algorithm = detectAlgorithm(input);
Expand Down Expand Up @@ -2912,6 +2931,9 @@ void KChecksumsPlugin::slotVerifyChecksum(const QString &input)
case QCryptographicHash::Sha256:
slotShowSha256();
break;
case QCryptographicHash::Sha512:
slotShowSha512();
break;
default:
break;
}
Expand Down Expand Up @@ -2949,6 +2971,12 @@ bool KChecksumsPlugin::isSha256(const QString &input)
return regex.match(input).hasMatch();
}

bool KChecksumsPlugin::isSha512(const QString &input)
{
QRegularExpression regex(QStringLiteral("^[a-f0-9]{128}$"));
return regex.match(input).hasMatch();
}

QString KChecksumsPlugin::computeChecksum(QCryptographicHash::Algorithm algorithm, const QString &path)
{
QFile file(path);
Expand Down Expand Up @@ -2976,6 +3004,10 @@ QCryptographicHash::Algorithm KChecksumsPlugin::detectAlgorithm(const QString &i
return QCryptographicHash::Sha256;
}

if (isSha512(input)) {
return QCryptographicHash::Sha512;
}

// Md4 used as negative error code.
return QCryptographicHash::Md4;
}
Expand Down Expand Up @@ -3080,6 +3112,8 @@ QString KChecksumsPlugin::cachedChecksum(QCryptographicHash::Algorithm algorithm
return d->m_sha1;
case QCryptographicHash::Sha256:
return d->m_sha256;
case QCryptographicHash::Sha512:
return d->m_sha512;
default:
break;
}
Expand All @@ -3099,6 +3133,9 @@ void KChecksumsPlugin::cacheChecksum(const QString &checksum, QCryptographicHash
case QCryptographicHash::Sha256:
d->m_sha256 = checksum;
break;
case QCryptographicHash::Sha512:
d->m_sha512 = checksum;
break;
default:
return;
}
Expand Down
2 changes: 2 additions & 0 deletions src/widgets/kpropertiesdialog_p.h
Expand Up @@ -177,6 +177,7 @@ private Q_SLOTS:
void slotShowMd5();
void slotShowSha1();
void slotShowSha256();
void slotShowSha512();
/**
* Compare @p input (required to be lowercase) with the checksum in cache.
*/
Expand All @@ -186,6 +187,7 @@ private Q_SLOTS:
static bool isMd5(const QString &input);
static bool isSha1(const QString &input);
static bool isSha256(const QString &input);
static bool isSha512(const QString &input);
static QString computeChecksum(QCryptographicHash::Algorithm algorithm, const QString &path);
static QCryptographicHash::Algorithm detectAlgorithm(const QString &input);

Expand Down

0 comments on commit 9cb68b5

Please sign in to comment.