Solved

c++ MD5 Hash WINAPI

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

Get up to 2TB FREE CLOUD per backup license!

An exclusive Black Friday offer just for Expert Exchange audience! Buy any of our top-rated backup solutions & get up to 2TB free cloud per system! Perform local & cloud backup in the same step, and restore instantly—anytime, anywhere. Grab this deal now before it disappears!

Join & Write a Comment

Sometimes drives fill up and we don't know why.  If you don't understand the best way to use the tools available, you may end up being stumped as to why your drive says it's not full when you have no space left!  Here's how you can find out...
When you start your Windows 10 PC and got an "Operating system not found" error or just saw  "Auto repair for startup". After a while, you have entered a loop for Auto repair which does not fix anything and you will be in a  panic as all your work w…
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.

706 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