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

62 lines
1.2 KiB
C++
Raw Normal View History

2017-02-20 13:50:17 +01:00
#include "updater.hpp"
#include <util/windows/WinHandle.hpp>
#include <vector>
using namespace std;
void HashToString(const uint8_t *in, wchar_t *out)
{
const wchar_t alphabet[] = L"0123456789abcdef";
for (int i = 0; i != BLAKE2_HASH_LENGTH; ++i) {
out[2 * i] = alphabet[in[i] / 16];
out[2 * i + 1] = alphabet[in[i] % 16];
}
out[BLAKE2_HASH_LENGTH * 2] = 0;
}
void StringToHash(const wchar_t *in, BYTE *out)
{
int temp;
for (int i = 0; i < BLAKE2_HASH_LENGTH; i++) {
swscanf_s(in + i * 2, L"%02x", &temp);
out[i] = (BYTE)temp;
}
}
bool CalculateFileHash(const wchar_t *path, BYTE *hash)
{
blake2b_state blake2;
if (blake2b_init(&blake2, BLAKE2_HASH_LENGTH) != 0)
return false;
WinHandle handle = CreateFileW(path, GENERIC_READ, FILE_SHARE_READ,
nullptr, OPEN_EXISTING, 0, nullptr);
if (handle == INVALID_HANDLE_VALUE)
return false;
vector<BYTE> buf;
buf.resize(65536);
for (;;) {
DWORD read = 0;
if (!ReadFile(handle, buf.data(), (DWORD)buf.size(), &read,
nullptr))
return false;
if (!read)
break;
if (blake2b_update(&blake2, buf.data(), read) != 0)
return false;
}
if (blake2b_final(&blake2, hash, BLAKE2_HASH_LENGTH) != 0)
return false;
return true;
}