Skip to content

Commit

Permalink
Add auto clipboard copying
Browse files Browse the repository at this point in the history
Refactor error handling
  • Loading branch information
CherryPill committed Jun 3, 2023
1 parent e1bec10 commit 959a5fc
Show file tree
Hide file tree
Showing 13 changed files with 289 additions and 115 deletions.
7 changes: 6 additions & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,12 @@ add_executable(md5_hash_tool WIN32
src/proc/mainwindowProc.c
src/util/utility.c
src/ui/view.c
src/res/resource1.h)
src/res/resource1.h
src/handler/error_handler.c
src/handler/error_handler.h
src/consts/text_consts.c
src/consts/text_consts.h
)


target_link_libraries(md5_hash_tool comctl32)
Expand Down
5 changes: 5 additions & 0 deletions src/consts/text_consts.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
//
// Created by Roman on 6/3/2023.
//

#include "text_consts.h"
27 changes: 27 additions & 0 deletions src/consts/text_consts.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
//
// Created by Roman on 6/3/2023.
//

#ifndef MD5_HASH_TOOL_TEXT_CONSTS_H
#define MD5_HASH_TOOL_TEXT_CONSTS_H

#include <tchar.h>

enum errorMsgResolver {
openFileError = 0,
CryptAcquireCtxError,
CryptCreateHashError,
CryptHashDataError,
fileReadError,
clipboardError
};

static TCHAR* errorMessages[] = {
_T("Error opening file"),
_T("CryptAcquireContext failed"),
_T("CryptCreateHash failed"),
_T("CryptHashData failed"),
_T("Couldn't read file"),
_T("Clipboard error"),
};
#endif //MD5_HASH_TOOL_TEXT_CONSTS_H
213 changes: 114 additions & 99 deletions src/enc/hashing.c
Original file line number Diff line number Diff line change
Expand Up @@ -5,104 +5,119 @@
#include "hashing.h"
#include "../global/globalVars.h"
#include "../ui/view.h"
#include "../handler/error_handler.h"
#include "../consts/text_consts.h"

void cleanUpResources(HCRYPTPROV prov, HCRYPTHASH hash, HANDLE pVoid);

void generateHash(TCHAR *filePath, char *hash) {
char percentageDisplay[5] = {0};
int previousProgress = 0;
int currentProgress = 0;
DWORD dwStatus = 0;
BOOL bResult = FALSE;
HCRYPTPROV hProv = 0;
HCRYPTHASH hHash = 0;
HANDLE hFile = NULL;
BYTE rgbFile[BUFSIZE];
DWORD cbRead = 0;
DWORD totalBytesRead = 0;
BYTE rgbHash[MD5LEN];
DWORD cbHash = 0;
TCHAR rgbDigits[] = _T("0123456789abcdef");

hFile = CreateFile(filePath,
GENERIC_READ,
NULL,
NULL,
OPEN_EXISTING,
FILE_FLAG_SEQUENTIAL_SCAN,
NULL);
LARGE_INTEGER totalBytes;
GetFileSizeEx(hFile, &totalBytes);
if (hFile == INVALID_HANDLE_VALUE) {
dwStatus = GetLastError();
MessageBox(NULL,"Error opening file","Error",MB_OK|MB_ICONERROR);
return 1;
}
// Get handle to the crypto provider
if (!CryptAcquireContext(&hProv,
NULL,
NULL,
PROV_RSA_FULL,
CRYPT_VERIFYCONTEXT)) {
dwStatus = GetLastError();
MessageBox(NULL, "CryptAcquireContext failed", "Error", MB_OK | MB_ICONERROR);
CloseHandle(hFile);
return dwStatus;
}

if (!CryptCreateHash(hProv, CALG_MD5, 0, 0, &hHash)) {
dwStatus = GetLastError();
MessageBox(NULL, "CryptCreateHash failed", "Error", MB_OK | MB_ICONERROR);
CloseHandle(hFile);
CryptReleaseContext(hProv, 0);
return dwStatus;
}
while (bResult = ReadFile(hFile, rgbFile, BUFSIZE,
&cbRead, NULL)) {
if (0 == cbRead) {
break;
}
if (!CryptHashData(hHash, rgbFile, cbRead, 0)) {
dwStatus = GetLastError();
MessageBox(NULL, "CryptHashData failed", "Error", MB_OK | MB_ICONERROR);
CryptReleaseContext(hProv, 0);
CryptDestroyHash(hHash);
CloseHandle(hFile);
return dwStatus;
}
totalBytesRead += cbRead;
currentProgress = ((double)totalBytesRead/totalBytes.QuadPart)*100;
SendMessage(progressBarCntrl, PBM_SETPOS, currentProgress, 0);
if (currentProgress > previousProgress)
{
sprintf(percentageDisplay, "%d%%", currentProgress);
SetWindowText(GetDlgItem(mainWindowHandle, COMPUTED_HASH_LABEL), percentageDisplay);
memset(percentageDisplay, 0, sizeof(percentageDisplay));
previousProgress = currentProgress;
}
}
if (!bResult) {
dwStatus = GetLastError();
MessageBox(NULL, "ReadFile failed", "Error", MB_OK | MB_ICONERROR);
CryptReleaseContext(hProv, 0);
CryptDestroyHash(hHash);
CloseHandle(hFile);
return dwStatus;
}
cbHash = MD5LEN;
if (CryptGetHashParam(hHash, HP_HASHVAL, rgbHash, &cbHash, 0)){
for (DWORD i = 0; i < cbHash; i++) {
char hashHexPart[3] = { 0 };

sprintf(hashHexPart, "%c%c",
rgbDigits[rgbHash[i] >> 4],
rgbDigits[rgbHash[i] & 0xf]);
strcat(hash, hashHexPart);
}
hash[strlen(hash) + 1] = '\0';
}
else {
dwStatus = GetLastError();
}
CryptDestroyHash(hHash);
CryptReleaseContext(hProv, 0);
CloseHandle(hFile);
}

