0

我正在遵循Sysinternals检查文件数字签名的代码

在那里我需要获取文件的哈希值。对于那部分,我正在使用以下代码。

        'Open file
        Dim fs As FileStream = New FileStream(filename, FileMode.Open, FileAccess.Read, FileShare.Read, 8)


        'Get the size we need for our hash.
        Dim fileHandle As IntPtr = fs.SafeFileHandle.DangerousGetHandle()
        Dim hashSize As Integer
        Dim hash As Byte() = New Byte(256) {}

        Dim b As Boolean = CryptCATAdminCalcHashFromFileHandle(fileHandle, hashSize, Nothing, 0)

        'check results
        System.Console.WriteLine(fileHandle)
        System.Console.WriteLine(hashSize)
        System.Console.WriteLine(hash)
        System.Console.WriteLine(b)

使用 wintrust.dll 如下

    <DllImport("Wintrust.dll", PreserveSig:=True, SetLastError:=False)> _
    Private Shared Function CryptCATAdminCalcHashFromFileHandle(ByRef fileHandle As IntPtr, ByVal hash As IntPtr, ByVal hashSize As Byte(), ByVal dwFlags As Integer) As Boolean
    End Function

文件句柄的输出在不同的执行中获得不同的值(这没关系)但是哈希大小和哈希始终为零。b 总是假的。

我在这里错过了什么吗?请指教

4

1 回答 1

2

声明应该是这样的:

<DllImport("Wintrust.dll", PreserveSig:=True, SetLastError:=False)> _
Private Shared Function CryptCATAdminCalcHashFromFileHandle(ByVal fileHandle As IntPtr, ByRef hashSize As Integer, ByVal hash As Byte(), ByVal dwFlags As Integer) As Boolean
End Function

您需要初始化hashSize缓冲区的大小,并将缓冲区传递给函数:

hashSize = 256
Dim b As Boolean = CryptCATAdminCalcHashFromFileHandle(fileHandle, hashSize, hash, 0)

hashSize当函数完成时,应该更新以包含实际的哈希大小。

(我的 VB 很生疏,如有错别字请见谅。)

于 2012-09-26T18:34:49.517 回答