ReactOS 0.4.16-dev-927-g467dec4
hashfile.cpp
Go to the documentation of this file.
1/*
2 * PROJECT: ReactOS certutil
3 * LICENSE: MIT (https://spdx.org/licenses/MIT)
4 * PURPOSE: CertUtil hashfile implementation
5 * COPYRIGHT: Copyright 2020 Mark Jansen (mark.jansen@reactos.org)
6 */
7
8#include "precomp.h"
9#include <wincrypt.h>
10#include <stdlib.h>
11
12
14{
16 BOOL bSuccess = FALSE;
17
20
22 {
23 ConPrintf(StdOut, L"CertUtil: -hashfile command failed: %d\n", GetLastError());
24 return bSuccess;
25 }
26
28 {
30
32 {
33 BYTE Buffer[2048];
34 DWORD cbRead;
35
36 while ((bSuccess = ReadFile(hFile, Buffer, sizeof(Buffer), &cbRead, NULL)))
37 {
38 if (cbRead == 0)
39 break;
40
41 if (!CryptHashData(hHash, Buffer, cbRead, 0))
42 {
43 bSuccess = FALSE;
44 ConPrintf(StdOut, L"CertUtil: -hashfile command failed to hash: %d\n", GetLastError());
45 break;
46 }
47 }
48
49 if (bSuccess)
50 {
51 BYTE rgbHash[20];
52 DWORD cbHash, n;
53
54 if (CryptGetHashParam(hHash, HP_HASHVAL, rgbHash, &cbHash, 0))
55 {
56 ConPrintf(StdOut, L"SHA1 hash of %s:\n", Filename);
57 for (n = 0; n < cbHash; ++n)
58 {
59 ConPrintf(StdOut, L"%02x", rgbHash[n]);
60 }
61 ConPuts(StdOut, L"\n");
62 }
63 else
64 {
65 ConPrintf(StdOut, L"CertUtil: -hashfile command failed to extract hash: %d\n", GetLastError());
66 bSuccess = FALSE;
67 }
68 }
69
71 }
72 else
73 {
74 ConPrintf(StdOut, L"CertUtil: -hashfile command no algorithm: %d\n", GetLastError());
75 }
76
78 }
79 else
80 {
81 ConPrintf(StdOut, L"CertUtil: -hashfile command no context: %d\n", GetLastError());
82 }
83
85 return bSuccess;
86}
87
void ConPuts(FILE *fp, LPCWSTR psz)
Definition: fc.c:16
void ConPrintf(FILE *fp, LPCWSTR psz,...)
Definition: fc.c:20
#define StdOut
Definition: fc.c:14
Definition: bufpool.h:45
#define NULL
Definition: types.h:112
#define FALSE
Definition: types.h:117
BOOL WINAPI CryptCreateHash(HCRYPTPROV hProv, ALG_ID Algid, HCRYPTKEY hKey, DWORD dwFlags, HCRYPTHASH *phHash)
Definition: crypt.c:740
BOOL WINAPI CryptGetHashParam(HCRYPTHASH hHash, DWORD dwParam, BYTE *pbData, DWORD *pdwDataLen, DWORD dwFlags)
Definition: crypt.c:1610
BOOL WINAPI CryptDestroyHash(HCRYPTHASH hHash)
Definition: crypt.c:890
BOOL WINAPI CryptReleaseContext(HCRYPTPROV hProv, DWORD dwFlags)
Definition: crypt.c:648
BOOL WINAPI CryptHashData(HCRYPTHASH hHash, const BYTE *pbData, DWORD dwDataLen, DWORD dwFlags)
Definition: crypt.c:1771
BOOL WINAPI CryptAcquireContextW(HCRYPTPROV *phProv, LPCWSTR pszContainer, LPCWSTR pszProvider, DWORD dwProvType, DWORD dwFlags)
Definition: crypt.c:358
#define CloseHandle
Definition: compat.h:739
#define OPEN_EXISTING
Definition: compat.h:775
#define ReadFile(a, b, c, d, e)
Definition: compat.h:742
#define INVALID_HANDLE_VALUE
Definition: compat.h:731
#define GENERIC_READ
Definition: compat.h:135
#define CreateFileW
Definition: compat.h:741
#define FILE_SHARE_READ
Definition: compat.h:136
IN PVCB IN PBCB OUT PDIRENT IN USHORT IN POEM_STRING Filename
Definition: fatprocs.h:940
unsigned int BOOL
Definition: ntddk_ex.h:94
unsigned long DWORD
Definition: ntddk_ex.h:95
GLdouble n
Definition: glext.h:7729
BOOL hash_file(LPCWSTR Filename)
Definition: hashfile.cpp:13
#define FILE_FLAG_SEQUENTIAL_SCAN
Definition: disk.h:43
static HCRYPTPROV hProv
Definition: rsaenh.c:32
_In_ HANDLE hFile
Definition: mswsock.h:90
#define L(x)
Definition: ntvdm.h:50
DWORD WINAPI GetLastError(void)
Definition: except.c:1042
#define PROV_RSA_FULL
Definition: wincrypt.h:2039
#define CRYPT_VERIFYCONTEXT
Definition: wincrypt.h:2069
#define CALG_SHA1
Definition: wincrypt.h:1807
ULONG_PTR HCRYPTPROV
Definition: wincrypt.h:46
ULONG_PTR HCRYPTHASH
Definition: wincrypt.h:50
_In_ HCRYPTHASH hHash
Definition: wincrypt.h:4198
#define HP_HASHVAL
Definition: wincrypt.h:2183
const WCHAR * LPCWSTR
Definition: xmlstorage.h:185
unsigned char BYTE
Definition: xxhash.c:193