Solved

c++ MD5 Hash WINAPI

Posted on 2015-01-01
3
357 Views
Last Modified: 2015-01-02
Hello,

I'm trying to reproduce this example from MSDN: http://msdn.microsoft.com/en-us/library/aa382380%28VS.85%29.aspx

I'm using Windows 8.1 and i''m getting error 87 in CryptGetHashParam api, but i'm sure that i don't have a parameter problem:

bool Md5::HashCrypt(){
	BOOL fileReaded = FALSE;
	BYTE fileBuff[1024];
	DWORD hash = BUFF_MD5;
	BYTE hashBuff[MAX_PATH];	
	//read file content and fill hash object
	while (fileReaded  = ReadFile(fileHandle, fileBuff, BUFF_SIZE, &nBytes, NULL)){
		if (!fileReaded){
			ToClose();
			cout << "ReadFile Fails." << endl;
			return false;
		}
		if (nBytes == 0){
			break;
		}		
		if (!CryptHashData(cryptObject, fileBuff, nBytes, 0)){
			ToClose();
			cout << "CryptHashData Fails." << endl;
			return false;
		}
	}

	CHAR key[] = "0123456789abcdef";
	if (CryptGetHashParam(crypHandle, HP_HASHSIZE, hashBuff, &hash, 0)){
		cout << "-> MD5:\n " << endl;
		for (DWORD i = 0; i < hash; i++){
			printf("%c%c", key[hashBuff[i] >> 4],
				key[hashBuff[i] & 0xf]);
		}

		ToClose();
		return true;
	}
	else{
		DWORD status = GetLastError();
		ToClose();
		cout << "CryptGetHashParam Fails. Error: " << status << endl;
		return false;

	}	
}

Open in new window


I'm out of idea and Google is not helping. I want to get the md5 hash of my program in run-time and display it.

classes.hmain.cppmd5Functions.cpp
0
Comment
Question by:Júlio
  • 2
3 Comments
 
LVL 86

Accepted Solution

by:
jkr earned 500 total points
ID: 40526618
In yoiur call to 'CryptGetHashParam()', you are using 'crypHandle' as the 1st parameter, whereas you are using 'cryptObject' in the previous calls to 'CryptHashData()'. You should use 'cryptObject' here as well (at least that's what the MSDN code does with 'hHash'), i.e.

	if (CryptGetHashParam(cryptObject, HP_HASHSIZE, hashBuff, &hash, 0)){ // <-- here
		cout << "-> MD5:\n " << endl;
		for (DWORD i = 0; i < hash; i++){
			printf("%c%c", key[hashBuff[i] >> 4],
				key[hashBuff[i] & 0xf]);
		}

Open in new window

0
 

Author Comment

by:Júlio
ID: 40527672
You are absolutely right!
but the result doesn't look like a MD5:

result
0
 

Author Closing Comment

by:Júlio
ID: 40527676
he fixed it for me!

But my final MD5 doesn't looks like a MD5, so i'm asking in the same question about it.

He deserve the solution points.
0

Featured Post

The Eight Noble Truths of Backup and Recovery

How can IT departments tackle the challenges of a Big Data world? This white paper provides a roadmap to success and helps companies ensure that all their data is safe and secure, no matter if it resides on-premise with physical or virtual machines or in the cloud.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Ever wondered why Windows 8 and 10 don't seem to accept your GPO-based software deployment while Windows 7 does? Read on.
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
In this Micro Tutorial viewers will learn how to use Boot Corrector from Paragon Rescue Kit Free to identify and fix the boot problems of Windows 7/8/2012R2 etc. As an example is used Windows 2012R2 which lost its active partition flag (often happen…
This Micro Tutorial will teach you how to reformat your flash drive. Sometimes your flash drive may have issues carrying files so this will completely restore it to manufacturing settings. Make sure to backup all files before reformatting. This w…

773 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question