diff --git a/src/core/Tools.cpp b/src/core/Tools.cpp index 6811d078..33f2fee2 100644 --- a/src/core/Tools.cpp +++ b/src/core/Tools.cpp @@ -53,6 +53,22 @@ bool hasChild(const QObject* parent, const QObject* child) return false; } +bool readFromDevice(QIODevice* device, QByteArray& data, int size) +{ + QByteArray buffer; + buffer.resize(size); + + qint64 readResult = device->read(buffer.data(), size); + if (readResult == -1) { + return false; + } + else { + buffer.resize(readResult); + data = buffer; + return true; + } +} + bool readAllFromDevice(QIODevice* device, QByteArray& data) { QByteArray result; diff --git a/src/core/Tools.h b/src/core/Tools.h index 65c0f5e2..e5188d17 100644 --- a/src/core/Tools.h +++ b/src/core/Tools.h @@ -28,6 +28,7 @@ namespace Tools { QString humanReadableFileSize(qint64 bytes); bool hasChild(const QObject* parent, const QObject* child); +bool readFromDevice(QIODevice* device, QByteArray& data, int size = 16384); bool readAllFromDevice(QIODevice* device, QByteArray& data); QDateTime currentDateTimeUtc(); QString imageReaderFilter(); diff --git a/src/crypto/CryptoHash.cpp b/src/crypto/CryptoHash.cpp index 31c0c934..c9459598 100644 --- a/src/crypto/CryptoHash.cpp +++ b/src/crypto/CryptoHash.cpp @@ -66,6 +66,10 @@ void CryptoHash::addData(const QByteArray& data) { Q_D(CryptoHash); + if (data.isEmpty()) { + return; + } + gcry_md_write(d->ctx, data.constData(), data.size()); } diff --git a/src/keys/FileKey.cpp b/src/keys/FileKey.cpp index 9063251d..b22c5b0f 100644 --- a/src/keys/FileKey.cpp +++ b/src/keys/FileKey.cpp @@ -222,8 +222,14 @@ bool FileKey::loadBinary(QIODevice* device) return false; } - m_key = device->readAll(); - return true; + QByteArray data; + if (!Tools::readAllFromDevice(device, data) || data.size() != 32) { + return false; + } + else { + m_key = data; + return true; + } } bool FileKey::loadHex(QIODevice* device) @@ -232,7 +238,10 @@ bool FileKey::loadHex(QIODevice* device) return false; } - QByteArray data = device->readAll(); + QByteArray data; + if (!Tools::readAllFromDevice(device, data) || data.size() != 64) { + return false; + } if (!Tools::isHex(data)) { return false; @@ -253,11 +262,12 @@ bool FileKey::loadHashed(QIODevice* device) CryptoHash cryptoHash(CryptoHash::Sha256); QByteArray buffer; - - while (!device->atEnd()) { - buffer = device->read(1024); + do { + if (!Tools::readFromDevice(device, buffer)) { + return false; + } cryptoHash.addData(buffer); - } + } while (!buffer.isEmpty()); m_key = cryptoHash.result();