Bump CMake minimum to 4.0 for VoidLibrary toolchain

This commit is contained in:
faumaray committed 2026-06-22 15:53:27 +03:00
1 parent e646080683
commit bcf4f4770a
12 files changed
+1784 -1208

No files matched your search

+216
View File
@@ -0,0 +1,216 @@
#include "storage/SecureStorage.hpp"
#include <QCoreApplication>
#include <QDateTime>
#include <QDir>
#include <QFileInfo>
#include <QStandardPaths>
#include <QtEndian>
namespace Echoion::Desktop
{
SecureStorage::SecureStorage() = default;
QString SecureStorage::getStorageDir()
{
QString appDir = QCoreApplication::applicationDirPath();
QString dataDir = appDir + "/data";
QDir dir(dataDir);
if (!dir.exists()) {
dir.mkpath(".");
}
return dataDir;
}
QString SecureStorage::getSettingsPath()
{
return getStorageDir() + "/settings.dat";
}
QString SecureStorage::getTokensPath()
{
return getStorageDir() + "/tokens.dat";
}
QByteArray SecureStorage::generateKey() const
{
if (!cachedKey_.isEmpty()) {
return cachedKey_;
}
// Generate a deterministic key from machine-specific data
// This makes it harder to copy tokens to another machine
QString seed =
QCoreApplication::applicationDirPath() +
QCoreApplication::applicationName() +
QString::number(QDateTime::currentDateTime().date().year());
QByteArray hash =
QCryptographicHash::hash(seed.toUtf8(), QCryptographicHash::Sha256);
// Add some randomness but keep it deterministic for this machine
QRandomGenerator rng(qHash(seed));
for (int i = 0; i < hash.size(); ++i) {
hash[i] = hash[i] ^ static_cast<char>(rng.generate() & 0xFF);
}
cachedKey_ = hash;
return hash;
}
QByteArray SecureStorage::xorEncrypt(const QByteArray& data,
const QByteArray& key) const
{
QByteArray result = data;
int keyLen = key.length();
for (int i = 0; i < result.length(); ++i) {
result[i] = result[i] ^ key[i % keyLen];
}
return result;
}
QByteArray SecureStorage::encrypt(const QByteArray& data) const
{
QByteArray key = generateKey();
// Add simple header for validation
QByteArray header;
QDataStream headerStream(&header, QIODevice::WriteOnly);
headerStream << quint32(0x4543484F); // "ECHO" magic
headerStream << quint32(data.length());
QByteArray encrypted = xorEncrypt(data, key);
return header + encrypted;
}
QByteArray SecureStorage::decrypt(const QByteArray& data) const
{
if (data.length() < 8) {
return QByteArray(); // Too short
}
// Verify header
QDataStream headerStream(data);
quint32 magic;
quint32 dataLen;
headerStream >> magic >> dataLen;
if (magic != 0x4543484F) {
return QByteArray(); // Invalid magic
}
QByteArray key = generateKey();
QByteArray encrypted = data.mid(8); // Skip header
return xorEncrypt(encrypted, key);
}
bool SecureStorage::saveToFile(const QString& filepath,
const QByteArray& data) const
{
// Create backup of existing file
if (QFile::exists(filepath)) {
createBackup(filepath);
}
// Write to temp file first
QString tempPath = filepath + ".tmp";
QFile tempFile(tempPath);
if (!tempFile.open(QIODevice::WriteOnly)) {
return false;
}
if (tempFile.write(data) != data.length()) {
tempFile.close();
QFile::remove(tempPath);
return false;
}
tempFile.close();
// Atomic rename
if (!QFile::rename(tempPath, filepath)) {
QFile::remove(tempPath);
return false;
}
return true;
}
QByteArray SecureStorage::loadFromFile(const QString& filepath) const
{
QFile file(filepath);
if (!file.open(QIODevice::ReadOnly)) {
// Try to restore from backup
if (restoreFromBackup(filepath)) {
return loadFromFile(filepath); // Retry
}
return QByteArray();
}
QByteArray data = file.readAll();
file.close();
return data;
}
void SecureStorage::createBackup(const QString& filepath) const
{
QString backupPath = filepath + ".backup";
QFile::remove(backupPath); // Remove old backup
QFile::copy(filepath, backupPath);
// Cleanup old backups (keep only last 3)
cleanupOldBackups(filepath, 3);
}
bool SecureStorage::restoreFromBackup(const QString& filepath) const
{
QString backupPath = filepath + ".backup";
if (!QFile::exists(backupPath)) {
return false;
}
QFile::remove(filepath);
return QFile::copy(backupPath, filepath);
}
void SecureStorage::cleanupOldBackups(const QString& filepath,
int maxBackups) const
{
QString basePath = filepath + ".backup";
QFileInfo info(basePath);
QDir dir = info.dir();
QString baseName = info.fileName();
// List all backup files
QStringList filters;
filters << baseName + "*";
QFileInfoList backups = dir.entryInfoList(filters, QDir::Files);
if (backups.size() <= maxBackups) {
return;
}
// Sort by modification time (oldest first)
std::sort(backups.begin(), backups.end(),
[](const QFileInfo& a, const QFileInfo& b) {
return a.lastModified() < b.lastModified();
});
// Remove oldest backups
for (int i = 0; i < backups.size() - maxBackups; ++i) {
QFile::remove(backups[i].absoluteFilePath());
}
}
} // namespace Echoion::Desktop