Solved

c++ MD5 Hash WINAPI

Posted on 2015-01-01
3
346 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Firefox seemed to slow down recently and it occurred to me that I had many open tabs — ultimately, I would find out that three-quarters of them were Experts Exchange tabs! :) So I decided to go on a hunt for a tool that would capture the names an…
With Windows evolving further, the built-in protective mechanisms get better and better. Still, Microsoft is not very good at introducing those to the technical community. This article is about a new bitlocker functionality that could revolutionize …
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.
The Task Scheduler is a powerful tool that is built into Windows. It allows you to schedule tasks (actions) on a recurring basis, such as hourly, daily, weekly, monthly, at log on, at startup, on idle, etc. This video Micro Tutorial is a brief intro…

864 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

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now