char percentageDisplay[5] = {0};
int previousProgress = 0;
int currentProgress = 0;

DWORD dwStatus = 0;
BOOL bResult = FALSE;
HCRYPTPROV hProv = 0;
HCRYPTHASH hHash = 0;
HANDLE hFile = NULL;
BYTE rgbFile[BUFSIZE];
DWORD cbRead = 0;
DWORD totalBytesRead = 0;
BYTE rgbHash[MD5LEN];
DWORD cbHash = 0;
TCHAR rgbDigits[] = _T("0123456789abcdef");


hFile = CreateFile(filePath,
GENERIC_READ,
NULL,
NULL,
OPEN_EXISTING,
FILE_FLAG_SEQUENTIAL_SCAN,
NULL);
LARGE_INTEGER totalBytes;

GetFileSizeEx(hFile, &totalBytes);

if (hFile == INVALID_HANDLE_VALUE) {
enum errorMsgResolver errorMsgIdx = openFileError;
struct ERROR_INFO errorInfo = {errorMessages[errorMsgIdx], GetLastError()};
handleError(errorInfo);
}
// Get handle to the crypto provider
if (!CryptAcquireContext(&hProv,
NULL,
NULL,
PROV_RSA_FULL,
CRYPT_VERIFYCONTEXT)) {
CloseHandle(hFile);

enum errorMsgResolver errorMsgIdx = CryptAcquireCtxError;
struct ERROR_INFO errorInfo = {errorMessages[errorMsgIdx], GetLastError()};
handleError(errorInfo);
}

