Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

c++ MD5 Hash WINAPI

Posted on 2015-01-01
3
Medium Priority
?
473 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
3 Comments
 
LVL 86

Accepted Solution

by:
jkr earned 2000 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

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

The Samsung SSD 840 EVO and 840 EVO mSATA have a well-known problem with a drop in read performance. I first learned about this in an interesting thread here at Experts Exchange: http://www.experts-exchange.com/Hardware/Storage/Hard_Drives/Q_2852…
If you use NetMotion Mobility on your PC and plan to upgrade to Windows 10, it may not work unless you take these steps.
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…
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

715 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