Created
July 11, 2024 22:13
-
-
Save dextercd/5e8f6f1c187089f93bab11f537f7b047 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <iostream> | |
#include <stdio.h> | |
#define WIN32_LEAN_AND_MEAN | |
#include <windows.h> | |
#include <bcrypt.h> | |
#include <ntstatus.h> | |
void check(NTSTATUS status) | |
{ | |
if (status != STATUS_SUCCESS) { | |
ExitProcess(1); | |
} | |
} | |
int main(int argc, char** argv) | |
{ | |
BCRYPT_ALG_HANDLE alg; | |
check(BCryptOpenAlgorithmProvider( | |
&alg, | |
L"SHA1", | |
nullptr, | |
0 | |
)); | |
BCRYPT_HASH_HANDLE hash_handle; | |
check(BCryptCreateHash( | |
alg, | |
&hash_handle, | |
nullptr, | |
0, | |
nullptr, | |
0, | |
0 | |
)); | |
DWORD property_size; | |
DWORD hash_size; | |
check(BCryptGetProperty( | |
alg, | |
BCRYPT_HASH_LENGTH, | |
(PBYTE)&hash_size, | |
sizeof(hash_size), | |
&property_size, | |
0 | |
)); | |
auto hash_result = (unsigned char*)malloc(hash_size); | |
auto file = CreateFile( | |
argv[1], | |
GENERIC_READ, | |
FILE_SHARE_READ, | |
nullptr, | |
OPEN_EXISTING, | |
FILE_ATTRIBUTE_NORMAL, | |
nullptr | |
); | |
if (!file) { | |
std::cerr << "couldn't open\n"; | |
ExitProcess(1); | |
} | |
const size_t data_size = 1024 * 1024; | |
auto data = VirtualAlloc(nullptr, data_size, MEM_COMMIT | MEM_RESERVE, PAGE_READWRITE); | |
while (true) { | |
DWORD read_size; | |
if (!ReadFile(file, data, data_size, &read_size, nullptr)) | |
ExitProcess(2); | |
if (read_size == 0) | |
break; | |
check(BCryptHashData(hash_handle, (unsigned char*)data, read_size, 0)); | |
} | |
check(BCryptFinishHash( | |
hash_handle, | |
hash_result, | |
hash_size, | |
0 | |
)); | |
for (int i = 0; i != hash_size; ++i) { | |
printf("%02x", (unsigned int)hash_result[i]); | |
} | |
puts(""); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment