MD5/SHA 加密处理

本文介绍如何使用Windows Crypt API中的Cryptographic Library来计算文件的MD5和SHA-1哈希值。通过逐步解析示例代码,展示了如何创建和使用hash对象,以及如何读取文件并传递数据到哈希函数。

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

看来需要用这个才是关键的处理。利用SHA1 源码来处理超大文件的checksum值计算真的好慢。

 

改用:Windows Crypt API。先尝试一下看不看快起来。

 

If you happen to be developing your code to be Microsoft Windows specific, the Windows Cryptographic Library contains the MD5 and SHA-1 hash functions. Microsoft provides functions called CryptCreateHash , CryptHashData , CryptGetHashParam and CryptDestroyHash which can be used to run the MD5 or SHA-1 hash functions on given message data to produce a message digest.

The use of these functions is pretty simple. First, as with most Windows cryptography operations, you need to create a Crypt Context using the CryptAcquireContext function. Once the context is acquired, you must create a hash object using the CryptCreateHash function. This hash object will hold the state of your hash function and will also hold the result when complete. When you call CryptCreateHash , you must specify which hash function you want. You can choose between several hash functions: CALG_MD2, CALG_MD4, CALG_MD5, CALG_SHA1, CALG_SHA_256, CALG_SHA_384 and CALG_SHA_512. Each of these cryptographic hash algorithms can have different message digest lengths.

Once the hash object is created, you must call the CryptHashData function. This function is used to pass the message data into the cryptographic hash function. This function can be called multiple times to keep feeding message data into the function. This is helpful if you are reading data from a file or other stream. Once you are done feeding data to the function, you need to retrieve the message digest from the hash object. You do this using the CryptGetHashParam function. This function will be called twice. Once for retrieving the length of the digest (HP_HASHSIZE) and again for retrieving the digest itself (HP_HASHVAL). Once you have the message digest, you can destroy the hash object and the crypt context using the CryptDestroyHash and CryptReleaseContext functions respectively.

 

 

参考:http://www.tomhandal.com/DevBlog/2010/06/13/md5sha-1-cryptographic-hash-functions-in-windows/

http://blog.youkuaiyun.com/dkfdtf/archive/2009/03/14/3989388.aspx

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值