Skip to content

Commit

Permalink
Merge remote-tracking branch 'local/gpg_decrypt_from_buffer'
Browse files Browse the repository at this point in the history
  • Loading branch information
CodiePP committed Nov 27, 2023
2 parents bbbda26 + 4c4121f commit 337e7af
Show file tree
Hide file tree
Showing 5 changed files with 43 additions and 5 deletions.
4 changes: 2 additions & 2 deletions build/test/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ set(Boost_DEBUG OFF)
set(Boost_USE_STATIC_LIBS OFF)
set(Boost_USE_STATIC_RUNTIME OFF)
set(Boost_USE_MULTITHREADED ON)
find_package( Boost 1.74.0 REQUIRED COMPONENTS system date_time unit_test_framework contract)
#include_directories(${Boost_INCLUDE_DIRS})
find_package( Boost 1.83.0 REQUIRED COMPONENTS unit_test_framework)
include_directories(${Boost_INCLUDE_DIRS})

# mine
include_directories(../src)
Expand Down
2 changes: 2 additions & 0 deletions src/cpp/gpg.hpp.md
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,8 @@ namespace [lxr](namespace.list) {

>bool [decrypt_from_file](gpg_functions.cpp.md)(std::string const & fpath);

>std::optional<std::string> [decrypt_from_buffer](gpg_functions.cpp.md)(std::string const & buffer);

>private:

>Gpg(Gpg const &) = delete;
Expand Down
26 changes: 26 additions & 0 deletions src/cpp/gpg_ctor.cpp.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ struct Gpg::pimpl {
std::ostream& ostream() { oss.reset(new std::ostringstream()); return *oss; }
std::istream& istream() { return *iss; }
bool decrypt_from_file(std::string const & fpath);
std::optional<std::string> decrypt_from_buffer(std::string const & buffer);

private:
gpgme_ctx_t ctx{nullptr};
Expand Down Expand Up @@ -181,6 +182,31 @@ bool Gpg::pimpl::decrypt_from_file(std::string const &p_fp)
return true;
}

std::optional<std::string> Gpg::pimpl::decrypt_from_buffer(std::string const &p_buffer)
{
gpgme_error_t err;
gpgme_data_t plain, cipher;
err = gpgme_data_new_from_mem(&cipher, p_buffer.c_str(), p_buffer.size(), 0);
if (err != GPG_ERR_NO_ERROR) { OUTPUT_GPGME_ERROR(err); return {}; }
err = gpgme_data_new(&plain);
if (err != GPG_ERR_NO_ERROR) { OUTPUT_GPGME_ERROR(err); gpgme_data_release(cipher); return {}; }
auto cleanup_data = [&](void) {
gpgme_data_release(plain);
gpgme_data_release(cipher);
};

err = gpgme_new(&ctx);
if (err != GPG_ERR_NO_ERROR) { OUTPUT_GPGME_ERROR(err); cleanup_data(); return {}; }

err = gpgme_op_decrypt(ctx, cipher, plain);
if (err != GPG_ERR_NO_ERROR) { OUTPUT_GPGME_ERROR(err); cleanup_data(); gpgme_release(ctx); return {}; }

gpgme_data_release(cipher);
size_t mlen;
const char *mptr = gpgme_data_release_and_get_mem(plain, &mlen);
return std::string(mptr, mlen);
}

Gpg::Gpg()
: _pimpl(std::make_unique<pimpl>())
{
Expand Down
5 changes: 5 additions & 0 deletions src/cpp/gpg_functions.cpp.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,4 +37,9 @@ bool Gpg::decrypt_from_file(std::string const &fp)
return _pimpl->decrypt_from_file(fp);
}

std::optional<std::string> Gpg::decrypt_from_buffer(std::string const &b)
{
return _pimpl->decrypt_from_buffer(b);
}

```
11 changes: 8 additions & 3 deletions test/cpp/utGpg.cpp.md
Original file line number Diff line number Diff line change
Expand Up @@ -52,11 +52,16 @@ BOOST_AUTO_TEST_CASE( gpg_check_non_private_key )
BOOST_TEST_WARN(!gpg.has_private_key(addr), "the archiving process should not have access to this private key.");
}

BOOST_AUTO_TEST_CASE( gpg_encrypt_string )
BOOST_AUTO_TEST_CASE( gpg_encrypt_decrypt_string )
{
lxr::Gpg gpg;
auto s = gpg.encrypt_to_key(addr, "hello world.");
BOOST_CHECK(s);
std::optional<std::string> cipher = gpg.encrypt_to_key(addr, "hello world.");
BOOST_CHECK(cipher);
if (cipher) {
std::optional<std::string> plain = gpg.decrypt_from_buffer(cipher.value());
BOOST_CHECK(plain);
BOOST_CHECK_EQUAL("hello world.", plain.value());
}
}

BOOST_AUTO_TEST_CASE( gpg_encrypt_stream )
Expand Down

0 comments on commit 337e7af

Please sign in to comment.