2

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
LeChatDeNansen
  • 133
  • 1
  • 5
  • bar is referencing the script you are running. Do you get the same error referencing a file that is not in use? – Sorceri Oct 06 '17 at 19:58

1 Answers1

3

Use

hsh = scs.ComputeHash_2((bar))

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

see here.

Ekkehard.Horner
  • 38,498
  • 2
  • 45
  • 96