Creating hash for folder

后端 未结 7 1148
梦谈多话
梦谈多话 2020-12-13 05:05

i need to create hash for folder, that contains some files. I already done this task for each of files, but i searching the way to create one hash for all files in folder.

7条回答
  •  不思量自难忘°
    2020-12-13 05:46

    Quick and Dirty folder hash that does not go down to suborders or read binary data. It is based on file and sub-folder names.

    Public Function GetFolderHash(ByVal sFolder As String) As String
        Dim oFiles As List(Of String) = IO.Directory.GetFiles(sFolder).OrderBy(Function(x) x.Count).ToList()
        Dim oFolders As List(Of String) = IO.Directory.GetDirectories(sFolder).OrderBy(Function(x) x.Count).ToList()
        oFiles.AddRange(oFolders)
    
        If oFiles.Count = 0 Then
            Return ""
        End If
    
        Dim oDM5 As System.Security.Cryptography.MD5 = System.Security.Cryptography.MD5.Create()
        For i As Integer = 0 To oFiles.Count - 1
            Dim sFile As String = oFiles(i)
            Dim sRelativePath As String = sFile.Substring(sFolder.Length + 1)
            Dim oPathBytes As Byte() = System.Text.Encoding.UTF8.GetBytes(sRelativePath.ToLower())
    
            If i = oFiles.Count - 1 Then
                oDM5.TransformFinalBlock(oPathBytes, 0, oPathBytes.Length)
            Else
                oDM5.TransformBlock(oPathBytes, 0, oPathBytes.Length, oPathBytes, 0)
            End If
        Next
    
        Return BitConverter.ToString(oDM5.Hash).Replace("-", "").ToLower()
    End Function
    

提交回复
热议问题