pollymc/launcher/net/ChecksumValidator.h
flow 040ee919e5
refactor: more net cleanup
This runs clang-tidy on some other files in launcher/net/.

This also makes use of some JSON wrappers in HttpMetaCache, instead of
using the Qt stuff directly.

Lastly, this removes useless null checks (crashes don't occur because of
this, but because of concurrent usage / free of the QByteArray pointer),
and fix a fixme in Download.h
2022-05-12 18:11:55 -03:00

48 lines
1.1 KiB
C++

#pragma once
#include "Validator.h"
#include <QCryptographicHash>
#include <QFile>
namespace Net {
class ChecksumValidator : public Validator {
public:
ChecksumValidator(QCryptographicHash::Algorithm algorithm, QByteArray expected = QByteArray())
: m_checksum(algorithm), m_expected(expected){};
virtual ~ChecksumValidator() = default;
public:
auto init(QNetworkRequest&) -> bool override
{
m_checksum.reset();
return true;
}
auto write(QByteArray& data) -> bool override
{
m_checksum.addData(data);
return true;
}
auto abort() -> bool override { return true; }
auto validate(QNetworkReply&) -> bool override
{
if (m_expected.size() && m_expected != hash()) {
qWarning() << "Checksum mismatch, download is bad.";
return false;
}
return true;
}
auto hash() -> QByteArray { return m_checksum.result(); }
void setExpected(QByteArray expected) { m_expected = expected; }
private:
QCryptographicHash m_checksum;
QByteArray m_expected;
};
} // namespace Net