我需要一种在vbscript中本地计算文件的MD5 HASH的方法,而MD5类有一个名为GetMd5Hash的属性,这似乎可以帮助我。我只需要将文件读入字节数组,然后应用此方法。我在网页上找到了一个脚本代码 http://msdn.microsoft.com/en-us/library/system.security.cryptography.md5(v=vs.110).aspx?cs-save-lang=1&cs-lang=vb#code-snippet-2这正是我需要的,但当我使用命令cscript / E运行它时:vbs md5.vbs如果失败并显示错误代码:
md5.vbs(7,5)Microsoft VBScript编译错误:语法错误。有人可以帮我解决这个错误吗?
代码是:
Imports System
Imports System.Security.Cryptography
Imports System.Text
Class Program
Shared Sub Main(ByVal args() As String)
Dim [source] As String = "Hello World!"
Using md5Hash As MD5 = MD5.Create()
Dim hash As String = GetMd5Hash(md5Hash, source)
Console.WriteLine("The MD5 hash of " + source + " is: " + hash + ".")
Console.WriteLine("Verifying the hash...")
If VerifyMd5Hash(md5Hash, [source], hash) Then
Console.WriteLine("The hashes are the same.")
Else
Console.WriteLine("The hashes are not same.")
End If
End Using
End Sub 'Main
Shared Function GetMd5Hash(ByVal md5Hash As MD5, ByVal input As String) As String
' Convert the input string to a byte array and compute the hash.
Dim data As Byte() = md5Hash.ComputeHash(Encoding.UTF8.GetBytes(input))
' Create a new Stringbuilder to collect the bytes
' and create a string.
Dim sBuilder As New StringBuilder()
' Loop through each byte of the hashed data
' and format each one as a hexadecimal string.
Dim i As Integer
For i = 0 To data.Length - 1
sBuilder.Append(data(i).ToString("x2"))
Next i
' Return the hexadecimal string.
Return sBuilder.ToString()
End Function 'GetMd5Hash
' Verify a hash against a string.
Shared Function VerifyMd5Hash(ByVal md5Hash As MD5, ByVal input As String, ByVal hash As String) As Boolean
' Hash the input.
Dim hashOfInput As String = GetMd5Hash(md5Hash, input)
' Create a StringComparer an compare the hashes.
Dim comparer As StringComparer = StringComparer.OrdinalIgnoreCase
If 0 = comparer.Compare(hashOfInput, hash) Then
Return True
Else
Return False
End If
End Function 'VerifyMd5Hash
End Class 'Program
' This code example produces the following output:
'
' The MD5 hash of Hello World! is: ed076287532e86365e841e92bfc50d8c.
' Verifying the hash...
' The hashes are the same.
答案 0 :(得分:2)
此dom.Body
对我有用(在Win10 Ent N上测试):
md5_of_file.vbs
cmd中的用法示例:
' md5_of_file.vbs - Prints md5 hashes of specified files
' Combined at least from following sources:
' md5 from https://stackoverflow.com/a/31453654
' https://support.microsoft.com/en-us/help/276488/how-to-use-the-adodb-stream-object-to-send-binary-files-to-the-browser
Dim md5obj
set md5obj = CreateObject("System.Security.Cryptography.MD5CryptoServiceProvider")
md5obj.Initialize()
Set fso = CreateObject("Scripting.FileSystemObject")
Const adTypeBinary = 1
Set objStream = CreateObject("ADODB.Stream")
Function bytesToHex(aBytes)
Dim hexStr, x
For x=1 To lenb(aBytes)
hexStr= LCase(hex(ascb(midb( (aBytes) ,x,1))))
if len(hexStr)=1 then hexStr="0" & hexStr
bytesToHex=bytesToHex & hexStr
Next
end Function
' read bytes from fileName
Function LoadFile(fileName)
objStream.Open
objStream.Type = adTypeBinary
objStream.LoadFromFile( fileName )
LoadFile = objStream.Read
objStream.Close
End Function
' returns hex value of md5 hash from content of fileName
Function HashOfFile(fileName)
fileBytes = LoadFile(fileName)
' Do NOT omit braces around fileBytes - it will not work...
md5hashBytes = md5obj.ComputeHash_2( (fileBytes) )
HashOfFile = bytesToHex(md5hashBytes)
End Function
If WScript.Arguments.Count < 1 Then
WScript.Echo("Script to compute md5 hash of specified files...")
WScript.Echo("Usage: " & WScript.ScriptName & " file1 ...")
WScript.Quit(1)
End If
For i = 0 To Wscript.Arguments.Count-1
fileName = WScript.Arguments.Item(i)
WScript.Echo( HashOfFile(fileName) & " " & fileName )
Next
版权免责声明-大多数代码来自https://stackoverflow.com/a/31453654
答案 1 :(得分:0)
获取md5 / sha1代码的Microsoft File Checksum Integrity Verifier,这是命令行模式,非常快。只需将其解压缩到System32文件夹即可。