Results 1 to 5 of 5

Thread: [RESOLVED] [2005] SHA-1/MD5 hash values odd

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Nov 2007
    Location
    Seattle, WA
    Posts
    76

    Resolved [RESOLVED] [2005] SHA-1/MD5 hash values odd

    I am having a strange issue with the SHA-1 and MD5 values from files. using the below code computing the value for the same file I get the following values:
    MD5 - F7ABA96EFC684E3E75EEE8C3E46DDFA
    SHA-1- 714E16482F18EBDBCD6B72FC1B0536CF0DC5B72

    My issue is when i compute the hash values using Microsoft's FCIV.exe tool, or other tools that compute the values they have some extra zero's that my tool does not have. here is what the FCIV.exe tool outputs.

    MD5 - 0f7aba96efc684e3e75eee8c3e46ddfa
    SHA-1- 714e16482f18eb0dbcd6b72fc1b0536cf0dc5b72

    Does anyone know why there is an extra zero in the output from the other tools or why my code is not putting them in?

    MD5
    Code:
        Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
            If Not (Me.OpenFileDialog1.FileName = String.Empty) Then
                Dim f As New System.IO.FileStream(Me.OpenFileDialog1.FileName, IO.FileMode.Open)
                Dim md5 As New System.Security.Cryptography.MD5CryptoServiceProvider
                Dim hash As Array
                Dim md5Hash As String
                Dim sb As New System.Text.StringBuilder
                md5.ComputeHash(f)
                hash = md5.Hash
                For Each hashByte As Byte In hash
                    sb.Append(String.Format("{0:X1}", hashByte))
                Next
                md5Hash = sb.ToString
                f.Close()
                Me.TextBox2.Text = sb.ToString
            End If
        End Sub
    SHA-1
    Code:
        Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
            If Not (Me.OpenFileDialog1.FileName = String.Empty) Then
                Dim f As New System.IO.FileStream(Me.OpenFileDialog1.FileName, IO.FileMode.Open)
                Dim sha As New System.Security.Cryptography.SHA1CryptoServiceProvider
                Dim hash As Array
                Dim shaHash As String
                Dim sb As New System.Text.StringBuilder
                sha.ComputeHash(f)
                hash = sha.Hash
                For Each hashByte As Byte In hash
                    sb.Append(String.Format("{0:X1}", hashByte))
                Next
                shaHash = sb.ToString
                f.Close()
                Me.TextBox2.Text = sb.ToString
            End If
        End Sub

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: [2005] SHA-1/MD5 hash values odd

    Run this code and you should find your answer:
    Code:
    Dim b1 As Byte = 255
    Dim b2 As Byte = 15
    
    MessageBox.Show(b1.ToString("X1"), "X1")
    MessageBox.Show(b2.ToString("X1"), "X1")
    MessageBox.Show(b1.ToString("X2"), "X2")
    MessageBox.Show(b2.ToString("X2"), "X2")
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  3. #3

    Thread Starter
    Lively Member
    Join Date
    Nov 2007
    Location
    Seattle, WA
    Posts
    76

    Resolved Re: [2005] SHA-1/MD5 hash values odd

    Thanks again John...your input, as always, is the best! forgive me for my lack of experience with all of this, but if you have the time...would you explain exactly how the part
    Code:
    (String.Format("{0:X2}", hashByte))
    works? either way...thanks again for all of the help you provide!!!

  4. #4
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: [RESOLVED] [2005] SHA-1/MD5 hash values odd

    You shouldn't really be using:
    vb.net Code:
    1. String.Format("{0:X2}", hashByte)
    but rather:
    vb.net Code:
    1. hashByte.ToString("X2")
    Either way though, the result is the same. "X2" is the format string. The "X" means the number will be formatted as hexadecimal in upper case, while the "2" means the result should be padded to 2 digits with leading zeroes if required.

    If you don't know how String.Format works then try executing this code and it should make it clear:
    vb.net Code:
    1. Dim aString As String = "Hello World"
    2. Dim aNumber As Integer = 100
    3. Dim aDate As Date = Date.Now
    4.  
    5. MessageBox.Show(String.Format("0={0}, 1={1}, 2={2}, 0={0}", _
    6.                               aString, _
    7.                               aNumber, _
    8.                               aDate))
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  5. #5

    Thread Starter
    Lively Member
    Join Date
    Nov 2007
    Location
    Seattle, WA
    Posts
    76

    Re: [RESOLVED] [2005] SHA-1/MD5 hash values odd

    You're a gentleman and a scholar...thanks again my friend for all the help

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