Checksum sha1 ou md5 d'un fichier

Contenu du snippet

Ces 2 fonctions servent simplement à obtenir la somme de controle SHA1 ou MD5 d'un fichier sans aucune API, exe externe ou autre

Cette source est une traduction VB.net de la source de CrazyHT (http://www.csharpfr.com/codes/SOMME-MD5-FICHIER_11517.aspx)

Source / Exemple :


Public Function GetFileMD5CheckSum(ByVal fichier As String) As String
        If IO.File.Exists(fichier) Then
            Dim st As System.IO.FileStream = Nothing
            Try
                Dim check As New System.Security.Cryptography.MD5CryptoServiceProvider
                st = System.IO.File.Open(fichier, System.IO.FileMode.Open, System.IO.FileAccess.Read)
                Dim somme As Byte() = check.ComputeHash(st)
                Dim ret As String = ""
                For Each a As Byte In somme
                    If (a < 16) Then
                        ret += "0" + a.ToString("X")
                    Else
                        ret += a.ToString("X")
                    End If
                Next
                Return ret
            Catch ex As Exception
                Exit Try
            Finally
                If st IsNot Nothing Then st.Close()
            End Try
        Else
            Return ""
        End If
        Return ""
    End Function

    Public Function GetFileSHA1CheckSum(ByVal fichier As String) As String
        If IO.File.Exists(fichier) Then
            Dim st As System.IO.FileStream = Nothing
            Try
                Dim check As New System.Security.Cryptography.SHA1CryptoServiceProvider
                st = System.IO.File.Open(fichier, System.IO.FileMode.Open, System.IO.FileAccess.Read)
                Dim somme As Byte() = check.ComputeHash(st)
                Dim ret As String = ""
                For Each a As Byte In somme
                    If (a < 16) Then
                        ret += "0" + a.ToString("X")
                    Else
                        ret += a.ToString("X")
                    End If
                Next
                Return ret
            Catch ex As Exception
                Exit Try
            Finally
                If st IsNot Nothing Then st.Close()
            End Try
        Else
            Return ""
        End If
        Return ""
    End Function

A voir également

Vous n'êtes pas encore membre ?

inscrivez-vous, c'est gratuit et ça prend moins d'une minute !

Les membres obtiennent plus de réponses que les utilisateurs anonymes.

Le fait d'être membre vous permet d'avoir un suivi détaillé de vos demandes et codes sources.

Le fait d'être membre vous permet d'avoir des options supplémentaires.