-
Notifications
You must be signed in to change notification settings - Fork 2
/
Source.cpp
238 lines (232 loc) · 7.13 KB
/
Source.cpp
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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
#pragma comment(linker,"\"/manifestdependency:type='win32' name='Microsoft.Windows.Common-Controls' version='6.0.0.0' processorArchitecture='*' publicKeyToken='6595b64144ccf1df' language='*'\"")
#pragma comment(lib, "shlwapi")
#include <windows.h>
#include <shlwapi.h>
#define ID_DELETE 100
#define ID_SELECTALL 101
#define ID_COPYTOCLIPBOARD 102
BOOL CalcFileHash(LPCTSTR lpszFilePath, ALG_ID Algid, LPTSTR lpszHashValue)
{
if (PathFileExists(lpszFilePath) == FALSE) return FALSE;
if (lpszHashValue == NULL) return FALSE;
if ((Algid & ALG_CLASS_HASH) == 0) return FALSE;
HANDLE hFile = CreateFile(lpszFilePath, GENERIC_READ, 0, 0, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, 0);
if (hFile == INVALID_HANDLE_VALUE)
{
return FALSE;
}
HCRYPTPROV hProv = 0;
if (!CryptAcquireContext(&hProv, 0, 0, PROV_RSA_AES, CRYPT_VERIFYCONTEXT | CRYPT_MACHINE_KEYSET))
{
CloseHandle(hFile);
return FALSE;
}
HCRYPTHASH hHash = 0;
if (!CryptCreateHash(hProv, Algid, 0, 0, &hHash))
{
CloseHandle(hFile);
CryptReleaseContext(hProv, 0);
return FALSE;
}
BOOL bRet = FALSE;
for (;;)
{
BYTE Buffer[64 * 1024];
DWORD wReadSize;
if (ReadFile(hFile, Buffer, sizeof(Buffer), &wReadSize, 0) == FALSE)
break;
if (!wReadSize)
break;
bRet = CryptHashData(hHash, Buffer, wReadSize, 0) ? TRUE : FALSE;
if (!bRet)
break;
}
CloseHandle(hFile);
if (!bRet)
{
CryptDestroyHash(hHash);
CryptReleaseContext(hProv, 0);
return FALSE;
}
DWORD dwHashLen = 0;
if (!CryptGetHashParam(hHash, HP_HASHVAL, 0, &dwHashLen, 0) || !dwHashLen)
{
CryptDestroyHash(hHash);
CryptReleaseContext(hProv, 0);
return FALSE;
}
LPBYTE lpHash = (LPBYTE)GlobalAlloc(GMEM_FIXED, dwHashLen);
if (!lpHash)
{
CryptDestroyHash(hHash);
CryptReleaseContext(hProv, 0);
return FALSE;
}
if (!CryptGetHashParam(hHash, HP_HASHVAL, lpHash, &dwHashLen, 0))
{
GlobalFree(lpHash);
CryptDestroyHash(hHash);
CryptReleaseContext(hProv, 0);
return FALSE;
}
CryptDestroyHash(hHash);
CryptReleaseContext(hProv, 0);
lpszHashValue[0] = 0;
for (DWORD i = 0; i < dwHashLen; ++i)
{
TCHAR tmp[3] = { 0 };
wsprintf(tmp, TEXT("%02X"), lpHash[i]);
lstrcat(lpszHashValue, tmp);
}
GlobalFree(lpHash);
return TRUE;
}
LRESULT CALLBACK WndProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
static HWND hCombo;
static HWND hList;
switch (msg)
{
case WM_CREATE:
hCombo = CreateWindow(TEXT("COMBOBOX"), 0, WS_VISIBLE | WS_CHILD | CBS_DROPDOWNLIST, 10, 10, 256, 1024, hWnd, 0, ((LPCREATESTRUCT)lParam)->hInstance, 0);
SendMessage(hCombo, CB_SETITEMDATA, SendMessage(hCombo, CB_ADDSTRING, 0, (LPARAM)TEXT("SHA-1")), (LPARAM)CALG_SHA1);
SendMessage(hCombo, CB_SETITEMDATA, SendMessage(hCombo, CB_ADDSTRING, 0, (LPARAM)TEXT("SHA-256")), (LPARAM)CALG_SHA_256);
SendMessage(hCombo, CB_SETITEMDATA, SendMessage(hCombo, CB_ADDSTRING, 0, (LPARAM)TEXT("SHA-384")), (LPARAM)CALG_SHA_384);
SendMessage(hCombo, CB_SETITEMDATA, SendMessage(hCombo, CB_ADDSTRING, 0, (LPARAM)TEXT("SHA-512")), (LPARAM)CALG_SHA_512);
SendMessage(hCombo, CB_SETITEMDATA, SendMessage(hCombo, CB_ADDSTRING, 0, (LPARAM)TEXT("MD5")), (LPARAM)CALG_MD5);
SendMessage(hCombo, CB_SETCURSEL, 0, 0);
hList = CreateWindowEx(WS_EX_CLIENTEDGE, TEXT("LISTBOX"), 0, WS_VISIBLE | WS_CHILD | WS_VSCROLL | LBS_NOINTEGRALHEIGHT | LBS_EXTENDEDSEL, 0, 0, 0, 0, hWnd, 0, ((LPCREATESTRUCT)lParam)->hInstance, 0);
DragAcceptFiles(hWnd, TRUE);
break;
case WM_SIZE:
MoveWindow(hList, 10, 50, LOWORD(lParam) - 20, HIWORD(lParam) - 60, TRUE);
break;
case WM_DROPFILES:
{
SendMessage(hList, LB_SETSEL, 0, -1);
const ALG_ID Algid = (ALG_ID)SendMessage(hCombo, CB_GETITEMDATA, SendMessage(hCombo, CB_GETCURSEL, 0, 0), 0);
if (Algid)
{
const UINT nFileCount = DragQueryFile((HDROP)wParam, 0xFFFFFFFF, NULL, 0);
for (UINT i = 0; i < nFileCount; ++i)
{
TCHAR szFilePath[MAX_PATH];
DragQueryFile((HDROP)wParam, i, szFilePath, _countof(szFilePath));
TCHAR szHash[256];
if (CalcFileHash(szFilePath, Algid, szHash))
{
TCHAR szText[1024];
wsprintf(szText, TEXT("%s(%s)"), szHash, szFilePath);
SendMessage(hList, LB_SETSEL, 1, SendMessage(hList, LB_ADDSTRING, 0, (LPARAM)szText));
}
}
}
DragFinish((HDROP)wParam);
}
break;
case WM_COMMAND:
switch (LOWORD(wParam))
{
case ID_COPYTOCLIPBOARD:
{
const int nSelItems = (int)SendMessage(hList, LB_GETSELCOUNT, 0, 0);
if (nSelItems > 0)
{
int* pBuffer = (int*)GlobalAlloc(0, sizeof(int) * nSelItems);
SendMessage(hList, LB_GETSELITEMS, nSelItems, (LPARAM)pBuffer);
INT nLen = 0;
for (int i = 0; i < nSelItems; ++i)
{
nLen += (INT)SendMessage(hList, LB_GETTEXTLEN, pBuffer[i], 0);
nLen += 2; // 改行文字 \r\n 分
}
HGLOBAL hMem = GlobalAlloc(GMEM_DDESHARE | GMEM_MOVEABLE, sizeof(TCHAR)*(nLen + 1)); // NULL 終端文字用の +1
LPTSTR lpszBuflpszBuf = (LPTSTR)GlobalLock(hMem);
lpszBuflpszBuf[0] = 0;
for (int i = 0; i < nSelItems; ++i)
{
SendMessage(hList, LB_GETTEXT, pBuffer[i], (LPARAM)(lpszBuflpszBuf + lstrlen(lpszBuflpszBuf)));
lstrcat(lpszBuflpszBuf, TEXT("\r\n"));
}
GlobalFree(pBuffer);
GlobalUnlock(hMem);
OpenClipboard(NULL);
EmptyClipboard();
SetClipboardData(CF_UNICODETEXT, hMem);
CloseClipboard();
}
}
break;
case ID_SELECTALL:
SendMessage(hList, LB_SETSEL, 1, -1);
break;
case ID_DELETE:
{
const INT nSelItems = (INT)SendMessage(hList, LB_GETSELCOUNT, 0, 0);
if (nSelItems > 0)
{
LPINT pBuffer = (LPINT)GlobalAlloc(0, sizeof(INT) * nSelItems);
SendMessage(hList, LB_GETSELITEMS, nSelItems, (LPARAM)pBuffer);
for (INT i = nSelItems - 1; i >= 0; --i)
{
SendMessage(hList, LB_DELETESTRING, pBuffer[i], 0);
}
GlobalFree(pBuffer);
}
}
break;
}
break;
case WM_DESTROY:
PostQuitMessage(0);
break;
default:
return DefWindowProc(hWnd, msg, wParam, lParam);
}
return 0;
}
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPreInst, LPSTR pCmdLine, int nCmdShow)
{
TCHAR szClassName[] = TEXT("FileHash");
MSG msg;
WNDCLASS wndclass = {
CS_HREDRAW | CS_VREDRAW,
WndProc,
0,
0,
hInstance,
0,
LoadCursor(0,IDC_ARROW),
(HBRUSH)(COLOR_WINDOW + 1),
0,
szClassName
};
RegisterClass(&wndclass);
HWND hWnd = CreateWindow(
szClassName,
TEXT("ドロップされたファイルのハッシュ値を求める"),
WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT,
0,
CW_USEDEFAULT,
0,
0,
0,
hInstance,
0
);
ShowWindow(hWnd, SW_SHOWDEFAULT);
UpdateWindow(hWnd);
ACCEL Accel[] = { { FVIRTKEY, VK_DELETE, ID_DELETE }, { FVIRTKEY | FCONTROL, 'A', ID_SELECTALL }, { FVIRTKEY | FCONTROL, 'C', ID_COPYTOCLIPBOARD } };
HACCEL hAccel = CreateAcceleratorTable(Accel, sizeof(Accel) / sizeof(ACCEL));
while (GetMessage(&msg, 0, 0, 0))
{
if (!TranslateAccelerator(hWnd, hAccel, &msg))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
}
DestroyAcceleratorTable(hAccel);
return (int)msg.wParam;
}