Creating an MD5 Hash from File Content

本文展示了一个使用C++和Windows CryptoAPI计算文件MD5哈希的示例代码。通过读取指定文件的内容并计算其MD5哈希值,该程序能够验证文件的完整性和一致性。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

转自:https://docs.microsoft.com/zh-cn/windows/desktop/SecCrypto/example-c-program--creating-an-md-5-hash-from-file-content

Example C Program: Creating an MD5 Hash from File Content
2018/05/31
2 分钟阅读时长
The following example demonstrates using CryptoAPI to compute the MD5 hash of the contents of a file. This example performs the computation on the contents of a file specified at run time.

C++

复制
#include <stdio.h>
#include <windows.h>
#include <Wincrypt.h>

#define BUFSIZE 1024
#define MD5LEN 16

DWORD main()
{
DWORD dwStatus = 0;
BOOL bResult = FALSE;
HCRYPTPROV hProv = 0;
HCRYPTHASH hHash = 0;
HANDLE hFile = NULL;
BYTE rgbFile[BUFSIZE];
DWORD cbRead = 0;
BYTE rgbHash[MD5LEN];
DWORD cbHash = 0;
CHAR rgbDigits[] = “0123456789abcdef”;
LPCWSTR filename=L"filename.txt";
// Logic to check usage goes here.

hFile = CreateFile(filename,
    GENERIC_READ,
    FILE_SHARE_READ,
    NULL,
    OPEN_EXISTING,
    FILE_FLAG_SEQUENTIAL_SCAN,
    NULL);

if (INVALID_HANDLE_VALUE == hFile)
{
    dwStatus = GetLastError();
    printf("Error opening file %s\nError: %d\n", filename, 
        dwStatus); 
    return dwStatus;
}

// Get handle to the crypto provider
if (!CryptAcquireContext(&hProv,
    NULL,
    NULL,
    PROV_RSA_FULL,
    CRYPT_VERIFYCONTEXT))
{
    dwStatus = GetLastError();
    printf("CryptAcquireContext failed: %d\n", dwStatus); 
    CloseHandle(hFile);
    return dwStatus;
}

if (!CryptCreateHash(hProv, CALG_MD5, 0, 0, &hHash))
{
    dwStatus = GetLastError();
    printf("CryptAcquireContext failed: %d\n", dwStatus); 
    CloseHandle(hFile);
    CryptReleaseContext(hProv, 0);
    return dwStatus;
}

while (bResult = ReadFile(hFile, rgbFile, BUFSIZE, 
    &cbRead, NULL))
{
    if (0 == cbRead)
    {
        break;
    }

    if (!CryptHashData(hHash, rgbFile, cbRead, 0))
    {
        dwStatus = GetLastError();
        printf("CryptHashData failed: %d\n", dwStatus); 
        CryptReleaseContext(hProv, 0);
        CryptDestroyHash(hHash);
        CloseHandle(hFile);
        return dwStatus;
    }
}

if (!bResult)
{
    dwStatus = GetLastError();
    printf("ReadFile failed: %d\n", dwStatus); 
    CryptReleaseContext(hProv, 0);
    CryptDestroyHash(hHash);
    CloseHandle(hFile);
    return dwStatus;
}

cbHash = MD5LEN;
if (CryptGetHashParam(hHash, HP_HASHVAL, rgbHash, &cbHash, 0))
{
    printf("MD5 hash of file %s is: ", filename);
    for (DWORD i = 0; i < cbHash; i++)
    {
        printf("%c%c", rgbDigits[rgbHash[i] >> 4],
            rgbDigits[rgbHash[i] & 0xf]);
    }
    printf("\n");
}
else
{
    dwStatus = GetLastError();
    printf("CryptGetHashParam failed: %d\n", dwStatus); 
}

CryptDestroyHash(hHash);
CryptReleaseContext(hProv, 0);
CloseHandle(hFile);

return dwStatus; 

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值