mirror of
https://github.com/transmission/transmission.git
synced 2026-04-20 00:40:03 +01:00
refactor: aggregate crypto handles when computing digests (#6662)
* refactor: aggregate per-crypto-pkg fields instead of using inheritance
This commit is contained in:
@@ -22,6 +22,10 @@
|
||||
#include "libtransmission/tr-assert.h"
|
||||
#include "libtransmission/utils.h"
|
||||
|
||||
#ifndef WITH_WOLFSSL
|
||||
#error wolfssl module
|
||||
#endif
|
||||
|
||||
#if LIBWOLFSSL_VERSION_HEX >= 0x04000000 // 4.0.0
|
||||
using TR_WC_RNG = WC_RNG;
|
||||
#else
|
||||
@@ -85,88 +89,66 @@ TR_WC_RNG* get_rng()
|
||||
|
||||
std::mutex rng_mutex_;
|
||||
|
||||
// ---
|
||||
|
||||
class Sha1Impl final : public tr_sha1
|
||||
{
|
||||
public:
|
||||
Sha1Impl()
|
||||
{
|
||||
clear();
|
||||
}
|
||||
|
||||
~Sha1Impl() override = default;
|
||||
|
||||
void clear() override
|
||||
{
|
||||
wc_InitSha(&handle_);
|
||||
}
|
||||
|
||||
void add(void const* data, size_t data_length) override
|
||||
{
|
||||
if (data_length > 0U)
|
||||
{
|
||||
wc_ShaUpdate(&handle_, static_cast<byte const*>(data), data_length);
|
||||
}
|
||||
}
|
||||
|
||||
[[nodiscard]] tr_sha1_digest_t finish() override
|
||||
{
|
||||
auto digest = tr_sha1_digest_t{};
|
||||
wc_ShaFinal(&handle_, reinterpret_cast<byte*>(std::data(digest)));
|
||||
clear();
|
||||
return digest;
|
||||
}
|
||||
|
||||
private:
|
||||
wc_Sha handle_ = {};
|
||||
};
|
||||
|
||||
class Sha256Impl final : public tr_sha256
|
||||
{
|
||||
public:
|
||||
Sha256Impl()
|
||||
{
|
||||
clear();
|
||||
}
|
||||
|
||||
~Sha256Impl() override = default;
|
||||
|
||||
void clear() override
|
||||
{
|
||||
wc_InitSha256(&handle_);
|
||||
}
|
||||
|
||||
void add(void const* data, size_t data_length) override
|
||||
{
|
||||
if (data_length > 0U)
|
||||
{
|
||||
wc_Sha256Update(&handle_, static_cast<byte const*>(data), data_length);
|
||||
}
|
||||
}
|
||||
|
||||
[[nodiscard]] tr_sha256_digest_t finish() override
|
||||
{
|
||||
auto digest = tr_sha256_digest_t{};
|
||||
wc_Sha256Final(&handle_, reinterpret_cast<byte*>(std::data(digest)));
|
||||
clear();
|
||||
return digest;
|
||||
}
|
||||
|
||||
private:
|
||||
wc_Sha256 handle_ = {};
|
||||
};
|
||||
|
||||
} // namespace
|
||||
|
||||
std::unique_ptr<tr_sha1> tr_sha1::create()
|
||||
// --- sha1
|
||||
|
||||
tr_sha1::tr_sha1()
|
||||
{
|
||||
return std::make_unique<Sha1Impl>();
|
||||
clear();
|
||||
}
|
||||
|
||||
std::unique_ptr<tr_sha256> tr_sha256::create()
|
||||
tr_sha1::~tr_sha1() = default;
|
||||
|
||||
void tr_sha1::clear()
|
||||
{
|
||||
return std::make_unique<Sha256Impl>();
|
||||
wc_InitSha(&handle_);
|
||||
}
|
||||
|
||||
void tr_sha1::add(void const* data, size_t data_length)
|
||||
{
|
||||
if (data_length > 0U)
|
||||
{
|
||||
wc_ShaUpdate(&handle_, static_cast<byte const*>(data), data_length);
|
||||
}
|
||||
}
|
||||
|
||||
tr_sha1_digest_t tr_sha1::finish()
|
||||
{
|
||||
auto digest = tr_sha1_digest_t{};
|
||||
wc_ShaFinal(&handle_, reinterpret_cast<byte*>(std::data(digest)));
|
||||
clear();
|
||||
return digest;
|
||||
}
|
||||
|
||||
// --- sha256
|
||||
|
||||
tr_sha256::tr_sha256()
|
||||
{
|
||||
clear();
|
||||
}
|
||||
|
||||
tr_sha256::~tr_sha256() = default;
|
||||
|
||||
void tr_sha256::clear()
|
||||
{
|
||||
wc_InitSha256(&handle_);
|
||||
}
|
||||
|
||||
void tr_sha256::add(void const* data, size_t data_length)
|
||||
{
|
||||
if (data_length > 0U)
|
||||
{
|
||||
wc_Sha256Update(&handle_, static_cast<byte const*>(data), data_length);
|
||||
}
|
||||
}
|
||||
|
||||
tr_sha256_digest_t tr_sha256::finish()
|
||||
{
|
||||
auto digest = tr_sha256_digest_t{};
|
||||
wc_Sha256Final(&handle_, reinterpret_cast<byte*>(std::data(digest)));
|
||||
clear();
|
||||
return digest;
|
||||
}
|
||||
|
||||
// ---
|
||||
|
||||
Reference in New Issue
Block a user