Merge pull request #8206 from tangge233/fix/hash

fix(hash): 修复哈希处理部分资源处理问题
This commit is contained in:
龙腾猫跃
2026-02-28 20:34:46 +08:00
committed by GitHub
+52 -44
View File
@@ -1075,15 +1075,17 @@ Public Module ModBase
Re: Re:
Try Try
'获取 MD5 '获取 MD5
Dim Result As New StringBuilder() Using File As New FileStream(FilePath, FileMode.Open, FileAccess.Read, FileShare.ReadWrite)
Dim File As New FileStream(FilePath, FileMode.Open, FileAccess.Read, FileShare.ReadWrite) Using h = MD5Cng.Create()
Dim MD5 As MD5 = New MD5CryptoServiceProvider() Dim Retval As Byte() = h.ComputeHash(File)
Dim Retval As Byte() = MD5.ComputeHash(File) Dim Result As New StringBuilder(Retval.Length * 2)
File.Close() For i = 0 To Retval.Length - 1
For i = 0 To Retval.Length - 1 Result.Append(Retval(i).ToString("x2"))
Result.Append(Retval(i).ToString("x2")) Next
Next
Return Result.ToString Return Result.ToString
End Using
End Using
Catch ex As Exception Catch ex As Exception
If Retry OrElse TypeOf ex Is FileNotFoundException Then If Retry OrElse TypeOf ex Is FileNotFoundException Then
Log(ex, "获取文件 MD5 失败:" & FilePath) Log(ex, "获取文件 MD5 失败:" & FilePath)
@@ -1106,15 +1108,17 @@ Re:
''检测该文件是否在下载中,若在下载则放弃检测 ''检测该文件是否在下载中,若在下载则放弃检测
'If IgnoreOnDownloading AndAlso NetManage.Files.ContainsKey(FilePath) AndAlso NetManage.Files(FilePath).State <= NetState.Merge Then Return "" 'If IgnoreOnDownloading AndAlso NetManage.Files.ContainsKey(FilePath) AndAlso NetManage.Files(FilePath).State <= NetState.Merge Then Return ""
'获取 SHA512 '获取 SHA512
Dim file As New FileStream(FilePath, FileMode.Open, FileAccess.Read, FileShare.ReadWrite) Using file As New FileStream(FilePath, FileMode.Open, FileAccess.Read, FileShare.ReadWrite)
Dim sha512 As SHA512 = New SHA512CryptoServiceProvider() Using h = SHA512Cng.Create()
Dim retval As Byte() = sha512.ComputeHash(file) Dim retval As Byte() = h.ComputeHash(file)
file.Close() Dim Result As New StringBuilder(retval.Length * 2)
Dim Result As New StringBuilder() For i As Integer = 0 To retval.Length - 1
For i As Integer = 0 To retval.Length - 1 Result.Append(retval(i).ToString("x2"))
Result.Append(retval(i).ToString("x2")) Next
Next
Return Result.ToString Return Result.ToString
End Using
End Using
Catch ex As Exception Catch ex As Exception
If Retry OrElse TypeOf ex Is FileNotFoundException Then If Retry OrElse TypeOf ex Is FileNotFoundException Then
Log(ex, "获取文件 SHA512 失败:" & FilePath) Log(ex, "获取文件 SHA512 失败:" & FilePath)
@@ -1137,15 +1141,17 @@ Re:
''检测该文件是否在下载中,若在下载则放弃检测 ''检测该文件是否在下载中,若在下载则放弃检测
'If IgnoreOnDownloading AndAlso NetManage.Files.ContainsKey(FilePath) AndAlso NetManage.Files(FilePath).State <= NetState.Merge Then Return "" 'If IgnoreOnDownloading AndAlso NetManage.Files.ContainsKey(FilePath) AndAlso NetManage.Files(FilePath).State <= NetState.Merge Then Return ""
'获取 SHA256 '获取 SHA256
Dim file As New FileStream(FilePath, FileMode.Open, FileAccess.Read, FileShare.ReadWrite) Using file As New FileStream(FilePath, FileMode.Open, FileAccess.Read, FileShare.ReadWrite)
Dim sha256 As SHA256 = New SHA256CryptoServiceProvider() Using h = SHA256Cng.Create()
Dim retval As Byte() = sha256.ComputeHash(file) Dim retval As Byte() = h.ComputeHash(file)
file.Close() Dim Result As New StringBuilder(retval.Length * 2)
Dim Result As New StringBuilder() For i As Integer = 0 To retval.Length - 1
For i As Integer = 0 To retval.Length - 1 Result.Append(retval(i).ToString("x2"))
Result.Append(retval(i).ToString("x2")) Next
Next
Return Result.ToString Return Result.ToString()
End Using
End Using
Catch ex As Exception Catch ex As Exception
If Retry OrElse TypeOf ex Is FileNotFoundException Then If Retry OrElse TypeOf ex Is FileNotFoundException Then
Log(ex, "获取文件 SHA256 失败:" & FilePath) Log(ex, "获取文件 SHA256 失败:" & FilePath)
@@ -1166,15 +1172,17 @@ Re:
Re: Re:
Try Try
'获取 SHA1 '获取 SHA1
Dim file As New FileStream(FilePath, FileMode.Open, FileAccess.Read, FileShare.ReadWrite) Using file As New FileStream(FilePath, FileMode.Open, FileAccess.Read, FileShare.ReadWrite)
Dim sha1 As SHA1 = New SHA1CryptoServiceProvider() Using h = SHA1Cng.Create()
Dim retval As Byte() = sha1.ComputeHash(file) Dim retval As Byte() = h.ComputeHash(file)
file.Close() Dim Result As New StringBuilder(retval.Length * 2)
Dim Result As New StringBuilder() For i As Integer = 0 To retval.Length - 1
For i As Integer = 0 To retval.Length - 1 Result.Append(retval(i).ToString("x2"))
Result.Append(retval(i).ToString("x2")) Next
Next
Return Result.ToString Return Result.ToString
End Using
End Using
Catch ex As Exception Catch ex As Exception
If Retry OrElse TypeOf ex Is FileNotFoundException Then If Retry OrElse TypeOf ex Is FileNotFoundException Then
Log(ex, "获取文件 SHA1 失败:" & FilePath) Log(ex, "获取文件 SHA1 失败:" & FilePath)
@@ -1647,14 +1655,14 @@ RetryDir:
''' 获取字符串 MD5。 ''' 获取字符串 MD5。
''' </summary> ''' </summary>
Public Function GetStringMD5(Str As String) As String Public Function GetStringMD5(Str As String) As String
Dim md5Hasher As New MD5CryptoServiceProvider Using h = MD5Cng.Create()
Dim hashedDataBytes As Byte() Dim hashedDataBytes = h.ComputeHash(Encoding.GetEncoding("gb2312").GetBytes(Str))
hashedDataBytes = md5Hasher.ComputeHash(Encoding.GetEncoding("gb2312").GetBytes(Str)) Dim tmp As New StringBuilder(hashedDataBytes.Length * 2)
Dim tmp As New StringBuilder() For Each i As Byte In hashedDataBytes
For Each i As Byte In hashedDataBytes tmp.Append(i.ToString("x2"))
tmp.Append(i.ToString("x2")) Next
Next Return tmp.ToString()
Return tmp.ToString() End Using
End Function End Function
''' <summary> ''' <summary>
''' 检查字符串中的字符是否均为 ASCII 字符。 ''' 检查字符串中的字符是否均为 ASCII 字符。