md5 hash a file

This is a discussion on md5 hash a file within the Windows Programming forums, part of the Platform Specific Boards category; Hi All, Is there a win32 library that I can use to generate an md5 of a file? I only ...

  1. #1
    Registered User
    Join Date
    Aug 2002
    Posts
    351

    md5 hash a file

    Hi All,

    Is there a win32 library that I can use to generate an md5 of a file?

    I only get .NET results when searching msdn.

    Thanks for any help,

    rotis23

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,452
    Try searching for md5.c

    Here
    http://www.l2tpd.org/cxref/md5.c.src.html

  3. #3
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,246
    I've always used the md5 code at http://sourceforge.net/project/showf...group_id=42360

    It is just two files (a .c and a .h), and very simple to use.

  4. #4
    Registered User
    Join Date
    Aug 2002
    Posts
    351
    Thanks guys - found win32 specifc info here:

    http://msdn.microsoft.com/library/de...le_content.asp

  5. #5
    Yes, my avatar is stolen anonytmouse's Avatar
    Join Date
    Dec 2002
    Posts
    2,544
    I wanted to post my code when I saw the thread title. I seem to have been beaten to the punch. Oh well, I'll post it anyway.
    Code:
    #include <windows.h>
    #include <wincrypt.h>
    #pragma comment(lib, "advapi32.lib")
    
    /*
     * Creates a hexadecimal hash string from input data.
     * ALG_ID can be one of CALG_MD2, CALG_MD4, CALG_MD5 or CALG_SHA1.
     * SHA1 algorithm is strongest, while MD2 and MD4 should be avoided.
     * MD algorithms require a 33 character output buffer. SHA1 requires 41 characters.
     * Returns TRUE on success, FALSE on failure.
     */
    BOOL HashData(LPTSTR szOut, DWORD cchOut, LPCBYTE lpIn, DWORD cbIn, ALG_ID hash_algorithm)
    {
    	HCRYPTPROV hProv      = 0;
    	HCRYPTHASH hHash      = 0;
    	BYTE       raw[64];
    	DWORD      raw_len    = sizeof(raw);
    	BOOL       bResult    = FALSE;
    	UINT       i;
    
    	if(CryptAcquireContext(&hProv, NULL, NULL, PROV_RSA_FULL, CRYPT_VERIFYCONTEXT | CRYPT_SILENT) &&
    	   CryptCreateHash(hProv, hash_algorithm, 0, 0, &hHash) &&
    	   CryptHashData(hHash, lpIn, cbIn, 0) &&
    	   CryptGetHashParam(hHash, HP_HASHVAL, raw, &raw_len, 0) &&
    	   (raw_len * 2) + 1 <= cchOut)
    	{
    		for (i = 0;i < raw_len;i++)
    		{
    			wsprintf(&szOut[i * 2], TEXT("%02.2x"), raw[i]);
    		}
    
    		bResult = TRUE;
    	}
    
    	if (hHash) CryptDestroyHash(hHash);
    	if (hProv) CryptReleaseContext(hProv, 0);
    
    	return bResult;
    }
    
    
    #if 1
    #include <stdio.h>
    
    int main(void)
    {
    	TCHAR szHash[64];
    	char * test = "Hello, this is a test";
    
    	printf("Input: %s\n", test);
    
    	if (HashData(szHash, 33, test, strlen(test), CALG_MD5))
    		printf("MD5: %s\n", szHash);
    
    	if (HashData(szHash, 33, test, strlen(test), CALG_MD2))
    		printf("MD2: %s\n", szHash);
    
    	if (HashData(szHash, 33, test, strlen(test), CALG_MD4))
    		printf("MD4: %s\n", szHash);
    
    	if (HashData(szHash, 41, test, strlen(test), CALG_SHA1))
    		printf("SHA1: %s\n", szHash);
    
    	getchar();
    	return 0;
    }
    #endif
    
    
    /// ===================================================================
    /// Hashing in chunks.
    
    HCRYPTHASH HashInit(HCRYPTPROV hCryptProvider, ALG_ID hash_algorithm)
    {
    	HCRYPTHASH hHash = 0;
    	CryptCreateHash(hCryptProvider, hash_algorithm, 0, 0, &hHash);
    	return hHash;
    }
    
    BOOL HashUpdate(HCRYPTHASH hHash, LPCBYTE lpData, DWORD cbData)
    {
    	return CryptHashData(hHash, lpData, cbData, 0);
    }
    
    BOOL HashFinal(HCRYPTHASH hHash, LPTSTR szOut, DWORD cchOut)
    {
    	BYTE       raw[64];
    	DWORD      raw_len    = sizeof(raw);
    	BOOL       bResult    = FALSE;
    	UINT       i;
    
    	if (CryptGetHashParam(hHash, HP_HASHVAL, raw, &raw_len, 0) &&
    	    (raw_len * 2) + 1 <= cchOut)
    	{
    		for (i = 0;i < raw_len;i++)
    		{
    			wsprintf(&szOut[i * 2], TEXT("%02.2x"), raw[i]);
    		}
    
    		bResult = TRUE;
    	}
    
    	if (hHash) CryptDestroyHash(hHash);
    
    	return bResult;
    }
    
    
    #if 0
    #include <stdio.h>
    
    int main(void)
    {
    	HCRYPTPROV hProv      = 0;
    	HCRYPTHASH hHash      = 0;
    	TCHAR szHash[64];
    
    	CryptAcquireContext(&hProv, NULL, NULL, PROV_RSA_FULL, CRYPT_VERIFYCONTEXT | CRYPT_SILENT);
    
    	hHash = HashInit(hProv, CALG_MD5);
    	HashUpdate(hHash, "abcd", strlen("abcd"));
    	HashUpdate(hHash, "efgh", strlen("efgh"));
    	HashFinal(hHash, szHash, 33);
    	printf("MD5: %s\n", szHash);
    
    	hHash = HashInit(hProv, CALG_SHA1);
    	HashUpdate(hHash, "abcd", strlen("abcd"));
    	HashUpdate(hHash, "efgh", strlen("efgh"));
    	HashFinal(hHash, szHash, 41);
    	printf("SHA1: %s\n", szHash);
    
    	CryptReleaseContext(hProv, 0);
    	getchar();
    	return 0;
    }
    #endif
    I have to comment on this, from the Microsoft sample code:
    Code:
        CHAR rgbDigits[] = "0123456789abcdef";
    
        printf("%c%c", rgbDigits[rgbHash[i] >> 4],
                rgbDigits[rgbHash[i] & 0xf]);
    That is one creative way of outputting hexadecimal!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. A development process
    By Noir in forum C Programming
    Replies: 37
    Last Post: 07-10-2011, 10:39 PM
  2. gcc link external library
    By spank in forum C Programming
    Replies: 6
    Last Post: 08-08-2007, 03:44 PM
  3. Unknown Memory Leak in Init() Function
    By CodeHacker in forum Windows Programming
    Replies: 3
    Last Post: 07-09-2004, 09:54 AM
  4. Making a LIB file from a DEF file for a DLL
    By JMPACS in forum C++ Programming
    Replies: 0
    Last Post: 08-02-2003, 08:19 PM
  5. Need a suggestion on a school project..
    By Screwz Luse in forum C Programming
    Replies: 5
    Last Post: 11-27-2001, 01:58 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21