2017-08-28 146 views
-1

如何在vb.net中獲取SHA1,SHA256,SHA512,MD5校驗值本身?VB.Net如何獲得彙編校驗和(SHA1,MD5,SHA256,SHA512)值?

我可以從第三方實用程序如哈希得到我的exe文件校驗和值... 但我想獲得我自己的程序集校驗和值本身?

請幫助

更新:1我想我自己

 Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click 
    Dim _myexe$ 
    Try 
     _myexe$ = IO.Path.Combine(My.Application.Info.DirectoryPath, My.Application.Info.AssemblyName & ".exe") 
     Using _sha512 As New System.Security.Cryptography.SHA512CryptoServiceProvider 
      Using stream = File.OpenRead(_myexe$) 
       Dim _hash = _sha512.ComputeHash(stream) 
       Trace.WriteLine(BitConverter.ToString(_hash).Replace("-", String.Empty)) 
      End Using 
     End Using 
    Catch ex As Exception 
     Trace.WriteLine(Err.Description) 
    End Try 
End Sub 

是獲得校驗值電流(運行)裝配這個正確的方式?

+1

僅供參考,你可以替換整個'IO.Path.Combine(My.Application.Info.DirectoryPath,My.Application.Info.AssemblyName與名爲 「.exe」)'和' Application.ExecutablePath'。 –

+0

非常感謝你得到程序集路徑+1 – DVELPR

回答

1

使用System.Security.Cryptography.MD5

Using md5Hash = MD5.Create() 
    Using stream = File.OpenRead(filename) ' file name is your assembly 
     Return md5Hash .ComputeHash(stream) 
    End Using 
End Using 
+0

感謝「al-3sli」,我有一點疑惑,是dato錯誤,而執行程序集像文件讀取錯誤或文件正在使用另一個進程...等等? – DVELPR

+0

@ pc8181:總會有IO錯誤被拋出的可能性。這一切都取決於其他東西是否鎖定文件。儘管打開它來閱讀不應該是一個問題,除非某些外部程序完全鎖定文件。 –

+0

是的,我害怕這個,有任何其他方式來獲取校驗和值本身 – DVELPR