Add Tools::readFromDevice() and make FileKey use it.

This commit is contained in:
Felix Geyer 2012-05-10 19:00:36 +02:00
parent 6eebd95de1
commit cc6f524168
4 changed files with 38 additions and 7 deletions

View File

@ -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;

View File

@ -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();

View File

@ -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());
}

View File

@ -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();