Created
December 20, 2013 17:44
-
-
Save neriberto/8058549 to your computer and use it in GitHub Desktop.
Hashes com OpenSSL
This file contains 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
void calc_hash(LPCWSTR lpFileName, const char* algo, char* output) | |
{ | |
HANDLE hFile = NULL; | |
DWORD cbRead = 0; | |
unsigned char *data; | |
size_t data_size; | |
hFile = CreateFile(lpFileName, | |
GENERIC_READ, | |
FILE_SHARE_READ, | |
NULL, | |
OPEN_EXISTING, | |
FILE_FLAG_SEQUENTIAL_SCAN, | |
NULL); | |
if (hFile != INVALID_HANDLE_VALUE) | |
{ | |
data_size = GetFileSize(hFile, NULL); | |
//char *rgbFile = ( char *) malloc (data_size); | |
data = ( unsigned char *) malloc (data_size); | |
if(data != NULL) | |
{ | |
while (ReadFile(hFile, data, data_size, | |
&cbRead, NULL)) | |
{ | |
if (0 == cbRead) | |
{ | |
break; | |
} | |
} | |
//free(data); | |
} | |
CloseHandle(hFile); | |
} | |
OpenSSL_add_all_digests(); | |
char hash_value[EVP_MAX_MD_SIZE * 2 + 1]; | |
const EVP_MD *md = EVP_get_digestbyname(algo); | |
unsigned char md_value[EVP_MAX_MD_SIZE]; | |
unsigned int md_len; | |
EVP_MD_CTX md_ctx; | |
// FIXME: Handle errors - Check return values. | |
EVP_MD_CTX_init(&md_ctx); | |
EVP_DigestInit_ex(&md_ctx, md, NULL); | |
EVP_DigestUpdate(&md_ctx, data, data_size); | |
EVP_DigestFinal_ex(&md_ctx, md_value, &md_len); | |
EVP_MD_CTX_cleanup(&md_ctx); | |
for (unsigned int i=0; i < md_len; i++) | |
sprintf(&output[i * 2], "%02x", md_value[i]); | |
free(data); | |
EVP_cleanup(); // Clean OpenSSL_add_all_digests. | |
} | |
void CfwVipreDlg::OnBnClickedButton9() | |
{ | |
char outMD5[EVP_MAX_MD_SIZE * 2 + 1]; | |
char outSHA1[EVP_MAX_MD_SIZE * 2 + 1]; | |
char outSHA256[EVP_MAX_MD_SIZE * 2 + 1]; | |
char outSHA512[EVP_MAX_MD_SIZE * 2 + 1]; | |
calc_hash(L"C:\\windows\\notepad.exe", "MD5", outMD5); | |
OutputDebugStringA(outMD5); | |
OutputDebugStringA("\n"); | |
calc_hash(L"C:\\windows\\notepad.exe", "SHA1", outSHA1); | |
OutputDebugStringA(outSHA1); | |
OutputDebugStringA("\n"); | |
calc_hash(L"C:\\windows\\notepad.exe", "SHA256", outSHA256); | |
OutputDebugStringA(outSHA256); | |
OutputDebugStringA("\n"); | |
calc_hash(L"C:\\windows\\notepad.exe", "SHA512", outSHA512); | |
OutputDebugStringA(outSHA512); | |
OutputDebugStringA("\n"); | |
SecureZeroMemory(outMD5, MD5_DIGEST_LENGTH); | |
SecureZeroMemory(outSHA1, SHA_DIGEST_LENGTH); | |
SecureZeroMemory(outSHA256, SHA256_DIGEST_LENGTH); | |
SecureZeroMemory(outSHA512, EVP_MAX_MD_SIZE * 2 + 1); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment