本文介绍了VBScript 错误 5 尝试使用“System.Security.Cryptography.SHA512Managed"计算 sha512的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试用 VBScript 编写一段代码来计算给定文件的 SHA512 值.根据 MSFT 文档SHA512Managed 对象的 ComputeHash 方法需要一个字节数组作为输入.所以我用 ADODB 来读取输入文件要计算 SHA512 值(因为,AFAIK,没有办法在 VBScript 中构建一个字节数组).但是我得到一个运行时错误 5,调用方法时出现无效的过程调用或参数".这下面代码中的变量 bar 是 Byte() 类型 - VBScript 说.

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

谁能告诉我出了什么问题?

Could anyone tell me what is going wrong ?

代码:

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

推荐答案

使用

hsh = scs.ComputeHash_2((bar))

(不设置,_2后缀不挑其他ComputeHash方法,传值())

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

请参阅此处.

这篇关于VBScript 错误 5 尝试使用“System.Security.Cryptography.SHA512Managed"计算 sha512的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-30 13:05