2016-05-28 18:54:17 +01:00
|
|
|
#pragma once
|
|
|
|
|
|
|
|
#include "Validator.h"
|
2022-04-27 22:36:11 +01:00
|
|
|
|
2016-05-28 18:54:17 +01:00
|
|
|
#include <QCryptographicHash>
|
|
|
|
#include <QFile>
|
|
|
|
|
|
|
|
namespace Net {
|
2022-04-27 22:36:11 +01:00
|
|
|
class ChecksumValidator : public Validator {
|
|
|
|
public:
|
2018-07-15 13:51:05 +01:00
|
|
|
ChecksumValidator(QCryptographicHash::Algorithm algorithm, QByteArray expected = QByteArray())
|
2022-04-27 22:36:11 +01:00
|
|
|
: m_checksum(algorithm), m_expected(expected){};
|
|
|
|
virtual ~ChecksumValidator() = default;
|
2016-05-28 18:54:17 +01:00
|
|
|
|
2022-04-27 22:36:11 +01:00
|
|
|
public:
|
|
|
|
auto init(QNetworkRequest&) -> bool override
|
2018-07-15 13:51:05 +01:00
|
|
|
{
|
|
|
|
m_checksum.reset();
|
|
|
|
return true;
|
|
|
|
}
|
2022-04-27 22:36:11 +01:00
|
|
|
|
|
|
|
auto write(QByteArray& data) -> bool override
|
2018-07-15 13:51:05 +01:00
|
|
|
{
|
|
|
|
m_checksum.addData(data);
|
|
|
|
return true;
|
|
|
|
}
|
2022-04-27 22:36:11 +01:00
|
|
|
|
|
|
|
auto abort() -> bool override { return true; }
|
|
|
|
|
|
|
|
auto validate(QNetworkReply&) -> bool override
|
2018-07-15 13:51:05 +01:00
|
|
|
{
|
2022-04-27 22:36:11 +01:00
|
|
|
if (m_expected.size() && m_expected != hash()) {
|
2018-07-15 13:51:05 +01:00
|
|
|
qWarning() << "Checksum mismatch, download is bad.";
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
2016-05-28 18:54:17 +01:00
|
|
|
|
2022-04-27 22:36:11 +01:00
|
|
|
auto hash() -> QByteArray { return m_checksum.result(); }
|
|
|
|
|
|
|
|
void setExpected(QByteArray expected) { m_expected = expected; }
|
|
|
|
|
|
|
|
private:
|
2018-07-15 13:51:05 +01:00
|
|
|
QCryptographicHash m_checksum;
|
|
|
|
QByteArray m_expected;
|
2016-05-28 18:54:17 +01:00
|
|
|
};
|
2022-04-27 22:36:11 +01:00
|
|
|
} // namespace Net
|