Generate hashes MD5, SHA-1 and SHA-256 of a file

Page 1 / 1

The only fast way to determine if a file is corrupted or not is to use hash functions "MD5", "SHA-1", "SHA-256", ... These functions also allow to compare files, allowing you to identify possible duplicates.

To calculate these hashes in .Net, nothing more simple. Just use the "ComputeHash" function depending on the class of the desired hash that is in the namespace "System.Security.Cryptography".

Since the procedure is similar to a line near to all existing types of hashes. We pooled 3 hashes most used, in this tutorial.

Important : The recommended hash is SHA-256.
Because, as shown in "Wikipedia": "In 2004, a Chinese team discovers complete MD5 collisions is therefore no longer considered safe are now cryptographically suggest using algorithms such as SHA-256, RIPEMD-160 or Whirlpool.".

  1. Calculate hashes : MD5, SHA-1 and SHA-256
  2. Source of the complete program

1. Calculate hashes : MD5, SHA-1 and SHA-256

Before we begin, we will import the following namespaces :
- System.IO
- System.Security
- System.Security.Cryptography

Note : Imports must be put top of the file before the line "Public Class xxx".


' We import System.IO for files
Imports System.IO
' And System.Security.Cryptography for Hashes : MD5, SHA1, SHA256, ...
Imports System.Security
Imports System.Security.Cryptography

Then, we will create the main function we will name "hash_generator" and take as parameters : the type of hash and the desired path of the file for which we want to calculate the hash.


' Function to obtain the desired hash of a file
Function hash_generator(ByVal hash_type As String, ByVal file_name As String)

  ' We declare the variable : hash
  Dim hash
  If hash_type.ToLower = "md5" Then
    ' Initializes a md5 hash object
    hash = MD5.Create
  ElseIf hash_type.ToLower = "sha1" Then
    ' Initializes a SHA-1 hash object
    hash = SHA1.Create()
  ElseIf hash_type.ToLower = "sha256" Then
    ' Initializes a SHA-256 hash object
    hash = SHA256.Create()
    MsgBox("Unknown type of hash : " & hash_type, MsgBoxStyle.Critical)
    Return False
  End If
  ' We declare a variable to be an array of bytes
  Dim hashValue() As Byte
  ' We create a FileStream for the file passed as a parameter
  Dim fileStream As FileStream = File.OpenRead(file_name)
  ' We position the cursor at the beginning of stream
  fileStream.Position = 0
  ' We calculate the hash of the file
  hashValue = hash.ComputeHash(fileStream)
  ' The array of bytes is converted into hexadecimal before it can be read easily
  Dim hash_hex = PrintByteArray(hashValue)
  ' We close the open file
  ' The hash is returned
  Return hash_hex
End Function

Now that the main function is created, you had to realize that "PrintByteArray" function was not defined. This function is based on that of Microsoft and is used to convert an array of bytes in hexadecimal.


' We traverse the array of bytes and converting each byte in hexadecimal
Public Function PrintByteArray(ByVal array() As Byte)

  Dim hex_value As String = ""

  ' We traverse the array of bytes
  Dim i As Integer
  For i = 0 To array.Length - 1
    ' We convert each byte in hexadecimal
    hex_value += array(i).ToString("X2")
  Next i
  ' We return the string in lowercase
  Return hex_value.ToLower

End Function

For the code to be clearer, and also by quickly you can create named functions with the types of hashes to generate.


' md5 is a reserved name, so we named the function : md5_hash
Function md5_hash(ByVal file_name As String)
  Return hash_generator("md5", file_name)
End Function
Function sha_1(ByVal file_name As String)
  Return hash_generator("sha1", file_name)
End Function
Function sha_256(ByVal file_name As String)
  Return hash_generator("sha256", file_name)
End Function

And finally, to use these features, simply pass the path of the desired file as parameter.


' We specify the file path
Dim path As String = "C:\mon_fichier.exe"

' And we compute the hashes : MD5, SHA-1 and SHA-256
Dim hash_md5 = md5_hash(path)
Dim hash_sha_1 = sha_1(path)
Dim hash_sha_256 = sha_256(path)

2. Source of the complete program

As you can see, the hashes obtained are correct because they are identical to those calculated by VirusTotal (Tab : Additional information)

Download the source