{"id":1738,"date":"2024-08-26T08:00:00","date_gmt":"2024-08-26T00:00:00","guid":{"rendered":"http:\/\/www.wyrcad.com\/?p=1738"},"modified":"2024-08-26T20:33:54","modified_gmt":"2024-08-26T12:33:54","slug":"%e6%96%87%e4%bb%b6%e7%9a%84-md5-hash-%e7%ae%97%e6%b3%95","status":"publish","type":"post","link":"https:\/\/www.wyrcad.com\/?p=1738","title":{"rendered":"\u6587\u4ef6\u7684 Hash \u7b97\u6cd5"},"content":{"rendered":"\n<p>&#8216;\u8ba1\u7b97 MD2\u3001MD3\u3001MD4\u3001MD5\u3001SHA1 \u7b49\u6563\u5217\u503c\u3002<\/p>\n\n\n\n<p>Option Explicit<br>Private Declare Function CryptAcquireContext Lib &#8220;advapi32.dll&#8221; Alias &#8220;CryptAcquireContextA&#8221; (ByRef phProv As Long, ByVal pszContainer As String, ByVal pszProvider As String, ByVal dwProvType As Long, ByVal dwFlags As Long) As Long<br>Private Declare Function CryptReleaseContext Lib &#8220;advapi32.dll&#8221; (ByVal hProv As Long, ByVal dwFlags As Long) As Long<br>Private Declare Function CryptCreateHash Lib &#8220;advapi32.dll&#8221; (ByVal hProv As Long, ByVal Algid As Long, ByVal hKey As Long, ByVal dwFlags As Long, ByRef phHash As Long) As Long<br>Private Declare Function CryptDestroyHash Lib &#8220;advapi32.dll&#8221; (ByVal hHash As Long) As Long<br>Private Declare Function CryptHashData Lib &#8220;advapi32.dll&#8221; (ByVal hHash As Long, pbData As Any, ByVal dwDataLen As Long, ByVal dwFlags As Long) As Long<br>Private Declare Function CryptGetHashParam Lib &#8220;advapi32.dll&#8221; (ByVal hHash As Long, ByVal dwParam As Long, pbData As Any, pdwDataLen As Long, ByVal dwFlags As Long) As Long<br>Private Const PROV_RSA_FULL = 1<br>Private Const CRYPT_NEWKEYSET = &amp;H8<br>Private Const ALG_CLASS_HASH = 32768<br>Private Const ALG_TYPE_ANY = 0<br>Private Const ALG_SID_MD2 = 1<br>Private Const ALG_SID_MD4 = 2<br>Private Const ALG_SID_MD5 = 3<br>Private Const ALG_SID_SHA1 = 4<br>Enum HashAlgorithm<br>MD2 = ALG_CLASS_HASH Or ALG_TYPE_ANY Or ALG_SID_MD2<br>MD4 = ALG_CLASS_HASH Or ALG_TYPE_ANY Or ALG_SID_MD4<br>MD5 = ALG_CLASS_HASH Or ALG_TYPE_ANY Or ALG_SID_MD5<br>SHA1 = ALG_CLASS_HASH Or ALG_TYPE_ANY Or ALG_SID_SHA1<br>End Enum<br>Private Const HP_HASHVAL = 2<br>Private Const HP_HASHSIZE = 4<\/p>\n\n\n\n<p>Function HashFile(ByVal FileName As String, Optional ByVal Algorithm As HashAlgorithm = MD5) As String<br>Dim hCtx As Long<br>Dim hHash As Long<br>Dim lFile As Long<br>Dim lRes As Long<br>Dim lLen As Long<br>Dim lIdx As Long<br>Dim abHash() As Byte<br>If Len(Dir$(FileName)) = 0 Then Err.Raise 53<br>lRes = CryptAcquireContext(hCtx, vbNullString, vbNullString, PROV_RSA_FULL, 0)<br>If lRes = 0 And Err.LastDllError = &amp;H80090016 Then<br>lRes = CryptAcquireContext(hCtx, vbNullString, vbNullString, PROV_RSA_FULL, CRYPT_NEWKEYSET)<br>End If<br>If lRes &lt;&gt; 0 Then<br>lRes = CryptCreateHash(hCtx, Algorithm, 0, 0, hHash)<br>If lRes &lt;&gt; 0 Then<br>lFile = FreeFile<br>Open FileName For Binary As lFile<br>If Err.Number = 0 Then<br>Const BLOCK_SIZE As Long = 32 * 1024&amp; &#8216; 32K<br>ReDim abBlock(1 To BLOCK_SIZE) As Byte<br>Dim lCount As Long<br>Dim lBlocks As Long<br>Dim lLastBlock As Long<br>lBlocks = LOF(lFile) \\ BLOCK_SIZE<br>lLastBlock = LOF(lFile) &#8211; lBlocks * BLOCK_SIZE<br>For lCount = 1 To lBlocks<br>Get lFile, , abBlock<br>lRes = CryptHashData(hHash, abBlock(1), BLOCK_SIZE, 0)<br>If lRes = 0 Then Exit For<br>Next<br>If lLastBlock &gt; 0 And lRes &lt;&gt; 0 Then<br>ReDim abBlock(1 To lLastBlock) As Byte<br>Get lFile, , abBlock<br>lRes = CryptHashData(hHash, abBlock(1), lLastBlock, 0)<br>End If<br>Close lFile<br>End If<br>If lRes &lt;&gt; 0 Then<br>lRes = CryptGetHashParam(hHash, HP_HASHSIZE, lLen, 4, 0)<br>If lRes &lt;&gt; 0 Then<br>ReDim abHash(0 To lLen &#8211; 1)<br>lRes = CryptGetHashParam(hHash, HP_HASHVAL, abHash(0), lLen, 0)<br>If lRes &lt;&gt; 0 Then<br>For lIdx = 0 To UBound(abHash)<br>HashFile = HashFile &amp; _<br>Right$(&#8220;0&#8221; &amp; Hex$(abHash(lIdx)), 2)<br>Next<br>End If<br>End If<br>End If<br>CryptDestroyHash hHash<br>End If<br>End If<br>CryptReleaseContext hCtx, 0<br>If lRes = 0 Then Err.Raise Err.LastDllError<br>End Function<\/p>\n","protected":false},"excerpt":{"rendered":"<p>&#8216;\u8ba1\u7b97 MD2\u3001MD3\u3001MD4\u3001MD5\u3001SHA1 \u7b49\u6563\u5217\u503c\u3002 Option ExplicitPri &hellip; <a href=\"https:\/\/www.wyrcad.com\/?p=1738\" class=\"more-link\">\u7ee7\u7eed\u9605\u8bfb<span class=\"screen-reader-text\">\u201c\u6587\u4ef6\u7684 Hash \u7b97\u6cd5\u201d<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[12],"tags":[24,17],"class_list":["post-1738","post","type-post","status-publish","format-standard","hentry","category-bianchengyuandi","tag-vb","tag-biancheng"],"_links":{"self":[{"href":"https:\/\/www.wyrcad.com\/index.php?rest_route=\/wp\/v2\/posts\/1738","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.wyrcad.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.wyrcad.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.wyrcad.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.wyrcad.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=1738"}],"version-history":[{"count":4,"href":"https:\/\/www.wyrcad.com\/index.php?rest_route=\/wp\/v2\/posts\/1738\/revisions"}],"predecessor-version":[{"id":1773,"href":"https:\/\/www.wyrcad.com\/index.php?rest_route=\/wp\/v2\/posts\/1738\/revisions\/1773"}],"wp:attachment":[{"href":"https:\/\/www.wyrcad.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1738"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.wyrcad.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1738"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.wyrcad.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1738"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}