0
0
mirror of https://github.com/obsproject/obs-studio.git synced 2024-09-20 13:08:50 +02:00
obs-studio/UI/win-update/updater/hash.cpp

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

78 lines
2.1 KiB
C++
Raw Normal View History

/*
2023-05-19 02:37:26 +02:00
* Copyright (c) 2023 Lain Bailey <lain@obsproject.com>
*
* Permission to use, copy, modify, and distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/
2017-02-20 13:50:17 +01:00
#include "updater.hpp"
#include <util/windows/WinHandle.hpp>
#include <vector>
using namespace std;
2023-05-01 13:15:37 +02:00
void HashToString(const B2Hash &in, string &out)
2017-02-20 13:50:17 +01:00
{
2023-08-26 23:42:02 +02:00
constexpr char alphabet[] = "0123456789abcdef";
2023-05-01 13:15:37 +02:00
out.resize(kBlake2StrLength);
2017-02-20 13:50:17 +01:00
2023-08-26 23:42:02 +02:00
for (size_t i = 0; i != kBlake2HashLength; ++i) {
2023-05-01 13:15:37 +02:00
out[2 * i] = alphabet[(uint8_t)in[i] / 16];
out[2 * i + 1] = alphabet[(uint8_t)in[i] % 16];
2017-02-20 13:50:17 +01:00
}
}
2023-05-01 13:15:37 +02:00
void StringToHash(const string &in, B2Hash &out)
2017-02-20 13:50:17 +01:00
{
unsigned int temp;
2023-05-01 13:15:37 +02:00
const char *str = in.c_str();
2017-02-20 13:50:17 +01:00
2023-08-26 23:42:02 +02:00
for (size_t i = 0; i < kBlake2HashLength; i++) {
2023-05-01 13:15:37 +02:00
sscanf_s(str + i * 2, "%02x", &temp);
2023-08-26 23:42:02 +02:00
out[i] = static_cast<std::byte>(temp);
2017-02-20 13:50:17 +01:00
}
}
2023-05-01 13:15:37 +02:00
bool CalculateFileHash(const wchar_t *path, B2Hash &hash)
2017-02-20 13:50:17 +01:00
{
static __declspec(thread) vector<BYTE> hashBuffer;
2017-02-20 13:50:17 +01:00
blake2b_state blake2;
2023-05-01 13:15:37 +02:00
if (blake2b_init(&blake2, kBlake2HashLength) != 0)
2017-02-20 13:50:17 +01:00
return false;
hashBuffer.resize(1048576);
2017-02-20 13:50:17 +01:00
WinHandle handle = CreateFileW(path, GENERIC_READ, FILE_SHARE_READ,
nullptr, OPEN_EXISTING, 0, nullptr);
if (handle == INVALID_HANDLE_VALUE)
return false;
for (;;) {
DWORD read = 0;
2023-08-26 23:42:02 +02:00
if (!ReadFile(handle, hashBuffer.data(),
(DWORD)hashBuffer.size(), &read, nullptr))
2017-02-20 13:50:17 +01:00
return false;
if (!read)
break;
2023-08-26 23:42:02 +02:00
if (blake2b_update(&blake2, hashBuffer.data(), read) != 0)
2017-02-20 13:50:17 +01:00
return false;
}
2023-08-26 23:42:02 +02:00
if (blake2b_final(&blake2, hash.data(), hash.size()) != 0)
2017-02-20 13:50:17 +01:00
return false;
return true;
}