VB.NET or C# - How to (HELP)

Discussion in 'Mixed Languages' started by QuantumBug, Mar 9, 2012.

  1. QuantumBug

    QuantumBug MDL Developer

    Mar 7, 2012
    1,485
    1,322
    60
    I am looking for a way in VB.NET to get my application to display it's OWN MD5 from internal code, so basically calculate it's own checksums and display it, I can't find a way to do this.

    Once I have aquired the code it will be used for security purposes within a certain application of mine, and since I'm only really novice at programming I'm still mainly working on VB.NET and learning C# from my Uncle.

    Can anyone explain how I can achieve this?

    Thanks in advance.
     
  2. QuantumBug

    QuantumBug MDL Developer

    Mar 7, 2012
    1,485
    1,322
    60
    #2 QuantumBug, Mar 10, 2012
    Last edited by a moderator: Apr 20, 2017
    (OP)
    Ok, I got it working myself with some help from Daz. Seems I can't find this anywhere else online I'll post the code for everyone else.

    Code:
    
    Imports System.Security.Cryptography
    
    Public Function MD5Sec(ByVal filepath As String) As String
    
            Using reader As New System.IO.FileStream(filepath, IO.FileMode.Open, IO.FileAccess.Read)
                Using md5 As New System.Security.Cryptography.MD5CryptoServiceProvider
    
                    Dim hash() As Byte = md5.ComputeHash(reader)
    
                    Return ByteArrayToString(hash)
    
                End Using
            End Using
    
        End Function
    
        Private Function ByteArrayToString(ByVal arrInput() As Byte) As String
    
            Dim sb As New System.Text.StringBuilder(arrInput.Length * 2)
    
            For i As Integer = 0 To arrInput.Length - 1
                sb.Append(arrInput(i).ToString("X2"))
            Next
    
            Return sb.ToString().ToUpper
    
        End Function
    
    'Usage below, mine is in the form load event and 'Self_MD5' can be a textbox, label, linklabel etc
    
            Try
                Self_MD5.Text = MD5Sec(Environment.CurrentDirectory + "\applicationnamehere.exe")
            Catch md5null As Exception
                Self_MD5.Text = ""
                'Or MessageBox.Show("message about the error", "Couldn't hash MD5", MessageBoxButtons.OK, MessageBoxStyle.Error)
            End Try
    
    
     
  3. Alphawaves

    Alphawaves Super Moderator/Developer
    Staff Member

    Aug 11, 2008
    5,891
    20,335
    180
    #3 Alphawaves, Mar 12, 2012
    Last edited by a moderator: Apr 20, 2017
    Something a little shorter would be:
    Code:
    Public Function CurrentMD5()
            Dim sb As New StringBuilder()
            Dim Hash As MD5 = MD5.Create()
            Using mf As FileStream = File.OpenRead(Process.GetCurrentProcess().ProcessName.ToString() & ".exe")
                For Each b As Byte In Hash.ComputeHash(mf)
                    sb.Append(b.ToString("x2").ToUpper())
                Next
            End Using
            Return sb.ToString()
        End Function
    Usage:
    Code:
    Try
            Label1.Text = CurrentMD5()
            Catch ex As Exception
                MsgBox(ex.Message)
            End Try
    ;)
     
    Stop hovering to collapse... Click to collapse... Hover to expand... Click to expand...
  4. QuantumBug

    QuantumBug MDL Developer

    Mar 7, 2012
    1,485
    1,322
    60
    Actually Alphawaves that's a good idea.

    The reason being is: with my code if my .exe is renamed the MD5 doesn't show.

    Thanks a lot for adding.
     
  5. Alphawaves

    Alphawaves Super Moderator/Developer
    Staff Member

    Aug 11, 2008
    5,891
    20,335
    180
    #5 Alphawaves, Mar 13, 2012
    Last edited by a moderator: Apr 20, 2017
    You could also use AppDomain.CurrentDomain.FriendlyName to return the applications name along with the file extension.
    ie: myapp.exe

    VB.NET
    Code:
    Public Function CurrentMD5()
            Dim sb As New StringBuilder()
            Dim Hash As MD5 = MD5.Create()
            Using mf As FileStream = File.OpenRead(AppDomain.CurrentDomain.FriendlyName)
                For Each b As Byte In Hash.ComputeHash(mf)
                    sb.Append(b.ToString("x2").ToUpper())
                Next
            End Using
            Return sb.ToString()
        End Function
    Code:
    Label1.Text = CurrentMD5()
    
    C#
    Code:
     public static string CurrentMD5()
            {
                StringBuilder sb = new StringBuilder();
                MD5 Hash = MD5.Create();
                using (FileStream mf = File.OpenRead(AppDomain.CurrentDomain.FriendlyName))
                {
                    foreach (byte b in Hash.ComputeHash(mf))
                    {
                        sb.Append(b.ToString("x2").ToUpper());
                    }
                }
                return sb.ToString();
            }
    Code:
    label1.Text = CurrentMD5();
     
    Stop hovering to collapse... Click to collapse... Hover to expand... Click to expand...