VBScript error 5 trying to compute sha512 with 'System.Security.Cryptography.SHA512Managed'

浪尽此生 提交于 2019-12-24 00:23:03

问题


I am trying to write a piece of code in VBScript to compute the SHA512 value for a given file. According to MSFT documentation the ComputeHash method of the SHA512Managed object requires a Byte array as input. So I used ADODB to read the input file which SHA512 value is to be computed (Because, AFAIK, there is no way to build a Byte array in VBScript). However I get a runtime error 5, 'Invalid procedure call or argument' when calling the method. The variable bar in the code below is of type Byte() - VBScript says.

Could anyone tell me what is going wrong ?

Code :

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

回答1:


Use

hsh = scs.ComputeHash_2((bar))

(no set, _2 suffix not to pick the other ComputeHash method, pass by value ())

see here.



来源:https://stackoverflow.com/questions/46612969/vbscript-error-5-trying-to-compute-sha512-with-system-security-cryptography-sha

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!