mirror of
https://github.com/keepassxreboot/keepassxc.git
synced 2025-04-19 07:15:57 -04:00
Add Tools::readFromDevice() and make FileKey use it.
This commit is contained in:
parent
6eebd95de1
commit
cc6f524168
@ -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;
|
||||
|
@ -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();
|
||||
|
@ -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());
|
||||
}
|
||||
|
||||
|
@ -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();
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user