mirror of
https://github.com/keepassxreboot/keepassxc.git
synced 2025-07-25 15:55:38 -04:00
Implement writing in SymmetricCipherStream and add a unit test.
This commit is contained in:
parent
c93ac9f6fc
commit
5da7d3fca6
6 changed files with 255 additions and 6 deletions
|
@ -19,6 +19,7 @@
|
|||
|
||||
#include <cstring>
|
||||
|
||||
#include "core/Endian.h"
|
||||
#include "crypto/CryptoHash.h"
|
||||
|
||||
const QSysInfo::Endian HashedBlockStream::BYTEORDER = QSysInfo::LittleEndian;
|
||||
|
@ -42,10 +43,41 @@ void HashedBlockStream::init()
|
|||
m_bufferPos = 0;
|
||||
m_blockIndex = 0;
|
||||
m_eof = false;
|
||||
m_error = false;
|
||||
}
|
||||
|
||||
bool HashedBlockStream::reset()
|
||||
{
|
||||
if (isWritable()) {
|
||||
if (!m_buffer.isEmpty()) {
|
||||
if (!writeHashedBlock()) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
// write empty final block
|
||||
if (!writeHashedBlock()) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
init();
|
||||
m_buffer.clear();
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
void HashedBlockStream::close()
|
||||
{
|
||||
if (isWritable()) {
|
||||
if (!m_buffer.isEmpty()) {
|
||||
writeHashedBlock();
|
||||
}
|
||||
|
||||
// write empty final block
|
||||
writeHashedBlock();
|
||||
}
|
||||
|
||||
LayeredStream::close();
|
||||
}
|
||||
|
||||
|
@ -134,12 +166,76 @@ bool HashedBlockStream::readHashedBlock()
|
|||
|
||||
qint64 HashedBlockStream::writeData(const char* data, qint64 maxSize)
|
||||
{
|
||||
// TODO implement
|
||||
return 0;
|
||||
Q_ASSERT(maxSize >= 0);
|
||||
|
||||
if (m_error) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
qint64 bytesRemaining = maxSize;
|
||||
qint64 offset = 0;
|
||||
|
||||
while (bytesRemaining > 0) {
|
||||
int bytesToCopy = qMin(bytesRemaining, static_cast<qint64>(m_blockSize - m_buffer.size()));
|
||||
|
||||
m_buffer.append(data + offset, bytesToCopy);
|
||||
|
||||
offset += bytesToCopy;
|
||||
bytesRemaining -= bytesToCopy;
|
||||
|
||||
if (m_buffer.size() == m_blockSize) {
|
||||
if (!writeHashedBlock()) {
|
||||
if (m_error) {
|
||||
return -1;
|
||||
}
|
||||
else {
|
||||
return maxSize - bytesRemaining;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return maxSize;
|
||||
}
|
||||
|
||||
bool HashedBlockStream::writeHashedBlock()
|
||||
{
|
||||
// TODO implement
|
||||
return false;
|
||||
if (!Endian::writeInt32(m_blockIndex, m_baseDevice, BYTEORDER)) {
|
||||
// TODO error
|
||||
Q_ASSERT(false);
|
||||
return false;
|
||||
}
|
||||
m_blockIndex++;
|
||||
|
||||
QByteArray hash;
|
||||
if (!m_buffer.isEmpty()) {
|
||||
hash = CryptoHash::hash(m_buffer, CryptoHash::Sha256);
|
||||
}
|
||||
else {
|
||||
hash.fill(0, 32);
|
||||
}
|
||||
|
||||
if (m_baseDevice->write(hash) != hash.size()) {
|
||||
// TODO error
|
||||
Q_ASSERT(false);
|
||||
return false;
|
||||
}
|
||||
|
||||
if (!Endian::writeInt32(m_buffer.size(), m_baseDevice, BYTEORDER)) {
|
||||
// TODO error
|
||||
Q_ASSERT(false);
|
||||
return false;
|
||||
}
|
||||
|
||||
if (!m_buffer.isEmpty()) {
|
||||
if (m_baseDevice->write(m_buffer) != m_buffer.size()) {
|
||||
// TODO error
|
||||
Q_ASSERT(false);
|
||||
return false;
|
||||
}
|
||||
|
||||
m_buffer.clear();
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
|
|
@ -18,9 +18,9 @@
|
|||
#ifndef KEEPASSX_HASHEDBLOCKSTREAM_H
|
||||
#define KEEPASSX_HASHEDBLOCKSTREAM_H
|
||||
|
||||
#include <QtCore/QSysInfo>
|
||||
|
||||
#include "LayeredStream.h"
|
||||
#include "core/Endian.h"
|
||||
#include "crypto/CryptoHash.h"
|
||||
|
||||
class HashedBlockStream : public LayeredStream
|
||||
{
|
||||
|
@ -30,6 +30,7 @@ public:
|
|||
explicit HashedBlockStream(QIODevice* baseDevice);
|
||||
HashedBlockStream(QIODevice* baseDevice, qint32 blockSize);
|
||||
|
||||
bool reset();
|
||||
void close();
|
||||
|
||||
protected:
|
||||
|
@ -47,6 +48,7 @@ private:
|
|||
int m_bufferPos;
|
||||
quint32 m_blockIndex;
|
||||
bool m_eof;
|
||||
bool m_error;
|
||||
};
|
||||
|
||||
#endif // KEEPASSX_HASHEDBLOCKSTREAM_H
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue