2017-10-06 70 views
2

我想在VBScript中编写一段代码来计算给定文件的 SHA512值。根据MSFT文档 SHA512Managed对象的ComputeHash方法需要一个 字节数组作为输入。所以我用ADODB读取了SHA512值要计算的输入文件(因为,AFAIK,在VBScript中没有办法用 建一个Byte数组)。但是,我得到一个运行时错误5, '无效的过程调用或参数'调用该方法时。下面代码中的变量栏是Byte()类型 - VBScript表示。VBScript错误5试图用'System.Security.Cryptography.SHA512Managed'计算sha512

有人能告诉我发生了什么事吗?

代码:

Option Explicit 
' 
' 
' 
Dim scs, ado    
Dim bar, hsh 

Set scs = CreateObject("System.Security.Cryptography.SHA512Managed") 
Set ado = CreateObject("ADODB.Stream") 

ado.type = 1 ' TypeBinary 
ado.open 
ado.LoadFromFile WScript.ScriptFullName 
bar = ado.Read 
ado.Close 

MsgBox TypeName(bar) & "/" & LenB(bar) & "/" & Len(bar),,"Box 1" 
' Displays : "Byte()/876/438" 

On Error Resume Next 
' Attempt 1 
Set hsh = scs.ComputeHash(bar) 
MsgBox Hex(Err.Number) & "/" & Err.Description,,"Set hsh = " 
' Displays : "5/Invalid procedure call or argument" 

' Attempt 2 
hsh = scs.ComputeHash(bar) 
MsgBox Hex(Err.Number) & "/" & Err.Description,,"hsh = "  
' Displays : "5/Invalid procedure call or argument" 

MsgBox TypeName(scs),,"scs" ' Displays : "SHA512Managed" 

Set ado = Nothing 
Set scs = Nothing 

WScript.Quit 
+0

bar正在引用您正在运行的脚本。引用未使用的文件是否会得到相同的错误? – Sorceri

回答

3

使用

hsh = scs.ComputeHash_2((bar)) 

(没有固定的,_2后缀不挑其他ComputeHash方法中,通过值传递())

看到here

+0

非常感谢Merci beaucoup Ekkehard。它工作正常。 – LeChatDeNansen

+0

并再次感谢您的链接。指出的代码清醒,清晰和高效。 – LeChatDeNansen