mirror of
https://github.com/dolphin-emu/dolphin.git
synced 2025-07-21 21:30:19 -06:00
introduce wrapper for SHA1 functionality
This commit is contained in:
51
Source/Core/Common/Crypto/SHA1.cpp
Normal file
51
Source/Core/Common/Crypto/SHA1.cpp
Normal file
@ -0,0 +1,51 @@
|
||||
// Copyright 2017 Dolphin Emulator Project
|
||||
// SPDX-License-Identifier: GPL-2.0-or-later
|
||||
|
||||
#include "SHA1.h"
|
||||
|
||||
#include <memory>
|
||||
|
||||
#include <mbedtls/sha1.h>
|
||||
|
||||
#include "Common/Assert.h"
|
||||
#include "Common/CPUDetect.h"
|
||||
#include "Common/CommonTypes.h"
|
||||
|
||||
namespace Common::SHA1
|
||||
{
|
||||
class ContextMbed final : public Context
|
||||
{
|
||||
public:
|
||||
ContextMbed()
|
||||
{
|
||||
mbedtls_sha1_init(&ctx);
|
||||
ASSERT(!mbedtls_sha1_starts_ret(&ctx));
|
||||
}
|
||||
~ContextMbed() { mbedtls_sha1_free(&ctx); }
|
||||
virtual void Update(const u8* msg, size_t len) override
|
||||
{
|
||||
ASSERT(!mbedtls_sha1_update_ret(&ctx, msg, len));
|
||||
}
|
||||
virtual Digest Finish() override
|
||||
{
|
||||
Digest digest;
|
||||
ASSERT(!mbedtls_sha1_finish_ret(&ctx, digest.data()));
|
||||
return digest;
|
||||
}
|
||||
|
||||
private:
|
||||
mbedtls_sha1_context ctx{};
|
||||
};
|
||||
|
||||
std::unique_ptr<Context> CreateContext()
|
||||
{
|
||||
return std::make_unique<ContextMbed>();
|
||||
}
|
||||
|
||||
Digest CalculateDigest(const u8* msg, size_t len)
|
||||
{
|
||||
auto ctx = CreateContext();
|
||||
ctx->Update(msg, len);
|
||||
return ctx->Finish();
|
||||
}
|
||||
} // namespace Common::SHA1
|
53
Source/Core/Common/Crypto/SHA1.h
Normal file
53
Source/Core/Common/Crypto/SHA1.h
Normal file
@ -0,0 +1,53 @@
|
||||
// Copyright 2017 Dolphin Emulator Project
|
||||
// SPDX-License-Identifier: GPL-2.0-or-later
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <array>
|
||||
#include <limits>
|
||||
#include <memory>
|
||||
#include <string_view>
|
||||
#include <type_traits>
|
||||
#include <vector>
|
||||
|
||||
#include "Common/Assert.h"
|
||||
#include "Common/CommonTypes.h"
|
||||
|
||||
namespace Common::SHA1
|
||||
{
|
||||
using Digest = std::array<u8, 160 / 8>;
|
||||
static constexpr size_t DIGEST_LEN = sizeof(Digest);
|
||||
|
||||
class Context
|
||||
{
|
||||
public:
|
||||
virtual ~Context() = default;
|
||||
virtual void Update(const u8* msg, size_t len) = 0;
|
||||
void Update(const std::vector<u8>& msg) { return Update(msg.data(), msg.size()); }
|
||||
virtual Digest Finish() = 0;
|
||||
};
|
||||
|
||||
std::unique_ptr<Context> CreateContext();
|
||||
|
||||
Digest CalculateDigest(const u8* msg, size_t len);
|
||||
|
||||
template <typename T>
|
||||
inline Digest CalculateDigest(const std::vector<T>& msg)
|
||||
{
|
||||
static_assert(std::is_trivially_copyable_v<T>);
|
||||
ASSERT(std::numeric_limits<size_t>::max() / sizeof(T) >= msg.size());
|
||||
return CalculateDigest(reinterpret_cast<const u8*>(msg.data()), sizeof(T) * msg.size());
|
||||
}
|
||||
|
||||
inline Digest CalculateDigest(const std::string_view& msg)
|
||||
{
|
||||
return CalculateDigest(reinterpret_cast<const u8*>(msg.data()), msg.size());
|
||||
}
|
||||
|
||||
template <typename T, size_t Size>
|
||||
inline Digest CalculateDigest(const std::array<T, Size>& msg)
|
||||
{
|
||||
static_assert(std::is_trivially_copyable_v<T>);
|
||||
return CalculateDigest(reinterpret_cast<const u8*>(msg.data()), sizeof(msg));
|
||||
}
|
||||
} // namespace Common::SHA1
|
Reference in New Issue
Block a user