2011年6月22日 星期三

File MD5 Checksum

    Public Shared Function file_checksum(ByVal file_path As String) As String
        Dim md5 As MD5CryptoServiceProvider = New MD5CryptoServiceProvider
        Dim fs As FileStream = New FileStream(file_path, FileMode.Open, FileAccess.Read, FileShare.Read)
        md5.ComputeHash(fs)

        Dim hash As Byte() = md5.Hash
        Dim buff As StringBuilder = New StringBuilder
        Dim hashByte As Byte
        For Each hashByte In hash
            buff.Append(String.Format("{0:X2}", hashByte))
        Next
        fs.Close()
        Return buff.ToString.Trim
    End Function

沒有留言: