Click here to Skip to main content
Click here to Skip to main content

MD5 File Creation and Verification

By , 3 Jan 2009
 
MD5

Introduction

In cryptography, MD5 (Message-Digest algorithm 5) is a widely used, partially insecure cryptographic hash function with a 128-bit hash value. As an Internet standard (RFC 1321), MD5 has been employed in a wide variety of security applications, and is also commonly used to check the integrity of files. An MD5 hash is typically expressed as a 32 digit hexadecimal number.

For more information regarding MD5, click here.

Background

This tool is for calculating/verifying MD5 value of an EXE or multiple EXEs in a folder and its sub folders.

Using the Code

The basic function in this tool is as follows. This function will accept the EXE "FileName" and will return the MD5 value in "MD5". Anyone can use this function in his/her code to get the MD5 value. Just copy and paste this function in your code. You should include the header <wincrypt.h>.

int CalculateMD5(CString FileName, CString &MD5)
{
    const size_t StringSize = FileName.GetLength() + 1;
    size_t CharactersConverted = 0;

    char *file = new char[StringSize];
    //char file[1024];
    wcstombs_s(&CharactersConverted, file, 
               FileName.GetLength()+1, FileName, _TRUNCATE);

    int i, j;
    FILE *fInput;
    MD5Context md5Hash;
    unsigned char bBuffer[4096];
    unsigned char b;
    char c;
    
    if(!CryptStartup())
    {
        MessageBoxW(0, L"Could not start crypto library", 
                    L"MD5", MB_ICONERROR);
        return 0;
    }
    
    fInput = fopen(file, "rb");
    if(!fInput)
	{
       MessageBoxW(0, L"Failed to open - Invalid File", 
                   L"MD5", MB_ICONERROR);
        CryptCleanup();
        return 0;
    }
    
    memset(&md5Hash, 0, sizeof(MD5Context));
    MD5Init(&md5Hash);
    while(!feof(fInput)){
        unsigned int nCount = fread(bBuffer, sizeof(unsigned char), 
                                    4096, fInput);
        MD5Update(&md5Hash, bBuffer, nCount);
    }
    MD5Final(&md5Hash);
    
    fclose(fInput);
    //printf("\nChecksum of '%s' is: ", argv[1]);
    char *Value = new char[1024];int k = 0;
    for(i = 0; i < 16; i++)
	{
        b = md5Hash.digest[i];
        for(j = 4; j >= 0; j -= 4)
		{
            c = ((char)(b >> j) & 0x0F);
            if(c < 10) c += '0';
            else c = ('a' + (c - 10));
            //printf("%c", c);
			Value[k] = c;
			k++;
        }
    }
    Value[k] = '\0';
    CryptCleanup();
	
    //CString cString;
    MD5 = CString(Value);
    //MessageBox(cString);
	
    delete file;
    delete Value;
    return 1;
}

History

  • 3rd January, 2009: Initial post

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

About the Author

Shaheer Abdulrahiman
Software Developer
India India
No Biography provided

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board.
Search this forum  
    Spacing  Noise  Layout  Per page   
GeneralMD5 Source in C++membernone28-Feb-09 4:54 
A lot of people use PHP for md5(), and when they use a C++ implementation it doesn't always return the same hash.
 
This implementation's hashes match that of php http://www.zedwood.com/article/121/cpp-md5-function.
GeneralMD5 is obsoletememberwim ton5-Jan-09 23:09 
AFAIK is MD5 not recommended for new projects, because of some cryptographic weaknesses.
 
Till the current hash competition is decided, SHA-256 might be a better choice. SHA-1 turned out to be less good as expected as well.
 
Regards, Wim
GeneralRe: MD5 is obsoletememberTobiasP6-Jan-09 8:44 
True, and the article also states that MD5 is "partially insecure". I've read that the MD5 can still be used to check for accidental tampering of a file (e.g., an additional check to verify that a downloaded file is correctly downloaded, or perhaps to find identical copies of files on your disks), but is too weak to offer security against intentional tampering (it is possible to modify a file in such a way that the original checksum does not change, but it is very unlikely to happen accidentally).
 
This is still a valid tool though, given how common the usage of MD5 checksums still are.
GeneralMy vote of 2memberzengkun1005-Jan-09 16:58 
why don't you use sprintf or someting else
General牛BmemberMember 45990724-Jan-09 4:16 
牛B Laugh | :laugh:
GeneralRe: 牛Bmemberbaijun19824-Jan-09 19:01 
看见老乡了。哈哈
GeneralRe: 牛BmemberEnjon CHEN5-Apr-10 23:48 
OOOOOO
GeneralRe: 牛Bmemberhaohui27-Oct-10 5:24 
我们都很牛B Shucks | :->
buct

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Permalink | Advertise | Privacy | Mobile
Web03 | 2.6.130617.1 | Last Updated 3 Jan 2009
Article Copyright 2009 by Shaheer Abdulrahiman
Everything else Copyright © CodeProject, 1999-2013
Terms of Use
Layout: fixed | fluid