Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

c++ MD5 Hash WINAPI

Posted on 2015-01-01
3
Medium Priority
?
515 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 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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Security measures require Windows be logged in using Standard User login (not Administrator).  Yet, sometimes an application has to be run “As Administrator” from a Standard User login.  This paper describes how to create a shortcut icon to launch a…
Article by: evilrix
Looking for a way to avoid searching through large data sets for data that doesn't exist? A Bloom Filter might be what you need. This data structure is a probabilistic filter that allows you to avoid unnecessary searches when you know the data defin…
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
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.
Suggested Courses

963 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