Click here to Skip to main content
15,891,136 members
Articles / Programming Languages / C

MD5 File Creation and Verification

Rate me:
Please Sign up or sign in to vote.
3.35/5 (10 votes)
3 Jan 2009CPOL 261.2K   4K   28  
A tool for creating/verifying MD5 files
#include "Test.h"


#include <windows.h>
#include <wincrypt.h>
#include <string.h>
#include <stdio.h>
HCRYPTPROV hCryptProv;

typedef struct {
    unsigned char digest[16];
    unsigned long hHash;
} MD5Context;


BOOL CryptStartup()
{
    if(CryptAcquireContext(&hCryptProv, NULL, MS_ENHANCED_PROV, PROV_RSA_FULL, CRYPT_VERIFYCONTEXT) == 0){
        if(GetLastError() == NTE_EXISTS){
            if(CryptAcquireContext(&hCryptProv, NULL, MS_ENHANCED_PROV, PROV_RSA_FULL, 0) == 0)
                return FALSE;
        }else return FALSE;
    }
    return TRUE;
}

void CryptCleanup()
{
    if(hCryptProv) CryptReleaseContext(hCryptProv, 0);
    hCryptProv = 0;
}

void MD5Init(MD5Context *ctx)
{
    CryptCreateHash(hCryptProv, CALG_MD5, 0, 0, &ctx->hHash);
}


void MD5Update(MD5Context *ctx, unsigned char const *buf, unsigned int len)
{
    CryptHashData(ctx->hHash, buf, len, 0);
}

void MD5Final(MD5Context *ctx)
{
    DWORD dwCount = 16;
    CryptGetHashParam(ctx->hHash, HP_HASHVAL, ctx->digest, &dwCount, 0);
    if(ctx->hHash) CryptDestroyHash(ctx->hHash);
    ctx->hHash = 0;
}
void Start()
{
	MessageBox(L"Starting"):
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

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


Written By
Software Developer
India India
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions