Results 1 to 2 of 2

Thread: [RESOLVED] Getting Hash of File in VB.net -- Code does not produce same hash as certutil.exe

  1. #1

    Thread Starter
    Member
    Join Date
    Aug 2014
    Posts
    51

    Resolved [RESOLVED] Getting Hash of File in VB.net -- Code does not produce same hash as certutil.exe

    I am looking to verify the integrity of some files in a VB.net program and have encountered something strange.

    Originally, I was using the attached class to produce a hash, but during testing noticed that it does not produce the same hash for any given file as Windows certutil.exe.

    Clearly I am missing something. Does anyone know what is missing? Or is there a better way to get the hash of a file in VB.net? I could use a process to run certutil.exe, but would prefer to avoid a process if possible.


    Class:
    Code:
    Imports System.Text
    Imports System.Security.Cryptography
    
    Public Class SHA
    
        Public Shared Function GenerateSHA512String(ByVal inputString) As String
            Dim sha512 As SHA512 = SHA512Managed.Create()
            Dim bytes As Byte() = Encoding.UTF8.GetBytes(inputString)
            Dim hash As Byte() = sha512.ComputeHash(bytes)
            Dim stringBuilder As New StringBuilder()
    
            For i As Integer = 0 To hash.Length - 1
                stringBuilder.Append(hash(i).ToString("X2"))
            Next
    
            Return stringBuilder.ToString()
        End Function
    
    End Class
    Last edited by walt4; May 9th, 2021 at 10:05 AM.

  2. #2

    Thread Starter
    Member
    Join Date
    Aug 2014
    Posts
    51

    Re: Getting Hash of File in VB.net -- Code does not produce same hash as certutil.exe

    I have been able to resolve the problem with the following changes to the class:

    Code:
    Imports System
    Imports System.IO
    Imports System.Text
    Imports System.Security.Cryptography
    
    Public Class SHA
    
        Public Shared Function GenerateSHA512String(ByVal inputString As String) As String
            Using SHA512 As SHA512 = SHA512Managed.Create()
    
                Using fileStream As FileStream = File.OpenRead(inputString)
                    Return BitConverter.ToString(SHA512.ComputeHash(fileStream)).Replace("-", "").ToLowerInvariant()
                End Using
    
            End Using
        End Function
    
    End Class
    Now, the hash generated is the same hash as shown when using certutil.exe.

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width