एक पुराना सवाल है कि एक बेहतर जवाब इस्तेमाल कर सकते हैं। ये फ़ंक्शन विशेष रूप से हैशिंग फ़ाइलों के लिए हैं, हैशिंग पासवर्ड के लिए नहीं। बोनस के रूप में, मैं SHA1 के लिए एक फ़ंक्शन समेत हूं। यदि आप प्रकार की घोषणाओं से छुटकारा पा लेते हैं तो ये कार्य VBScript में भी काम करते हैं, सिवाय इसके कि GetSileBytes फ़ंक्शन को FileSystemObject (या संभवतः एडीओ स्ट्रीम) का उपयोग करने के लिए बदला जाना आवश्यक है क्योंकि नि: शुल्क फ़ाइल VBScript में मौजूद नहीं है।
Private Sub TestMD5()
Debug.Print FileToMD5Hex("C:\test.txt")
Debug.Print FileToSHA1Hex("C:\test.txt")
End Sub
Public Function FileToMD5Hex(sFileName As String) As String
Dim enc
Dim bytes
Dim outstr As String
Dim pos As Integer
Set enc = CreateObject("System.Security.Cryptography.MD5CryptoServiceProvider")
'Convert the string to a byte array and hash it
bytes = GetFileBytes(sFileName)
bytes = enc.ComputeHash_2((bytes))
'Convert the byte array to a hex string
For pos = 1 To LenB(bytes)
outstr = outstr & LCase(Right("0" & Hex(AscB(MidB(bytes, pos, 1))), 2))
Next
FileToMD5Hex = outstr
Set enc = Nothing
End Function
Public Function FileToSHA1Hex(sFileName As String) As String
Dim enc
Dim bytes
Dim outstr As String
Dim pos As Integer
Set enc = CreateObject("System.Security.Cryptography.SHA1CryptoServiceProvider")
'Convert the string to a byte array and hash it
bytes = GetFileBytes(sFileName)
bytes = enc.ComputeHash_2((bytes))
'Convert the byte array to a hex string
For pos = 1 To LenB(bytes)
outstr = outstr & LCase(Right("0" & Hex(AscB(MidB(bytes, pos, 1))), 2))
Next
FileToSHA1Hex = outstr 'Returns a 40 byte/character hex string
Set enc = Nothing
End Function
Private Function GetFileBytes(ByVal path As String) As Byte()
Dim lngFileNum As Long
Dim bytRtnVal() As Byte
lngFileNum = FreeFile
If LenB(Dir(path)) Then ''// Does file exist?
Open path For Binary Access Read As lngFileNum
ReDim bytRtnVal(LOF(lngFileNum) - 1&) As Byte
Get lngFileNum, , bytRtnVal
Close lngFileNum
Else
Err.Raise 53
End If
GetFileBytes = bytRtnVal
Erase bytRtnVal
End Function
जो केवल एक स्ट्रिंग का हैश बनाता है। अगर फ़ाइल में 700 एमबी है, तो मैं इसे एक स्ट्रिंग में नहीं डाल सकता और एमडी 5 हैश करता हूं। तो, क्या फाइल संस्करण या किसी अन्य समाधान के लिए md5 हैश है? : पी –