Windows平台下使用C++和Windows API计算文件MD5的方法

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
#include <iostream>
#include <windows.h>
#include <tchar.h>
#include <string>
#include <cassert>
#include <functional>
 
typedef std::basic_string<TCHAR> StringT;
typedef std::string StringA;
 
#define _PP_CAT_IMPL_(a, b ) a ## b
#define PP_CAT(a, b) _PP_CAT_IMPL_( a, b )
 
namespace Details
{
    template <typename TCallable>
    class ScopedExit
    {
    public:
        ScopedExit(TCallable func) :
            Func(func)
        {
        }
 
        ~ScopedExit()
        {
            Func();
        }
 
        ScopedExit(const ScopedExit&) = delete;
        const ScopedExit& operator=(const ScopedExit&) = delete;
 
    private:
        TCallable Func;
    };
}
 
#define SCOPED_EXIT(expression) \
    ::Details::ScopedExit<std::function<void()> > PP_CAT(ScopedExitInstalce_, __COUNTER__)([&]()\
{ \
    expression;\
})
 
 
StringA CalculateFileMD5(const StringT& filepath)
{
#define BUFSIZE 1024
#define MD5LEN  16
 
    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";
    // Logic to check usage goes here.
 
    hFile = CreateFile(filepath.c_str(),
        GENERIC_READ,
        FILE_SHARE_READ,
        NULL,
        OPEN_EXISTING,
        FILE_FLAG_SEQUENTIAL_SCAN,
        NULL);
 
    if (INVALID_HANDLE_VALUE == hFile)
    {
        return "";
    }
 
    SCOPED_EXIT(CloseHandle(hFile));
 
    // Get handle to the crypto provider
    if (!CryptAcquireContext(&hProv,
        NULL,
        NULL,
        PROV_RSA_FULL,
        CRYPT_VERIFYCONTEXT))
    {
        return "";
    }
 
    SCOPED_EXIT(CryptReleaseContext(hProv, 0));
 
    if (!CryptCreateHash(hProv, CALG_MD5, 0, 0, &hHash))
    {
        return "";
    }
 
    SCOPED_EXIT(CryptDestroyHash(hHash));
 
    while (bResult = ReadFile(hFile, rgbFile, BUFSIZE, &cbRead, NULL))
    {
        if (0 == cbRead)
        {
            break;
        }
 
        if (!CryptHashData(hHash, rgbFile, cbRead, 0))
        {
            return "";
        }
    }
 
    if (!bResult)
    {
        return "";
    }
 
    cbHash = MD5LEN;
    if (CryptGetHashParam(hHash, HP_HASHVAL, rgbHash, &cbHash, 0))
    {
        StringA result;
        result.reserve(40);
 
        for (DWORD i = 0; i < cbHash; i++)
        {
            result.push_back(rgbDigits[rgbHash[i] >> 4]);
            result.push_back(rgbDigits[rgbHash[i] & 0xf]);
        }
 
        return result;
    }
 
    return "";
}
 
int main()
{
    auto md5 = CalculateFileMD5(_T("E:\\aow-install-apk-100.xml"));
    std::cout << md5 << std::endl;
 
    return 0;
}

  测试:

 

posted @   bodong  阅读(157)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· winform 绘制太阳,地球,月球 运作规律
· AI与.NET技术实操系列(五):向量存储与相似性搜索在 .NET 中的实现
· 超详细:普通电脑也行Windows部署deepseek R1训练数据并当服务器共享给他人
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
· 上周热点回顾(3.3-3.9)
点击右上角即可分享
微信分享提示