-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGostHashUtil.cpp
More file actions
88 lines (79 loc) · 1.99 KB
/
Copy pathGostHashUtil.cpp
File metadata and controls
88 lines (79 loc) · 1.99 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
#include "GostHashUtil.h"
#include <windows.h>
#include <strsafe.h>
#include <array>
#include <vector>
extern "C"
{
#include "gost89.h"
#include "gosthash.h"
extern gost_subst_block GostR3411_94_TestParamSet;
}
namespace
{
std::wstring HexEncodeUpper(const byte* data, size_t size)
{
static const wchar_t digits[] = L"0123456789ABCDEF";
std::wstring out;
out.reserve(size * 2);
for (size_t i = 0; i < size; ++i)
{
byte b = data[i];
out.push_back(digits[(b >> 4) & 0xF]);
out.push_back(digits[b & 0xF]);
}
return out;
}
}
bool ComputeGostHashFile(const std::wstring& filePath, std::wstring& outHex)
{
gost_hash_ctx ctx{};
if (!init_gost_hash_ctx(&ctx, &GostR3411_94_TestParamSet))
{
return false;
}
if (!start_hash(&ctx))
{
done_gost_hash_ctx(&ctx);
return false;
}
HANDLE hFile = CreateFileW(filePath.c_str(), GENERIC_READ, FILE_SHARE_READ, nullptr,
OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, nullptr);
if (hFile == INVALID_HANDLE_VALUE)
{
done_gost_hash_ctx(&ctx);
return false;
}
const DWORD chunk = 64 * 1024;
std::vector<byte> buffer(chunk);
for (;;)
{
DWORD bytesRead = 0;
if (!ReadFile(hFile, buffer.data(), chunk, &bytesRead, nullptr))
{
CloseHandle(hFile);
done_gost_hash_ctx(&ctx);
return false;
}
if (bytesRead == 0)
{
break;
}
if (!hash_block(&ctx, buffer.data(), bytesRead))
{
CloseHandle(hFile);
done_gost_hash_ctx(&ctx);
return false;
}
}
CloseHandle(hFile);
std::array<byte, 32> digest{};
if (!finish_hash(&ctx, digest.data()))
{
done_gost_hash_ctx(&ctx);
return false;
}
done_gost_hash_ctx(&ctx);
outHex = HexEncodeUpper(digest.data(), digest.size());
return true;
}