mirror of
https://github.com/dolphin-emu/dolphin.git
synced 2026-10-24 03:29:02 +02:00
Common: Create MD5Context class and ComputeMD5 function to minimize exposure to mbedtls throughout the project.
This commit is contained in:
1 parent
cf5cb7ef81
commit
db5bbbd6cf
2 files changed
+54
No files matched your search
@@ -9,6 +9,8 @@
|
||||
|
||||
#include <zlib.h>
|
||||
|
||||
#include <mbedtls/md5.h>
|
||||
|
||||
#include "Common/CPUDetect.h"
|
||||
#include "Common/Intrinsics.h"
|
||||
|
||||
@@ -457,4 +459,34 @@ u32 ComputeCRC32(std::string_view data)
|
||||
{
|
||||
return ComputeCRC32(reinterpret_cast<const u8*>(data.data()), data.size());
|
||||
}
|
||||
|
||||
struct MD5Context::Impl
|
||||
{
|
||||
mbedtls_md5_context context;
|
||||
};
|
||||
|
||||
MD5Context::MD5Context() : m_impl{std::make_unique<Impl>()}
|
||||
{
|
||||
mbedtls_md5_init(&m_impl->context);
|
||||
mbedtls_md5_starts_ret(&m_impl->context);
|
||||
}
|
||||
|
||||
// FYI: mbedtls_md5_free is unnecessary. It just zeroes the struct.
|
||||
MD5Context::~MD5Context() = default;
|
||||
|
||||
void MD5Context::Update(std::span<const u8> data)
|
||||
{
|
||||
mbedtls_md5_update_ret(&m_impl->context, data.data(), data.size());
|
||||
}
|
||||
|
||||
void MD5Context::Finish(u8* output)
|
||||
{
|
||||
mbedtls_md5_finish_ret(&m_impl->context, output);
|
||||
}
|
||||
|
||||
void ComputeMD5(std::span<const u8> input, u8* output)
|
||||
{
|
||||
mbedtls_md5_ret(input.data(), input.size(), output);
|
||||
}
|
||||
|
||||
} // namespace Common
|
||||
@@ -4,6 +4,8 @@
|
||||
#pragma once
|
||||
|
||||
#include <cstddef>
|
||||
#include <memory>
|
||||
#include <span>
|
||||
#include <string_view>
|
||||
|
||||
#include "Common/CommonTypes.h"
|
||||
@@ -21,4 +23,24 @@ u32 StartCRC32();
|
||||
u32 UpdateCRC32(u32 crc, const u8* data, size_t len);
|
||||
u32 ComputeCRC32(const u8* data, size_t len);
|
||||
u32 ComputeCRC32(std::string_view data);
|
||||
|
||||
class MD5Context
|
||||
{
|
||||
public:
|
||||
MD5Context();
|
||||
~MD5Context();
|
||||
|
||||
void Update(std::span<const u8> data);
|
||||
|
||||
// output should point to 16 bytes.
|
||||
void Finish(u8* output);
|
||||
|
||||
private:
|
||||
struct Impl;
|
||||
std::unique_ptr<Impl> m_impl;
|
||||
};
|
||||
|
||||
// output should point to 16 bytes.
|
||||
void ComputeMD5(std::span<const u8> input, u8* output);
|
||||
|
||||
} // namespace Common
|
||||
Reference in new issue
Block a user