2017-10-06 33 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