Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

File's hash code - how to get it? 3

Status
Not open for further replies.

IlyaRabyy

Programmer
Nov 9, 2010
566
0
16
US
Colleagues,
the subject line says it.
Search in the MS Learning (aka on-line Help) produced "you're in a helicopter" results.
GetFileHashCode() threw an exception even before Ctrl+S.

AHWBGA!

Regards,

Ilya
 
here's an example I've knocked together


Code:
[COLOR=blue]Imports System.Security.Cryptography
Imports System.IO

Public Class Form1
    Public Enum HashOption
        hashMD5
        hashSHA1
        hashSHA256
        hashSHA512
    End Enum

    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        MessageBox.Show(filehash("d:\downloads\deleteme\test.txt", HashOption.hashSHA256))
    End Sub

    Function filehash(file_name As String, Optional hashmethod As HashOption = HashOption.hashSHA512) As String
        Dim hash
        Dim hashValue() As Byte

        Dim fileStream As FileStream = File.OpenRead(file_name)
        fileStream.Position = 0

        [COLOR=green]' There are multiple ways of selecting the desired hashing algorithm
        ' This is just one[/color]
        Select Case hashmethod
            Case HashOption.hashMD5
                hash = MD5.Create()
            Case HashOption.hashSHA1
                hash = SHA1.Create()
            Case HashOption.hashSHA256
                hash = SHA256.Create()
            Case HashOption.hashSHA512
                hash = SHA512.Create()
            Case Else
        End Select

        hashValue = hash.ComputeHash(fileStream)
        Dim hash_hex = Convert.ToHexString(hashValue)
        fileStream.Close()
        filehash = hash_hex

    End Function
End Class[/color]
 
Thank you, StrongM!
There's one drawback, though:

2024-07-15_ConvertToHexString_errs_z9y9wy.jpg


Is it because my testing program is on .NET Framework 4.8, and Convert.ToHexString() method

2024-07-15_ConvertToHexString_Is_In_dotNet_5_xrgkga.jpg


I opened the Project Options screen (VS 2022), and there's no option for .NET, only for the Framework...

[ponder]

Regards,

Ilya
 
if this doesn't work with the older .NET framework:
Code:
Dim hash_hex = Convert.ToHexString(hashValue)
then try if this works for you:
Code:
Dim hash_hex = BitConverter.ToString(hashValue).Replace("-", "");
 
mikrom has provided the earlier code that can be used
 
Sorry - can I just check - did my solution work for you?
 
Hi IlyaRabyy,
it's fine that it worked, but the credit goes to strongm for providing an essential solution example, with my involvement being only very marginal.
 
StrongM said:
can I just check - did my solution work for you?

It did, muchos gratis, le grand merci, tada raba, bol'shoe spasibo, djakue bardzo! :)

Regards,

Ilya
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top