2017-08-28 149 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