From 4c1578f9aab9969dd50ec16b7ae2e0a0652e9cb1 Mon Sep 17 00:00:00 2001 From: tangge233 Date: Sun, 22 Feb 2026 22:41:30 +0800 Subject: [PATCH 1/2] =?UTF-8?q?fix(hash):=20=E4=BF=AE=E5=A4=8D=E5=93=88?= =?UTF-8?q?=E5=B8=8C=E5=A4=84=E7=90=86=E9=83=A8=E5=88=86=E8=B5=84=E6=BA=90?= =?UTF-8?q?=E5=A4=84=E7=90=86=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Modules/Base/ModBase.vb | 93 ++++++++++--------- 1 file changed, 50 insertions(+), 43 deletions(-) diff --git a/Plain Craft Launcher 2/Modules/Base/ModBase.vb b/Plain Craft Launcher 2/Modules/Base/ModBase.vb index 1e8106d..035eafb 100644 --- a/Plain Craft Launcher 2/Modules/Base/ModBase.vb +++ b/Plain Craft Launcher 2/Modules/Base/ModBase.vb @@ -1075,15 +1075,16 @@ Public Module ModBase Re: Try '获取 MD5 - Dim Result As New StringBuilder() Dim File As New FileStream(FilePath, FileMode.Open, FileAccess.Read, FileShare.ReadWrite) - Dim MD5 As MD5 = New MD5CryptoServiceProvider() - Dim Retval As Byte() = MD5.ComputeHash(File) - File.Close() - For i = 0 To Retval.Length - 1 - Result.Append(Retval(i).ToString("x2")) - Next - Return Result.ToString + Using h = MD5Cng.Create() + Dim Retval As Byte() = h.ComputeHash(File) + Dim Result As New StringBuilder(Retval.Length * 2) + For i = 0 To Retval.Length - 1 + Result.Append(Retval(i).ToString("x2")) + Next + + Return Result.ToString + End Using Catch ex As Exception If Retry OrElse TypeOf ex Is FileNotFoundException Then Log(ex, "获取文件 MD5 失败:" & FilePath) @@ -1106,15 +1107,17 @@ Re: ''检测该文件是否在下载中,若在下载则放弃检测 'If IgnoreOnDownloading AndAlso NetManage.Files.ContainsKey(FilePath) AndAlso NetManage.Files(FilePath).State <= NetState.Merge Then Return "" '获取 SHA512 - Dim file As New FileStream(FilePath, FileMode.Open, FileAccess.Read, FileShare.ReadWrite) - Dim sha512 As SHA512 = New SHA512CryptoServiceProvider() - Dim retval As Byte() = sha512.ComputeHash(file) - file.Close() - Dim Result As New StringBuilder() - For i As Integer = 0 To retval.Length - 1 - Result.Append(retval(i).ToString("x2")) - Next - Return Result.ToString + Using file As New FileStream(FilePath, FileMode.Open, FileAccess.Read, FileShare.ReadWrite) + Using h = SHA512Cng.Create() + Dim retval As Byte() = h.ComputeHash(file) + Dim Result As New StringBuilder(retval.Length * 2) + For i As Integer = 0 To retval.Length - 1 + Result.Append(retval(i).ToString("x2")) + Next + + Return Result.ToString + End Using + End Using Catch ex As Exception If Retry OrElse TypeOf ex Is FileNotFoundException Then Log(ex, "获取文件 SHA512 失败:" & FilePath) @@ -1137,15 +1140,17 @@ Re: ''检测该文件是否在下载中,若在下载则放弃检测 'If IgnoreOnDownloading AndAlso NetManage.Files.ContainsKey(FilePath) AndAlso NetManage.Files(FilePath).State <= NetState.Merge Then Return "" '获取 SHA256 - Dim file As New FileStream(FilePath, FileMode.Open, FileAccess.Read, FileShare.ReadWrite) - Dim sha256 As SHA256 = New SHA256CryptoServiceProvider() - Dim retval As Byte() = sha256.ComputeHash(file) - file.Close() - Dim Result As New StringBuilder() - For i As Integer = 0 To retval.Length - 1 - Result.Append(retval(i).ToString("x2")) - Next - Return Result.ToString + Using file As New FileStream(FilePath, FileMode.Open, FileAccess.Read, FileShare.ReadWrite) + Using h = SHA256Cng.Create() + Dim retval As Byte() = h.ComputeHash(file) + Dim Result As New StringBuilder(retval.Length * 2) + For i As Integer = 0 To retval.Length - 1 + Result.Append(retval(i).ToString("x2")) + Next + + Return Result.ToString() + End Using + End Using Catch ex As Exception If Retry OrElse TypeOf ex Is FileNotFoundException Then Log(ex, "获取文件 SHA256 失败:" & FilePath) @@ -1166,15 +1171,17 @@ Re: Re: Try '获取 SHA1 - Dim file As New FileStream(FilePath, FileMode.Open, FileAccess.Read, FileShare.ReadWrite) - Dim sha1 As SHA1 = New SHA1CryptoServiceProvider() - Dim retval As Byte() = sha1.ComputeHash(file) - file.Close() - Dim Result As New StringBuilder() - For i As Integer = 0 To retval.Length - 1 - Result.Append(retval(i).ToString("x2")) - Next - Return Result.ToString + Using file As New FileStream(FilePath, FileMode.Open, FileAccess.Read, FileShare.ReadWrite) + Using h = SHA1Cng.Create() + Dim retval As Byte() = h.ComputeHash(file) + Dim Result As New StringBuilder(retval.Length * 2) + For i As Integer = 0 To retval.Length - 1 + Result.Append(retval(i).ToString("x2")) + Next + + Return Result.ToString + End Using + End Using Catch ex As Exception If Retry OrElse TypeOf ex Is FileNotFoundException Then Log(ex, "获取文件 SHA1 失败:" & FilePath) @@ -1647,14 +1654,14 @@ RetryDir: ''' 获取字符串 MD5。 ''' Public Function GetStringMD5(Str As String) As String - Dim md5Hasher As New MD5CryptoServiceProvider - Dim hashedDataBytes As Byte() - hashedDataBytes = md5Hasher.ComputeHash(Encoding.GetEncoding("gb2312").GetBytes(Str)) - Dim tmp As New StringBuilder() - For Each i As Byte In hashedDataBytes - tmp.Append(i.ToString("x2")) - Next - Return tmp.ToString() + Using h = MD5Cng.Create() + Dim hashedDataBytes = h.ComputeHash(Encoding.GetEncoding("gb2312").GetBytes(Str)) + Dim tmp As New StringBuilder(hashedDataBytes.Length * 2) + For Each i As Byte In hashedDataBytes + tmp.Append(i.ToString("x2")) + Next + Return tmp.ToString() + End Using End Function ''' ''' 检查字符串中的字符是否均为 ASCII 字符。 From dc7d6b3e9180af1520d74a740574177f01d957f8 Mon Sep 17 00:00:00 2001 From: tangge233 Date: Sun, 22 Feb 2026 22:47:54 +0800 Subject: [PATCH 2/2] =?UTF-8?q?chore:=20=E6=BC=8F=E4=BA=86=E4=B8=80?= =?UTF-8?q?=E4=B8=AA?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Plain Craft Launcher 2/Modules/Base/ModBase.vb | 17 +++++++++-------- 1 file changed, 9 insertions(+), 8 deletions(-) diff --git a/Plain Craft Launcher 2/Modules/Base/ModBase.vb b/Plain Craft Launcher 2/Modules/Base/ModBase.vb index 035eafb..e279584 100644 --- a/Plain Craft Launcher 2/Modules/Base/ModBase.vb +++ b/Plain Craft Launcher 2/Modules/Base/ModBase.vb @@ -1075,15 +1075,16 @@ Public Module ModBase Re: Try '获取 MD5 - Dim File As New FileStream(FilePath, FileMode.Open, FileAccess.Read, FileShare.ReadWrite) - Using h = MD5Cng.Create() - Dim Retval As Byte() = h.ComputeHash(File) - Dim Result As New StringBuilder(Retval.Length * 2) - For i = 0 To Retval.Length - 1 - Result.Append(Retval(i).ToString("x2")) - Next + Using File As New FileStream(FilePath, FileMode.Open, FileAccess.Read, FileShare.ReadWrite) + Using h = MD5Cng.Create() + Dim Retval As Byte() = h.ComputeHash(File) + Dim Result As New StringBuilder(Retval.Length * 2) + For i = 0 To Retval.Length - 1 + Result.Append(Retval(i).ToString("x2")) + Next - Return Result.ToString + Return Result.ToString + End Using End Using Catch ex As Exception If Retry OrElse TypeOf ex Is FileNotFoundException Then