diff options
author | saturneric <[email protected]> | 2023-12-25 12:51:01 +0000 |
---|---|---|
committer | saturneric <[email protected]> | 2023-12-25 12:51:01 +0000 |
commit | 5260d7a5634279408b3eb36a5f26fe71fb6f9e69 (patch) | |
tree | 670e0878a41e8c6b1651fedbd224bfbea94e0849 /src/core/function/gpg | |
parent | fix: use the new async encrypt symmetric api at gui app (diff) | |
download | GpgFrontend-5260d7a5634279408b3eb36a5f26fe71fb6f9e69.tar.gz GpgFrontend-5260d7a5634279408b3eb36a5f26fe71fb6f9e69.zip |
feat: upgrade basical operations fully to async style and update test cases
Diffstat (limited to 'src/core/function/gpg')
-rw-r--r-- | src/core/function/gpg/GpgBasicOperator.cpp | 199 | ||||
-rw-r--r-- | src/core/function/gpg/GpgBasicOperator.h | 32 | ||||
-rw-r--r-- | src/core/function/gpg/GpgFileOpera.cpp | 190 | ||||
-rw-r--r-- | src/core/function/gpg/GpgFileOpera.h | 27 |
4 files changed, 226 insertions, 222 deletions
diff --git a/src/core/function/gpg/GpgBasicOperator.cpp b/src/core/function/gpg/GpgBasicOperator.cpp index aa944348..36bd25f6 100644 --- a/src/core/function/gpg/GpgBasicOperator.cpp +++ b/src/core/function/gpg/GpgBasicOperator.cpp @@ -33,17 +33,18 @@ #include "core/GpgModel.h" #include "core/model/GpgDecryptResult.h" #include "core/model/GpgEncryptResult.h" +#include "core/model/GpgSignResult.h" +#include "core/model/GpgVerifyResult.h" #include "core/utils/AsyncUtils.h" #include "core/utils/GpgUtils.h" namespace GpgFrontend { -GpgFrontend::GpgBasicOperator::GpgBasicOperator(int channel) +GpgBasicOperator::GpgBasicOperator(int channel) : SingletonFunctionObject<GpgBasicOperator>(channel) {} -void GpgFrontend::GpgBasicOperator::Encrypt(std::vector<GpgKey> keys, - GFBuffer in_buffer, bool ascii, - const GpgOperationCallback& cb) { +void GpgBasicOperator::Encrypt(KeyArgsList keys, GFBuffer in_buffer, bool ascii, + const GpgOperationCallback& cb) { RunGpgOperaAsync( [=](const DataObjectPtr& data_object) -> GpgError { std::vector<gpgme_key_t> recipients(keys.begin(), keys.end()); @@ -66,8 +67,8 @@ void GpgFrontend::GpgBasicOperator::Encrypt(std::vector<GpgKey> keys, cb, "gpgme_op_encrypt", "2.1.0"); } -void GpgFrontend::GpgBasicOperator::Decrypt(GFBuffer in_buffer, - const GpgOperationCallback& cb) { +void GpgBasicOperator::Decrypt(GFBuffer in_buffer, + const GpgOperationCallback& cb) { RunGpgOperaAsync( [=](const DataObjectPtr& data_object) -> GpgError { GpgData data_in(in_buffer); @@ -84,126 +85,117 @@ void GpgFrontend::GpgBasicOperator::Decrypt(GFBuffer in_buffer, cb, "gpgme_op_decrypt", "2.1.0"); } -auto GpgFrontend::GpgBasicOperator::Verify(BypeArrayRef& in_buffer, - ByteArrayPtr& sig_buffer, - GpgVerifyResult& result) const - -> GpgFrontend::GpgError { - GpgError err; - - GpgData data_in(in_buffer.data(), in_buffer.size()); - GpgData data_out; - - if (sig_buffer != nullptr && !sig_buffer->empty()) { - GpgData sig_data(sig_buffer->data(), sig_buffer->size()); - err = CheckGpgError( - gpgme_op_verify(ctx_.DefaultContext(), sig_data, data_in, nullptr)); - } else { - err = CheckGpgError( - gpgme_op_verify(ctx_.DefaultContext(), data_in, nullptr, data_out)); - } - - auto temp_result = NewResult(gpgme_op_verify_result(ctx_.DefaultContext())); - std::swap(result, temp_result); +void GpgBasicOperator::Verify(GFBuffer in_buffer, GFBuffer sig_buffer, + const GpgOperationCallback& cb) { + RunGpgOperaAsync( + [=](const DataObjectPtr& data_object) -> GpgError { + GpgError err; - return err; -} + GpgData data_in(in_buffer); + GpgData data_out; -auto GpgFrontend::GpgBasicOperator::Sign( - KeyListPtr signers, BypeArrayRef in_buffer, ByteArrayPtr& out_buffer, - gpgme_sig_mode_t mode, GpgSignResult& result) -> GpgFrontend::GpgError { - GpgError err; + if (!sig_buffer.Empty()) { + GpgData sig_data(sig_buffer); + err = CheckGpgError(gpgme_op_verify(ctx_.DefaultContext(), sig_data, + data_in, nullptr)); + } else { + err = CheckGpgError(gpgme_op_verify(ctx_.DefaultContext(), data_in, + nullptr, data_out)); + } - // Set Singers of this opera - SetSigners(*signers); + data_object->Swap({ + GpgVerifyResult(gpgme_op_verify_result(ctx_.DefaultContext())), + }); - GpgData data_in(in_buffer.data(), in_buffer.size()); - GpgData data_out; + return err; + }, + cb, "gpgme_op_verify", "2.1.0"); +} - err = CheckGpgError( - gpgme_op_sign(ctx_.DefaultContext(), data_in, data_out, mode)); +void GpgBasicOperator::Sign(KeyArgsList signers, GFBuffer in_buffer, + GpgSignMode mode, bool ascii, + const GpgOperationCallback& cb) { + RunGpgOperaAsync( + [=](const DataObjectPtr& data_object) -> GpgError { + GpgError err; - auto temp_data_out = data_out.Read2Buffer(); - std::swap(temp_data_out, out_buffer); + // Set Singers of this opera + SetSigners(signers, ascii); - auto temp_result = NewResult(gpgme_op_sign_result(ctx_.DefaultContext())); + GpgData data_in(in_buffer); + GpgData data_out; - std::swap(result, temp_result); + auto* ctx = ascii ? ctx_.DefaultContext() : ctx_.BinaryContext(); + err = CheckGpgError(gpgme_op_sign(ctx, data_in, data_out, mode)); - return err; + data_object->Swap({GpgSignResult(gpgme_op_sign_result(ctx)), + data_out.Read2GFBuffer()}); + return err; + }, + cb, "gpgme_op_sign", "2.1.0"); } -auto GpgFrontend::GpgBasicOperator::DecryptVerify( - BypeArrayRef in_buffer, ByteArrayPtr& out_buffer, - GpgDecrResult& decrypt_result, GpgVerifyResult& verify_result) -> GpgError { - GpgError err; - - GpgData data_in(in_buffer.data(), in_buffer.size()); - GpgData data_out; - - err = CheckGpgError( - gpgme_op_decrypt_verify(ctx_.DefaultContext(), data_in, data_out)); +void GpgBasicOperator::DecryptVerify(GFBuffer in_buffer, + const GpgOperationCallback& cb) { + RunGpgOperaAsync( + [=](const DataObjectPtr& data_object) -> GpgError { + GpgError err; - auto temp_data_out = data_out.Read2Buffer(); - std::swap(temp_data_out, out_buffer); + GpgData data_in(in_buffer); + GpgData data_out; - auto temp_decr_result = - NewResult(gpgme_op_decrypt_result(ctx_.DefaultContext())); - std::swap(decrypt_result, temp_decr_result); + err = CheckGpgError( + gpgme_op_decrypt_verify(ctx_.DefaultContext(), data_in, data_out)); - auto temp_verify_result = - NewResult(gpgme_op_verify_result(ctx_.DefaultContext())); - std::swap(verify_result, temp_verify_result); + data_object->Swap( + {GpgDecryptResult(gpgme_op_decrypt_result(ctx_.DefaultContext())), + GpgVerifyResult(gpgme_op_verify_result(ctx_.DefaultContext())), + data_out.Read2GFBuffer()}); - return err; + return err; + }, + cb, "gpgme_op_decrypt_verify", "2.1.0"); } -auto GpgFrontend::GpgBasicOperator::EncryptSign( - KeyListPtr keys, KeyListPtr signers, BypeArrayRef in_buffer, - ByteArrayPtr& out_buffer, GpgEncrResult& encr_result, - GpgSignResult& sign_result) -> GpgError { - GpgError err; - SetSigners(*signers); - - // gpgme_encrypt_result_t e_result; - gpgme_key_t recipients[keys->size() + 1]; +void GpgBasicOperator::EncryptSign(KeyArgsList keys, KeyArgsList signers, + GFBuffer in_buffer, bool ascii, + const GpgOperationCallback& cb) { + RunGpgOperaAsync( + [=](const DataObjectPtr& data_object) -> GpgError { + GpgError err; + std::vector<gpgme_key_t> recipients(keys.begin(), keys.end()); - // set key for user - int index = 0; - for (const auto& key : *keys) { - recipients[index++] = static_cast<gpgme_key_t>(key); - } + // Last entry data_in array has to be nullptr + recipients.emplace_back(nullptr); - // Last entry dataIn array has to be nullptr - recipients[keys->size()] = nullptr; + SetSigners(signers, ascii); - GpgData data_in(in_buffer.data(), in_buffer.size()); - GpgData data_out; + GpgData data_in(in_buffer); + GpgData data_out; - // If the last parameter isnt 0, a private copy of data is made - err = CheckGpgError(gpgme_op_encrypt_sign(ctx_.DefaultContext(), recipients, - GPGME_ENCRYPT_ALWAYS_TRUST, data_in, - data_out)); + auto* ctx = ascii ? ctx_.DefaultContext() : ctx_.BinaryContext(); + err = CheckGpgError(gpgme_op_encrypt_sign(ctx, recipients.data(), + GPGME_ENCRYPT_ALWAYS_TRUST, + data_in, data_out)); - auto temp_data_out = data_out.Read2Buffer(); - std::swap(temp_data_out, out_buffer); + data_object->Swap({GpgEncryptResult(gpgme_op_encrypt_result(ctx)), + GpgSignResult(gpgme_op_sign_result(ctx)), + data_out.Read2GFBuffer()}); + return err; + }, + cb, "gpgme_op_encrypt_sign", "2.1.0"); +} - auto temp_encr_result = - NewResult(gpgme_op_encrypt_result(ctx_.DefaultContext())); - swap(encr_result, temp_encr_result); - auto temp_sign_result = - NewResult(gpgme_op_sign_result(ctx_.DefaultContext())); - swap(sign_result, temp_sign_result); +void GpgBasicOperator::SetSigners(const KeyArgsList& signers, bool ascii) { + auto* ctx = ascii ? ctx_.DefaultContext() : ctx_.BinaryContext(); - return err; -} + gpgme_signers_clear(ctx); -void GpgFrontend::GpgBasicOperator::SetSigners(KeyArgsList& signers) { - gpgme_signers_clear(ctx_.DefaultContext()); for (const GpgKey& key : signers) { SPDLOG_DEBUG("key fpr: {}", key.GetFingerprint()); if (key.IsHasActualSigningCapability()) { SPDLOG_DEBUG("signer"); - auto error = gpgme_signers_add(ctx_.DefaultContext(), gpgme_key_t(key)); + auto error = gpgme_signers_add(ctx, gpgme_key_t(key)); CheckGpgError(error); } } @@ -211,19 +203,20 @@ void GpgFrontend::GpgBasicOperator::SetSigners(KeyArgsList& signers) { SPDLOG_DEBUG("not all signers added"); } -auto GpgFrontend::GpgBasicOperator::GetSigners() - -> std::unique_ptr<GpgFrontend::KeyArgsList> { - auto count = gpgme_signers_count(ctx_.DefaultContext()); +auto GpgBasicOperator::GetSigners(bool ascii) -> std::unique_ptr<KeyArgsList> { + auto* ctx = ascii ? ctx_.DefaultContext() : ctx_.BinaryContext(); + + auto count = gpgme_signers_count(ctx); auto signers = std::make_unique<std::vector<GpgKey>>(); for (auto i = 0U; i < count; i++) { - auto key = GpgKey(gpgme_signers_enum(ctx_.DefaultContext(), i)); + auto key = GpgKey(gpgme_signers_enum(ctx, i)); signers->push_back(GpgKey(std::move(key))); } return signers; } -void GpgFrontend::GpgBasicOperator::EncryptSymmetric( - GFBuffer in_buffer, bool ascii, const GpgOperationCallback& cb) { +void GpgBasicOperator::EncryptSymmetric(GFBuffer in_buffer, bool ascii, + const GpgOperationCallback& cb) { RunGpgOperaAsync( [=](const DataObjectPtr& data_object) -> GpgError { GpgData data_in(in_buffer); diff --git a/src/core/function/gpg/GpgBasicOperator.h b/src/core/function/gpg/GpgBasicOperator.h index 6836e390..4b1a2688 100644 --- a/src/core/function/gpg/GpgBasicOperator.h +++ b/src/core/function/gpg/GpgBasicOperator.h @@ -53,19 +53,13 @@ class GPGFRONTEND_CORE_EXPORT GpgBasicOperator int channel = SingletonFunctionObject::GetDefaultChannel()); /** - * @brief Call the interface provided by gpgme for encryption operation + * @brief * * All incoming data pointers out_buffer will be replaced with new valid * values * - * @param keys list of public keys - * @param in_buffer data that needs to be encrypted - * @param out_buffer encrypted data - * @param result the result of the operation - * @return error code */ - void Encrypt(std::vector<GpgKey>, GFBuffer, bool, - const GpgOperationCallback&); + void Encrypt(KeyArgsList, GFBuffer, bool, const GpgOperationCallback&); /** * @brief Call the interface provided by GPGME to symmetrical encryption @@ -91,9 +85,8 @@ class GPGFRONTEND_CORE_EXPORT GpgBasicOperator * @param sign_result Signature result * @return */ - auto EncryptSign(KeyListPtr keys, KeyListPtr signers, BypeArrayRef in_buffer, - ByteArrayPtr& out_buffer, GpgEncrResult& encr_result, - GpgSignResult& sign_result) -> GpgError; + void EncryptSign(KeyArgsList keys, KeyArgsList signers, GFBuffer in_buffer, + bool ascii, const GpgOperationCallback& cb); /** * @brief Call the interface provided by gpgme for decryption operation @@ -115,9 +108,7 @@ class GPGFRONTEND_CORE_EXPORT GpgBasicOperator * @param verify_result the result of the verifying operation * @return error code */ - auto DecryptVerify(BypeArrayRef in_buffer, ByteArrayPtr& out_buffer, - GpgDecrResult& decrypt_result, - GpgVerifyResult& verify_result) -> GpgError; + void DecryptVerify(GFBuffer in_buffer, const GpgOperationCallback& cb); /** * @brief Call the interface provided by gpgme for verification operation @@ -127,8 +118,8 @@ class GPGFRONTEND_CORE_EXPORT GpgBasicOperator * @param result the result of the operation * @return error code */ - auto Verify(BypeArrayRef in_buffer, ByteArrayPtr& sig_buffer, - GpgVerifyResult& result) const -> GpgError; + void Verify(GFBuffer in_buffer, GFBuffer sig_buffer, + const GpgOperationCallback& cb); /** * @brief Call the interface provided by gpgme for signing operation @@ -150,9 +141,8 @@ class GPGFRONTEND_CORE_EXPORT GpgBasicOperator * @param result the result of the operation * @return error code */ - auto Sign(KeyListPtr signers, BypeArrayRef in_buffer, - ByteArrayPtr& out_buffer, gpgme_sig_mode_t mode, - GpgSignResult& result) -> GpgError; + void Sign(KeyArgsList signers, GFBuffer in_buffer, GpgSignMode mode, + bool ascii, const GpgOperationCallback& cb); /** * @brief Set the private key for signatures, this operation is a global @@ -160,14 +150,14 @@ class GPGFRONTEND_CORE_EXPORT GpgBasicOperator * * @param keys */ - void SetSigners(KeyArgsList& signers); + void SetSigners(const KeyArgsList& signers, bool ascii); /** * @brief Get a global signature private keys that has been set. * * @return Intelligent pointer pointing to the private key list */ - auto GetSigners() -> std::unique_ptr<KeyArgsList>; + auto GetSigners(bool ascii) -> std::unique_ptr<KeyArgsList>; private: GpgContext& ctx_ = GpgContext::GetInstance( diff --git a/src/core/function/gpg/GpgFileOpera.cpp b/src/core/function/gpg/GpgFileOpera.cpp index cbfbc8b1..1c577997 100644 --- a/src/core/function/gpg/GpgFileOpera.cpp +++ b/src/core/function/gpg/GpgFileOpera.cpp @@ -31,16 +31,20 @@ #include "core/function/gpg/GpgBasicOperator.h" #include "core/model/GFBuffer.h" +#include "core/model/GpgDecryptResult.h" #include "core/model/GpgEncryptResult.h" #include "core/model/GpgKey.h" +#include "core/model/GpgSignResult.h" +#include "core/model/GpgVerifyResult.h" #include "core/utils/GpgUtils.h" #include "core/utils/IOUtils.h" -void GpgFrontend::GpgFileOpera::EncryptFile(std::vector<GpgKey> keys, - const std::string& in_path, - const std::string& out_path, - bool ascii, - const GpgOperationCallback& cb) { +namespace GpgFrontend { + +void GpgFileOpera::EncryptFile(std::vector<GpgKey> keys, + const std::string& in_path, + const std::string& out_path, bool ascii, + const GpgOperationCallback& cb) { #ifdef WINDOWS auto in_path_std = std::filesystem::path(QString::fromStdString(in_path).toStdU16String()); @@ -64,20 +68,18 @@ void GpgFrontend::GpgFileOpera::EncryptFile(std::vector<GpgKey> keys, } auto result = ExtractParams<GpgEncryptResult>(data_object, 0); auto buffer = ExtractParams<GFBuffer>(data_object, 1); - if (CheckGpgError(err) != GPG_ERR_NO_ERROR) { - cb(err, TransferParams(result)); - return; - } - - if (!WriteFileGFBuffer(out_path_std, buffer)) { - throw std::runtime_error("write buffer to file error"); + if (CheckGpgError(err) == GPG_ERR_NO_ERROR) { + if (!WriteFileGFBuffer(out_path_std, buffer)) { + throw std::runtime_error("write buffer to file error"); + } } + cb(err, TransferParams(result)); }); } -void GpgFrontend::GpgFileOpera::DecryptFile(const std::string& in_path, - const std::string& out_path, - const GpgOperationCallback& cb) { +void GpgFileOpera::DecryptFile(const std::string& in_path, + const std::string& out_path, + const GpgOperationCallback& cb) { #ifdef WINDOWS auto in_path_std = std::filesystem::path(QString::fromStdString(in_path).toStdU16String()); @@ -96,10 +98,10 @@ void GpgFrontend::GpgFileOpera::DecryptFile(const std::string& in_path, GpgBasicOperator::GetInstance().Decrypt( std::get<1>(read_result), [=](GpgError err, const DataObjectPtr& data_object) { - if (!data_object->Check<GpgEncryptResult, GFBuffer>()) { + if (!data_object->Check<GpgDecryptResult, GFBuffer>()) { throw std::runtime_error("data object transfers wrong arguments"); } - auto result = ExtractParams<GpgEncryptResult>(data_object, 0); + auto result = ExtractParams<GpgDecryptResult>(data_object, 0); auto buffer = ExtractParams<GFBuffer>(data_object, 1); if (CheckGpgError(err) == GPG_ERR_NO_ERROR && @@ -111,11 +113,9 @@ void GpgFrontend::GpgFileOpera::DecryptFile(const std::string& in_path, }); } -auto GpgFrontend::GpgFileOpera::SignFile(KeyListPtr keys, - const std::string& in_path, - const std::string& out_path, - GpgSignResult& result, int _channel) - -> gpgme_error_t { +void GpgFileOpera::SignFile(KeyArgsList keys, const std::string& in_path, + const std::string& out_path, bool ascii, + const GpgOperationCallback& cb) { #ifdef WINDOWS auto in_path_std = std::filesystem::path(QString::fromStdString(in_path).toStdU16String()); @@ -126,27 +126,31 @@ auto GpgFrontend::GpgFileOpera::SignFile(KeyListPtr keys, auto out_path_std = std::filesystem::path(out_path); #endif - std::string in_buffer; - if (!ReadFileStd(in_path_std, in_buffer)) { + auto read_result = ReadFileGFBuffer(in_path_std); + if (!std::get<0>(read_result)) { throw std::runtime_error("read file error"); } - ByteArrayPtr out_buffer; - auto err = GpgBasicOperator::GetInstance(_channel).Sign( - std::move(keys), in_buffer, out_buffer, GPGME_SIG_MODE_DETACH, result); - - if (CheckGpgError(err) == GPG_ERR_NO_ERROR) - if (!WriteFileStd(out_path_std, *out_buffer)) { - throw std::runtime_error("WriteBufferToFile error"); - }; + GpgBasicOperator::GetInstance().Sign( + std::move(keys), std::get<1>(read_result), GPGME_SIG_MODE_DETACH, ascii, + [=](GpgError err, const DataObjectPtr& data_object) { + if (!data_object->Check<GpgSignResult, GFBuffer>()) { + throw std::runtime_error("data object transfers wrong arguments"); + } + auto result = ExtractParams<GpgSignResult>(data_object, 0); + auto buffer = ExtractParams<GFBuffer>(data_object, 1); - return err; + if (CheckGpgError(err) == GPG_ERR_NO_ERROR && + !WriteFileGFBuffer(out_path_std, buffer)) { + throw std::runtime_error("write buffer to file error"); + } + cb(err, TransferParams(result)); + }); } -auto GpgFrontend::GpgFileOpera::VerifyFile(const std::string& data_path, - const std::string& sign_path, - GpgVerifyResult& result, - int _channel) -> gpgme_error_t { +void GpgFileOpera::VerifyFile(const std::string& data_path, + const std::string& sign_path, + const GpgOperationCallback& cb) { #ifdef WINDOWS auto data_path_std = std::filesystem::path(QString::fromStdString(data_path).toStdU16String()); @@ -157,27 +161,35 @@ auto GpgFrontend::GpgFileOpera::VerifyFile(const std::string& data_path, auto sign_path_std = std::filesystem::path(sign_path); #endif - std::string in_buffer; - if (!ReadFileStd(data_path_std, in_buffer)) { + auto read_result = ReadFileGFBuffer(data_path_std); + if (!std::get<0>(read_result)) { throw std::runtime_error("read file error"); } - ByteArrayPtr sign_buffer = nullptr; + + GFBuffer sign_buffer; if (!sign_path.empty()) { - std::string sign_buffer_str; - if (!ReadFileStd(sign_path_std, sign_buffer_str)) { + auto read_result = ReadFileGFBuffer(sign_path_std); + if (!std::get<0>(read_result)) { throw std::runtime_error("read file error"); } - sign_buffer = std::make_unique<std::string>(sign_buffer_str); + sign_buffer = std::get<1>(read_result); } - auto err = GpgBasicOperator::GetInstance(_channel).Verify( - in_buffer, sign_buffer, result); - return err; + + GpgBasicOperator::GetInstance().Verify( + std::get<1>(read_result), sign_buffer, + [=](GpgError err, const DataObjectPtr& data_object) { + if (!data_object->Check<GpgVerifyResult>()) { + throw std::runtime_error("data object transfers wrong arguments"); + } + auto result = ExtractParams<GpgVerifyResult>(data_object, 0); + cb(err, TransferParams(result)); + }); } -auto GpgFrontend::GpgFileOpera::EncryptSignFile( - KeyListPtr keys, KeyListPtr signer_keys, const std::string& in_path, - const std::string& out_path, GpgEncrResult& encr_res, - GpgSignResult& sign_res, int _channel) -> gpg_error_t { +void GpgFileOpera::EncryptSignFile(KeyArgsList keys, KeyArgsList signer_keys, + const std::string& in_path, + const std::string& out_path, bool ascii, + const GpgOperationCallback& cb) { #ifdef WINDOWS auto in_path_std = std::filesystem::path(QString::fromStdString(in_path).toStdU16String()); @@ -188,29 +200,32 @@ auto GpgFrontend::GpgFileOpera::EncryptSignFile( auto out_path_std = std::filesystem::path(out_path); #endif - std::string in_buffer; - if (!ReadFileStd(in_path_std, in_buffer)) { + auto read_result = ReadFileGFBuffer(in_path_std); + if (!std::get<0>(read_result)) { throw std::runtime_error("read file error"); } - ByteArrayPtr out_buffer = nullptr; - - auto err = GpgBasicOperator::GetInstance(_channel).EncryptSign( - std::move(keys), std::move(signer_keys), in_buffer, out_buffer, encr_res, - sign_res); - - if (CheckGpgError(err) == GPG_ERR_NO_ERROR) - if (!WriteFileStd(out_path_std, *out_buffer)) { - throw std::runtime_error("WriteBufferToFile error"); - }; - return err; + GpgBasicOperator::GetInstance().EncryptSign( + std::move(keys), std::move(signer_keys), std::get<1>(read_result), ascii, + [=](GpgError err, const DataObjectPtr& data_object) { + if (!data_object->Check<GpgEncryptResult, GpgSignResult, GFBuffer>()) { + throw std::runtime_error("data object transfers wrong arguments"); + } + auto encrypt_result = ExtractParams<GpgEncryptResult>(data_object, 0); + auto sign_result = ExtractParams<GpgSignResult>(data_object, 1); + auto buffer = ExtractParams<GFBuffer>(data_object, 2); + if (CheckGpgError(err) == GPG_ERR_NO_ERROR) { + if (!WriteFileGFBuffer(out_path_std, buffer)) { + throw std::runtime_error("write buffer to file error"); + } + } + cb(err, TransferParams(encrypt_result, sign_result)); + }); } -auto GpgFrontend::GpgFileOpera::DecryptVerifyFile(const std::string& in_path, - const std::string& out_path, - GpgDecrResult& decr_res, - GpgVerifyResult& verify_res) - -> gpg_error_t { +void GpgFileOpera::DecryptVerifyFile(const std::string& in_path, + const std::string& out_path, + const GpgOperationCallback& cb) { #ifdef WINDOWS auto in_path_std = std::filesystem::path(QString::fromStdString(in_path).toStdU16String()); @@ -221,25 +236,32 @@ auto GpgFrontend::GpgFileOpera::DecryptVerifyFile(const std::string& in_path, auto out_path_std = std::filesystem::path(out_path); #endif - std::string in_buffer; - if (!ReadFileStd(in_path_std, in_buffer)) { + auto read_result = ReadFileGFBuffer(in_path_std); + if (!std::get<0>(read_result)) { throw std::runtime_error("read file error"); } - ByteArrayPtr out_buffer = nullptr; - auto err = GpgBasicOperator::GetInstance().DecryptVerify( - in_buffer, out_buffer, decr_res, verify_res); - - if (CheckGpgError(err) == GPG_ERR_NO_ERROR) - if (!WriteFileStd(out_path_std, *out_buffer)) { - throw std::runtime_error("write file error"); - }; - - return err; + GpgBasicOperator::GetInstance().DecryptVerify( + std::get<1>(read_result), + [=](GpgError err, const DataObjectPtr& data_object) { + if (!data_object + ->Check<GpgDecryptResult, GpgVerifyResult, GFBuffer>()) { + throw std::runtime_error("data object transfers wrong arguments"); + } + auto decrypt_result = ExtractParams<GpgDecryptResult>(data_object, 0); + auto verify_result = ExtractParams<GpgVerifyResult>(data_object, 1); + auto buffer = ExtractParams<GFBuffer>(data_object, 2); + if (CheckGpgError(err) == GPG_ERR_NO_ERROR) { + if (!WriteFileGFBuffer(out_path_std, buffer)) { + throw std::runtime_error("write buffer to file error"); + } + } + cb(err, TransferParams(decrypt_result, verify_result)); + }); } -void GpgFrontend::GpgFileOpera::EncryptFileSymmetric( - const std::string& in_path, const std::string& out_path, bool ascii, - const GpgOperationCallback& cb) { +void GpgFileOpera::EncryptFileSymmetric(const std::string& in_path, + const std::string& out_path, bool ascii, + const GpgOperationCallback& cb) { #ifdef WINDOWS auto in_path_std = std::filesystem::path(QString::fromStdString(in_path).toStdU16String()); @@ -273,3 +295,5 @@ void GpgFrontend::GpgFileOpera::EncryptFileSymmetric( } }); } + +} // namespace GpgFrontend
\ No newline at end of file diff --git a/src/core/function/gpg/GpgFileOpera.h b/src/core/function/gpg/GpgFileOpera.h index ec34487c..0f0d65bf 100644 --- a/src/core/function/gpg/GpgFileOpera.h +++ b/src/core/function/gpg/GpgFileOpera.h @@ -50,7 +50,7 @@ class GPGFRONTEND_CORE_EXPORT GpgFileOpera { * @param channel Channel in context * @return unsigned int error code */ - static void EncryptFile(std::vector<GpgKey> keys, const std::string& in_path, + static void EncryptFile(KeyArgsList keys, const std::string& in_path, const std::string& out_path, bool ascii, const GpgOperationCallback& cb); @@ -89,9 +89,9 @@ class GPGFRONTEND_CORE_EXPORT GpgFileOpera { * @param channel * @return GpgError */ - static auto SignFile(KeyListPtr keys, const std::string& in_path, - const std::string& out_path, GpgSignResult& result, - int channel = kGpgFrontendDefaultChannel) -> GpgError; + static void SignFile(KeyArgsList keys, const std::string& in_path, + const std::string& out_path, bool ascii, + const GpgOperationCallback& cb); /** * @brief Verify file with public key @@ -102,9 +102,9 @@ class GPGFRONTEND_CORE_EXPORT GpgFileOpera { * @param channel Channel in context * @return GpgError */ - static auto VerifyFile(const std::string& data_path, - const std::string& sign_path, GpgVerifyResult& result, - int channel = kGpgFrontendDefaultChannel) -> GpgError; + static void VerifyFile(const std::string& data_path, + const std::string& sign_path, + const GpgOperationCallback& cb); /** * @brief Encrypt and sign file with public key and private key @@ -118,12 +118,10 @@ class GPGFRONTEND_CORE_EXPORT GpgFileOpera { * @param channel * @return GpgError */ - static auto EncryptSignFile(KeyListPtr keys, KeyListPtr signer_keys, + static void EncryptSignFile(KeyArgsList keys, KeyArgsList signer_keys, const std::string& in_path, - const std::string& out_path, - GpgEncrResult& encr_res, GpgSignResult& sign_res, - int channel = kGpgFrontendDefaultChannel) - -> GpgError; + const std::string& out_path, bool ascii, + const GpgOperationCallback& cb); /** * @brief @@ -134,10 +132,9 @@ class GPGFRONTEND_CORE_EXPORT GpgFileOpera { * @param verify_res * @return GpgError */ - static auto DecryptVerifyFile(const std::string& in_path, + static void DecryptVerifyFile(const std::string& in_path, const std::string& out_path, - GpgDecrResult& decr_res, - GpgVerifyResult& verify_res) -> GpgError; + const GpgOperationCallback& cb); }; } // namespace GpgFrontend |