MD5Digest: Span-ify

This commit is contained in:
Stenzek 2024-07-18 17:33:15 +10:00
parent 41e15539f9
commit 7217536fdd
No known key found for this signature in database
5 changed files with 33 additions and 9 deletions

View file

@ -137,6 +137,16 @@ void MD5Digest::Reset()
this->bits[1] = 0;
}
std::array<u8, MD5Digest::DIGEST_SIZE> MD5Digest::HashData(std::span<const u8> data)
{
std::array<u8, DIGEST_SIZE> ret;
MD5Digest digest;
digest.Update(data);
digest.Final(ret);
return ret;
}
void MD5Digest::Update(const void* pData, u32 cbData)
{
u32 t;
@ -185,7 +195,13 @@ void MD5Digest::Update(const void* pData, u32 cbData)
std::memcpy(this->in, pByteData, cbData);
}
void MD5Digest::Final(u8 Digest[16])
void MD5Digest::Update(std::span<const u8> data)
{
if (!data.empty())
Update(data.data(), static_cast<u32>(data.size_bytes()));
}
void MD5Digest::Final(std::span<u8, DIGEST_SIZE> digest)
{
u32 count;
u8* p;
@ -225,5 +241,5 @@ void MD5Digest::Final(u8 Digest[16])
MD5Transform(this->buf, (u32*)this->in);
byteReverse((unsigned char*)this->buf, 4);
std::memcpy(Digest, this->buf, 16);
std::memcpy(digest.data(), this->buf, 16);
}

View file

@ -1,18 +1,26 @@
// SPDX-FileCopyrightText: 2019-2022 Connor McLaughlin <stenzek@gmail.com>
// SPDX-FileCopyrightText: 2019-2024 Connor McLaughlin <stenzek@gmail.com>
// SPDX-License-Identifier: (GPL-3.0 OR CC-BY-NC-ND-4.0)
#pragma once
#include "types.h"
#include <array>
#include <span>
class MD5Digest
{
public:
static constexpr u32 DIGEST_SIZE = 16;
MD5Digest();
void Update(const void* pData, u32 cbData);
void Final(u8 Digest[16]);
void Update(std::span<const u8> data);
void Final(std::span<u8, DIGEST_SIZE> digest);
void Reset();
static std::array<u8, DIGEST_SIZE> HashData(std::span<const u8> data);
private:
u32 buf[4];
u32 bits[2];

View file

@ -292,7 +292,7 @@ std::string Achievements::GetGameHash(CDImage* image)
MD5Digest digest;
digest.Update(executable_name.c_str(), static_cast<u32>(executable_name.size()));
if (hash_size > 0)
digest.Update(executable_data.data(), hash_size);
digest.Update(executable_data);
u8 hash[16];
digest.Final(hash);

View file

@ -163,7 +163,7 @@ BIOS::Hash BIOS::GetImageHash(const BIOS::Image& image)
{
BIOS::Hash hash;
MD5Digest digest;
digest.Update(image.data(), static_cast<u32>(image.size()));
digest.Update(image);
digest.Final(hash.bytes);
return hash;
}

View file

@ -45,7 +45,7 @@ bool CDImageHasher::ReadIndex(CDImage* image, u8 track, u8 index, MD5Digest* dig
return false;
}
digest->Update(sector.data(), static_cast<u32>(sector.size()));
digest->Update(sector);
}
progress_callback->SetProgressValue(index_length);
@ -126,7 +126,7 @@ bool CDImageHasher::GetImageHash(CDImage* image, Hash* out_hash,
}
progress_callback->SetProgressValue(image->GetTrackCount());
digest.Final(out_hash->data());
digest.Final(*out_hash);
return true;
}
@ -137,6 +137,6 @@ bool CDImageHasher::GetTrackHash(CDImage* image, u8 track, Hash* out_hash,
if (!ReadTrack(image, track, &digest, progress_callback))
return false;
digest.Final(out_hash->data());
digest.Final(*out_hash);
return true;
}