if (!CryptCreateHash(hProv, CALG_MD5, 0, 0, &hHash)) {

CloseHandle(hFile);
CryptReleaseContext(hProv, 0);

enum errorMsgResolver errorMsgIdx = CryptCreateHashError;
struct ERROR_INFO errorInfo = {errorMessages[errorMsgIdx], GetLastError()};
handleError(errorInfo);
}
bResult = ReadFile(hFile, rgbFile, BUFSIZE,
&cbRead, NULL);
while (bResult) {
if (0 == cbRead) {
break;
}
if (!CryptHashData(hHash, rgbFile, cbRead, 0)) {
cleanUpResources(hProv, hHash, hFile);
enum errorMsgResolver errorMsgIdx = CryptHashDataError;
struct ERROR_INFO errorInfo = {errorMessages[errorMsgIdx], GetLastError()};
handleError(errorInfo);
}
totalBytesRead += cbRead;
currentProgress = ((double) totalBytesRead / totalBytes.QuadPart) * 100;
SendMessage(progressBarCntrl, PBM_SETPOS, currentProgress, 0);
if (currentProgress > previousProgress) {
sprintf(percentageDisplay, "%d%%", currentProgress);
SetWindowText(GetDlgItem(mainWindowHandle, COMPUTED_HASH_LABEL), percentageDisplay);
memset(percentageDisplay, 0, sizeof(percentageDisplay));
previousProgress = currentProgress;
}
bResult = ReadFile(hFile, rgbFile, BUFSIZE,
&cbRead, NULL);
}


if (!bResult) {
cleanUpResources(hProv, hHash, hFile);
enum errorMsgResolver errorMsgIdx = fileReadError;
struct ERROR_INFO errorInfo = {errorMessages[errorMsgIdx], GetLastError()};
}
cbHash = MD5LEN;
if (CryptGetHashParam(hHash, HP_HASHVAL, rgbHash, &cbHash, 0)) {
for (DWORD i = 0; i < cbHash; i++) {
char hashHexPart[3] = {0};

sprintf(hashHexPart, "%c%c",
rgbDigits[rgbHash[i] >> 4],
rgbDigits[rgbHash[i] & 0xf]);
strcat(hash, hashHexPart);
}
hash[strlen(hash) + 1] = '\0';
} else {
dwStatus = GetLastError();
}

cleanUpResources(hProv, hHash, hFile);
}

void cleanUpResources(HCRYPTPROV prov, HCRYPTHASH hash, HANDLE pVoid) {
CryptDestroyHash(hash);
CryptReleaseContext(prov, 0);
CloseHandle(pVoid);
}
3 changes: 1 addition & 2 deletions src/global/globalVars.c
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,7 @@
HWND mainWindowHandle;
HINSTANCE ghInstance;
HWND progressBarCntrl;
char *programName = "MD5 hash generation tool";
char *programClass = "MD5 hash generation tool";

int mainWindowHeight = 120;
int mainWindowHeight = 130;
int mainWindowWidth = 350;
5 changes: 5 additions & 0 deletions src/global/globalVars.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,9 @@ extern HWND progressBarCntrl;
extern char *programClass;
extern int mainWindowHeight;
extern int mainWindowWidth;

struct ERROR_INFO {
TCHAR *failedAction;
DWORD statusCode;
};
#endif
26 changes: 26 additions & 0 deletions src/handler/error_handler.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
//
// Created by Roman on 6/3/2023.
//

#include <tchar.h>
#include <stdio.h>
#include "error_handler.h"

void handleError(struct ERROR_INFO errorInfo) {

UINT style = MB_OK | MB_ICONERROR;

TCHAR errorBuff[256] = {0};
TCHAR statusCodeBuff[5] = {0};

_stprintf(statusCodeBuff, "%ld", errorInfo.statusCode);
_tcscpy(errorBuff, errorInfo.failedAction);
_tcscat(errorBuff, ", statusCode: ");
_tcscat(errorBuff, statusCodeBuff);

MessageBox(NULL, errorBuff, "Error", style);
}

void initErrorStruct(TCHAR *failedAction, DWORD statusCode) {
struct ERROR_INFO errorInfo = {_T(""),1};
}
20 changes: 20 additions & 0 deletions src/handler/error_handler.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
//
// Created by Roman on 6/3/2023.
//

#ifndef MD5_HASH_TOOL_ERROR_HANDLER_H
#define MD5_HASH_TOOL_ERROR_HANDLER_H

#include <windows.h>
#include "../global/globalVars.h"


void initErrorStruct(TCHAR *failedAction, DWORD statusCode);

/**
* Handle status error and show the appropriate error to user
* @param status
*/
void handleError(struct ERROR_INFO errorInfo);

#endif //MD5_HASH_TOOL_ERROR_HANDLER_H
Loading

0 comments on commit 959a5fc

Please sign in to comment.