diff options
author | Saturneric <[email protected]> | 2021-09-05 15:02:19 +0000 |
---|---|---|
committer | Saturneric <[email protected]> | 2021-09-05 15:02:19 +0000 |
commit | 39440522111abf3adeef6b56cb23722119fbf3c2 (patch) | |
tree | 5979b3808d58c8c576e288004877d826556b4cda /src/gpg/function/GpgFileOpera.cpp | |
parent | Modified ResultAnalyse. (diff) | |
download | GpgFrontend-39440522111abf3adeef6b56cb23722119fbf3c2.tar.gz GpgFrontend-39440522111abf3adeef6b56cb23722119fbf3c2.zip |
Rewrite the core.
Adjust the structure.
Diffstat (limited to '')
-rw-r--r-- | src/gpg/function/GpgFileOpera.cpp | 262 |
1 files changed, 262 insertions, 0 deletions
diff --git a/src/gpg/function/GpgFileOpera.cpp b/src/gpg/function/GpgFileOpera.cpp new file mode 100644 index 00000000..50e4e932 --- /dev/null +++ b/src/gpg/function/GpgFileOpera.cpp @@ -0,0 +1,262 @@ +/** + * 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 "gpg/function/GpgFileOpera.h" +#include "gpg/function/BasicOperator.h" +#include <memory> + +GpgFrontend::GpgError GpgFrontend::GpgFileOpera::EncryptFile( + KeyArgsList &keys, const std::string &path, GpgEncrResult &result) { + + QFileInfo file_info(path.c_str()); + + if (!file_info.isFile() || !file_info.isReadable()) + throw std::runtime_error("no permission"); + + QFile in_file(path.c_str()); + if (!in_file.open(QIODevice::ReadOnly)) + throw std::runtime_error("cannot open file"); + + QByteArray in_buffer = in_file.readAll(); + auto out_buffer = std::make_unique<QByteArray>(); + in_file.close(); + + auto err = + BasicOperator::GetInstance().Encrypt(keys, in_buffer, out_buffer, result); + + if (gpg_err_code(err) != GPG_ERR_NO_ERROR) + return err; + + QFile out_file((path + ".asc").c_str()); + + if (!out_file.open(QFile::WriteOnly)) + throw std::runtime_error("cannot open file"); + + QDataStream out(&out_file); + out.writeRawData(out_buffer->data(), out_buffer->length()); + out_file.close(); + return err; +} + +GpgFrontend::GpgError +GpgFrontend::GpgFileOpera::DecryptFile(const QString &path, + GpgDecrResult &result) { + + QFileInfo file_info(path); + + if (!file_info.isFile() || !file_info.isReadable()) + throw std::runtime_error("no permission"); + + QFile in_file(path); + if (!in_file.open(QIODevice::ReadOnly)) + throw std::runtime_error("cannot open file"); + + QByteArray in_buffer = in_file.readAll(); + auto out_buffer = std::make_unique<QByteArray>(); + in_file.close(); + + auto err = + BasicOperator::GetInstance().Decrypt(in_buffer, out_buffer, result); + + assert(gpgme_err_code(err) == GPG_ERR_NO_ERROR); + + QString out_file_name, file_extension = file_info.suffix(); + + if (file_extension == "asc" || file_extension == "gpg") { + int pos = path.lastIndexOf('.'); + out_file_name = path.left(pos); + } else + out_file_name = path + ".out"; + + QFile out_file(out_file_name); + + if (!out_file.open(QFile::WriteOnly)) + throw std::runtime_error("cannot open file"); + + QDataStream out(&out_file); + out.writeRawData(out_buffer->data(), out_buffer->length()); + out_file.close(); + + return err; +} + +gpgme_error_t GpgFrontend::GpgFileOpera::SignFile(KeyArgsList &keys, + const std::string &path, + GpgSignResult &result) { + + QFileInfo file_info(path.c_str()); + + if (!file_info.isFile() || !file_info.isReadable()) + throw std::runtime_error("no permission"); + + QFile in_file(path.c_str()); + if (!in_file.open(QIODevice::ReadOnly)) + throw std::runtime_error("cannot open file"); + + QByteArray in_buffer = in_file.readAll(); + auto out_buffer = std::make_unique<QByteArray>(); + in_file.close(); + + auto err = BasicOperator::GetInstance().Sign(keys, in_buffer, out_buffer, + GPGME_SIG_MODE_DETACH, result); + assert(gpgme_err_code(err) == GPG_ERR_NO_ERROR); + + QFile out_file((path + ".sig").c_str()); + + if (!out_file.open(QFile::WriteOnly)) + throw std::runtime_error("cannot open file"); + + QDataStream out(&out_file); + out.writeRawData(out_buffer->data(), out_buffer->length()); + out_file.close(); + + return err; +} + +gpgme_error_t GpgFrontend::GpgFileOpera::VerifyFile(const std::string &path, + GpgVerifyResult &result) { + + qDebug() << "Verify File Path" << path.c_str(); + + QFileInfo file_info(path.c_str()); + + if (!file_info.isFile() || !file_info.isReadable()) + throw std::runtime_error("no permission"); + + QFile in_file(path.c_str()); + if (!in_file.open(QIODevice::ReadOnly)) + throw std::runtime_error("cannot open file"); + + QByteArray in_buffer = in_file.readAll(); + std::unique_ptr<QByteArray> sign_buffer = nullptr; + + if (file_info.suffix() == "gpg") { + auto err = + BasicOperator::GetInstance().Verify(in_buffer, sign_buffer, result); + return err; + } else { + QFile sign_file; + sign_file.setFileName((path + ".sig").c_str()); + if (!sign_file.open(QIODevice::ReadOnly)) { + throw std::runtime_error("cannot open file"); + } + + auto sign_buffer = std::make_unique<QByteArray>(sign_file.readAll()); + in_file.close(); + + auto err = + BasicOperator::GetInstance().Verify(in_buffer, sign_buffer, result); + return err; + } +} + +// TODO + +gpg_error_t GpgFrontend::GpgFileOpera::EncryptSignFile( + KeyArgsList &keys, const std::string &path, GpgEncrResult &encr_res, + GpgSignResult &sign_res) { + + qDebug() << "Encrypt Sign File Path" << path.c_str(); + + QFileInfo file_info(path.c_str()); + + if (!file_info.isFile() || !file_info.isReadable()) + throw std::runtime_error("no permission"); + + QFile in_file; + in_file.setFileName(path.c_str()); + if (!in_file.open(QIODevice::ReadOnly)) + throw std::runtime_error("cannot open file"); + + QByteArray in_buffer = in_file.readAll(); + in_file.close(); + std::unique_ptr<QByteArray> out_buffer = nullptr; + + // TODO Fill the vector + std::vector<GpgKey> signerKeys; + + // TODO dealing with signer keys + auto err = BasicOperator::GetInstance().EncryptSign( + keys, signerKeys, in_buffer, out_buffer, encr_res, sign_res); + + if (gpg_err_code(err) != GPG_ERR_NO_ERROR) + return err; + + QFile out_file((path + ".gpg").c_str()); + + if (!out_file.open(QFile::WriteOnly)) + throw std::runtime_error("cannot open file"); + + QDataStream out(&out_file); + out.writeRawData(out_buffer->data(), out_buffer->length()); + out_file.close(); + + return err; +} + +gpg_error_t +GpgFrontend::GpgFileOpera::DecryptVerifyFile(const std::string &path, + GpgDecrResult &decr_res, + GpgVerifyResult &verify_res) { + + qDebug() << "Decrypt Verify File Path" << path.c_str(); + + QFileInfo file_info(path.c_str()); + + if (!file_info.isFile() || !file_info.isReadable()) + throw std::runtime_error("no permission"); + + QFile in_file; + in_file.setFileName(path.c_str()); + if (!in_file.open(QIODevice::ReadOnly)) + throw std::runtime_error("cannot open file"); + + QByteArray in_buffer = in_file.readAll(); + in_file.close(); + std::unique_ptr<QByteArray> out_buffer = nullptr; + + auto err = BasicOperator::GetInstance().DecryptVerify(in_buffer, out_buffer, + decr_res, verify_res); + if (gpg_err_code(err) != GPG_ERR_NO_ERROR) + return err; + + std::string out_file_name, + file_extension = file_info.suffix().toUtf8().constData(); + + if (file_extension == "asc" || file_extension == "gpg") { + int pos = path.find_last_of('.'); + out_file_name = path.substr(0, pos); + } else + out_file_name = (path + ".out").c_str(); + + QFile out_file(out_file_name.c_str()); + + if (!out_file.open(QFile::WriteOnly)) + throw std::runtime_error("cannot open file"); + + QDataStream out(&out_file); + out.writeRawData(out_buffer->data(), out_buffer->length()); + out_file.close(); + + return err; +} |