diff options
-rw-r--r-- | src/ui/data_struct/SettingsObj.cpp | 55 | ||||
-rw-r--r-- | src/ui/data_struct/SettingsObj.h | 54 | ||||
-rw-r--r-- | src/ui/settings/GlobalSettingStation.cpp | 55 | ||||
-rw-r--r-- | src/ui/settings/GlobalSettingStation.h | 4 |
4 files changed, 158 insertions, 10 deletions
diff --git a/src/ui/data_struct/SettingsObj.cpp b/src/ui/data_struct/SettingsObj.cpp new file mode 100644 index 00000000..d069dfe7 --- /dev/null +++ b/src/ui/data_struct/SettingsObj.cpp @@ -0,0 +1,55 @@ +/** + * This file is part of GpgFrontend. + * + * GpgFrontend is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * Foobar is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with Foobar. If not, see <https://www.gnu.org/licenses/>. + * + * The initial version of the source code is inherited from gpg4usb-team. + * Their source code version also complies with GNU General Public License. + * + * The source code version of this software was modified and released + * by Saturneric<[email protected]> starting on May 12, 2021. + * + */ + +#include "SettingsObj.h" + +nlohmann::json& GpgFrontend::UI::SettingsObj::Check( + const std::string& key, nlohmann::json default_value) { + if (!nlohmann::json::contains(key)) + nlohmann::json::operator[](key) = std::move(default_value); + return nlohmann::json::operator[](key); +} + +GpgFrontend::UI::SettingsObj GpgFrontend::UI::SettingsObj::Check( + const std::string& key) { + if (!nlohmann::json::contains(key)) nlohmann::json::operator[](key) = {}; + return SettingsObj{nlohmann::json::operator[](key), false}; +} + +GpgFrontend::UI::SettingsObj::SettingsObj(std::string settings_name) + : settings_name_(std::move(settings_name)) { + try { + auto _json_optional = + GlobalSettingStation::GetInstance().GetDataObject(settings_name_); + + if (_json_optional.has_value()) { + nlohmann::json::operator=(_json_optional.value()); + } + + } catch (...) { + } +} + +GpgFrontend::UI::SettingsObj::SettingsObj(nlohmann::json _sub_json, bool) + : nlohmann::json(std::move(_sub_json)), settings_name_({}) {} diff --git a/src/ui/data_struct/SettingsObj.h b/src/ui/data_struct/SettingsObj.h new file mode 100644 index 00000000..ee3da705 --- /dev/null +++ b/src/ui/data_struct/SettingsObj.h @@ -0,0 +1,54 @@ +/** + * This file is part of GpgFrontend. + * + * GpgFrontend is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * Foobar is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with Foobar. If not, see <https://www.gnu.org/licenses/>. + * + * The initial version of the source code is inherited from gpg4usb-team. + * Their source code version also complies with GNU General Public License. + * + * The source code version of this software was modified and released + * by Saturneric<[email protected]> starting on May 12, 2021. + * + */ + +#ifndef GPGFRONTEND_SETTINGSOBJ_H +#define GPGFRONTEND_SETTINGSOBJ_H + +#include <utility> + +#include "ui/settings/GlobalSettingStation.h" + +namespace GpgFrontend::UI { + +class SettingsObj : public nlohmann::json { + public: + explicit SettingsObj(std::string settings_name); + + explicit SettingsObj(nlohmann::json _sub_json, bool); + + ~SettingsObj() { + GpgFrontend::UI::GlobalSettingStation::GetInstance().SaveDataObj( + settings_name_, *this); + } + + nlohmann::json& Check(const std::string& key, nlohmann::json default_value); + + SettingsObj Check(const std::string& key); + + private: + std::string settings_name_; +}; +} // namespace GpgFrontend::UI + +#endif // GPGFRONTEND_SETTINGSOBJ_H diff --git a/src/ui/settings/GlobalSettingStation.cpp b/src/ui/settings/GlobalSettingStation.cpp index 42383bee..73090ec7 100644 --- a/src/ui/settings/GlobalSettingStation.cpp +++ b/src/ui/settings/GlobalSettingStation.cpp @@ -176,24 +176,37 @@ void GpgFrontend::UI::GlobalSettingStation::init_app_secure_key() { boost::filesystem::owner_read | boost::filesystem::owner_write); } -void GpgFrontend::UI::GlobalSettingStation::SaveDataObj( +std::string GpgFrontend::UI::GlobalSettingStation::SaveDataObj( const std::string& _key, const nlohmann::json& value) { - auto _hash_obj_key = - QCryptographicHash::hash(hash_key_ + QByteArray::fromStdString(_key), - QCryptographicHash::Sha256) - .toHex() - .toStdString(); + std::string _hash_obj_key = {}; + if (_key.empty()) { + _hash_obj_key = + QCryptographicHash::hash( + hash_key_ + QByteArray::fromStdString( + generate_passphrase(32) + + to_iso_extended_string( + boost::posix_time::second_clock::local_time())), + QCryptographicHash::Sha256) + .toHex() + .toStdString(); + } else { + _hash_obj_key = + QCryptographicHash::hash(hash_key_ + QByteArray::fromStdString(_key), + QCryptographicHash::Sha256) + .toHex() + .toStdString(); + } const auto obj_path = app_data_objs_path / _hash_obj_key; - LOG(INFO) << "save obj" << obj_path; - QAESEncryption encryption(QAESEncryption::AES_256, QAESEncryption::ECB, QAESEncryption::Padding::ISO); auto encoded = encryption.encode(QByteArray::fromStdString(to_string(value)), hash_key_); GpgFrontend::write_buffer_to_file(obj_path.string(), encoded.toStdString()); + + return _key.empty() ? _hash_obj_key : std::string(); } std::optional<nlohmann::json> @@ -224,7 +237,31 @@ GpgFrontend::UI::GlobalSettingStation::GetDataObject(const std::string& _key) { } catch (...) { return {}; } - return {}; +} +std::optional<nlohmann::json> +GpgFrontend::UI::GlobalSettingStation::GetDataObjectByRef( + const std::string& _ref) { + if (_ref.size() != 64) return {}; + + try { + auto _hash_obj_key = _ref; + const auto obj_path = app_data_objs_path / _hash_obj_key; + + if (!boost::filesystem::exists(obj_path)) return {}; + + auto buffer = GpgFrontend::read_all_data_in_file(obj_path.string()); + auto encoded = QByteArray::fromStdString(buffer); + + QAESEncryption encryption(QAESEncryption::AES_256, QAESEncryption::ECB, + QAESEncryption::Padding::ISO); + + auto decoded = + encryption.removePadding(encryption.decode(encoded, hash_key_)); + + return nlohmann::json::parse(decoded.toStdString()); + } catch (...) { + return {}; + } } GpgFrontend::UI::GlobalSettingStation::~GlobalSettingStation() noexcept = diff --git a/src/ui/settings/GlobalSettingStation.h b/src/ui/settings/GlobalSettingStation.h index 26c42f78..420b5c99 100644 --- a/src/ui/settings/GlobalSettingStation.h +++ b/src/ui/settings/GlobalSettingStation.h @@ -94,10 +94,12 @@ class GlobalSettingStation : public QObject { void SyncSettings() noexcept; - void SaveDataObj(const std::string& _key, const nlohmann::json& value); + std::string SaveDataObj(const std::string& _key, const nlohmann::json& value); std::optional<nlohmann::json> GetDataObject(const std::string& _key); + std::optional<nlohmann::json> GetDataObjectByRef(const std::string& _ref); + private: // Program Location boost::filesystem::path app_path = qApp->applicationDirPath().toStdString(); |