MD5ing a file

This is a discussion on MD5ing a file within the Windows Programming forums, part of the Platform Specific Boards category; I'm doing a file hasher, I'm starting with MD5, I'm following this example but I didn't want to use window's ...

  1. #1
    the magic penguim
    Join Date
    Jul 2005
    Posts
    91

    MD5ing a file

    I'm doing a file hasher, I'm starting with MD5, I'm following this example but I didn't want to use window's function, I'm using normal md5 functions.

    Code:
        LPSTR lpOut[40];
        HANDLE hFile = NULL;
        BOOL bResult = FALSE;
        BYTE rgbFile[BUFSIZE];
        DWORD cbRead = 0;
        unsigned char* md5;
    
        hFile = CreateFile(path, GENERIC_READ, FILE_SHARE_READ, NULL, 
                           OPEN_EXISTING, FILE_FLAG_SEQUENTIAL_SCAN, NULL);
    
        if (hFile == INVALID_HANDLE_VALUE)
        {
            wsprintf(lpOut, "Error opening file! Error: %d\0", GetLastError());
            MessageBox(hwnd, lpOut, "Error", MB_OK | MB_ICONERROR);
            break;
        }
                             
        MD5_CTX context;
        MD5Init(&context);
    
        while (bResult = ReadFile(hFile, rgbFile, BUFSIZE, &cbRead, NULL))
        {
            if (cbRead == 0)
            {
                break;
            }
    
            MD5Update(&context, rgbFile, BUFSIZE);
        }
    
        CloseHandle(hFile);
        MD5Final(md5, &context);
        SetDlgItemText(hwnd, HSH_MD5, md5);
    It compiles fine but I get an error while running it. Aditionally I would like to know if is there a function that would take the number given by GetLastError() and convert it into the description of the error.

  2. #2
    Yes, my avatar is stolen anonytmouse's Avatar
    Join Date
    Dec 2002
    Posts
    2,544
    Code:
        unsigned char* md5;
    
        ....
    
        MD5Final(md5, &context);
    Read this post. Assumably MD5Final is expecting an array of 16 unsigned chars where it will place the result. It is almost certainly not expecting a random pointer.
    Aditionally I would like to know if is there a function that would take the number given by GetLastError() and convert it into the description of the error.
    Yes, you can use the FormatMessage function:
    Code:
    TCHAR szErrorMsg[1024];
    
    FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS, NULL, 
                  GetLastError(), MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
                  szErrorMsg, sizeof(szErrorMsg) / sizeof(szErrorMsg[0]), NULL);

  3. #3
    the magic penguim
    Join Date
    Jul 2005
    Posts
    91
    Thanks it's working now. But I'm not getting the right hashes, actually it depends on the file, some files works fine, but some get different hashes each time I hash it...

    I must say that I have no clue on hashing files so could someone please tell me where am I doing wrong?
    Last edited by lala123; 07-28-2005 at 07:23 AM.

  4. #4
    Yes, my avatar is stolen anonytmouse's Avatar
    Join Date
    Dec 2002
    Posts
    2,544
    Code:
    MD5Update(&context, rgbFile, BUFSIZE);
    This call is hashing BUFSIZE bytes on each call to MD5Update, even if a lesser number of bytes were read. You should use cbRead, which contains the number of bytes read, instead:
    Code:
    MD5Update(&context, rgbFile, cbRead);

  5. #5
    the magic penguim
    Join Date
    Jul 2005
    Posts
    91
    Thanks a lot, again.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. sequential file program
    By needhelpbad in forum C Programming
    Replies: 80
    Last Post: 06-08-2008, 01:04 PM
  2. Unknown Memory Leak in Init() Function
    By CodeHacker in forum Windows Programming
    Replies: 3
    Last Post: 07-09-2004, 09:54 AM
  3. archive format
    By Nor in forum A Brief History of Cprogramming.com
    Replies: 0
    Last Post: 08-05-2003, 07:01 PM
  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