isuru chathuranga
isuru chathuranga

Reputation: 1025

Calculating the Hash of a file using CryptCATAdminCalcHashFromFileHandle in vb

I am following the code for Checking the Digital Signature of a file at Sysinternals

There I need to get the hash value of a file. for that part I'm using the following code.

        '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)

Using wintrust.dll as follows

    <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

Output for file handle gets different values in different executions (Which is ok) But the hashsize, and hash are always zero. and b is always false.

Am I missing something here. Please advice

Upvotes: 0

Views: 1131

Answers (1)

arx
arx

Reputation: 16906

The declaration should be something like:

<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

You need to initialize hashSize to the size of your buffer, and pass the buffer to the function:

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

hashSize should be updated to contain the actual hash size when the function completes.

(My VB is very rusty, so apologies for any typos.)

Upvotes: 2

Related Questions