Skip to content

Commit

Permalink
获取子文件夹 .jar 文件的 md5
Browse files Browse the repository at this point in the history
  • Loading branch information
YukiCoco committed Jan 17, 2020
1 parent e23a89a commit 48cd08a
Show file tree
Hide file tree
Showing 4 changed files with 178 additions and 23 deletions.
101 changes: 80 additions & 21 deletions src/GetMd5/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,36 +14,95 @@ static void Main(string[] args)
{
Console.WriteLine("输入 mods 文件夹");
string directory = Console.ReadLine();
string md5 = GetDirectoryMd5(directory);
CheckMd5 checkMd5 = new CheckMd5(directory);
string md5 = checkMd5.GetDirectoryMd5(".jar");
string[] filesPath = Directory.GetFiles(directory);
File.WriteAllText("md5.txt", md5);
Console.WriteLine("已在目录创建 md5.txt ,按任意键退出");
Console.ReadKey();
}

/// <summary>
/// 获取文件夹下所有文件的 md5
/// </summary>
/// <param name="dirPath"></param>
/// <returns></returns>
public static string GetDirectoryMd5(string dirPath)
public class CheckMd5
{
string[] filesPath = Directory.GetFiles(dirPath);
List<string> filesMd5 = new List<string>();
for (int i = 0; i < filesPath.Length; i++)
private List<string> Files = new List<string>();
private string DirPath { get; set; }

public CheckMd5(string dirPath)
{
DirPath = dirPath;
}

/// <summary>
/// 获取文件夹下所有文件的 md5
/// </summary>
/// <returns></returns>
public string GetDirectoryMd5()
{
listDirectory(DirPath);
string[] filesPath = Files.ToArray();
List<string> filesMd5 = new List<string>();
for (int i = 0; i < filesPath.Length; i++)
{
//这里引用了作者在 MD5Checker 类里的方法
//获取 md5
FileStream file = new FileStream(filesPath[i], FileMode.Open);
MD5 md5 = new MD5CryptoServiceProvider();//创建SHA1对象
byte[] md5Bytes = md5.ComputeHash(file);//Hash运算
md5.Dispose();//释放当前实例使用的所有资源
file.Dispose();
string result = BitConverter.ToString(md5Bytes);//将运算结果转为string类型
result = result.Replace("-", "");
filesMd5.Add(result);
}
return String.Join(",", filesMd5.ToArray());
}

/// <summary>
/// 获取文件夹下所有文件的 md5
/// </summary>
/// <param name="extension">扩展名</param>
/// <returns></returns>
public string GetDirectoryMd5(string extension)
{
listDirectory(DirPath);
string[] filesPath = Files.ToArray();
List<string> filesMd5 = new List<string>();
for (int i = 0; i < filesPath.Length; i++)
{
if(Path.GetExtension(filesPath[i]) == extension)
{
//这里引用了作者在 MD5Checker 类里的方法
//获取 md5
FileStream file = new FileStream(filesPath[i], FileMode.Open);
MD5 md5 = new MD5CryptoServiceProvider();//创建SHA1对象
byte[] md5Bytes = md5.ComputeHash(file);//Hash运算
md5.Dispose();//释放当前实例使用的所有资源
file.Dispose();
string result = BitConverter.ToString(md5Bytes);//将运算结果转为string类型
result = result.Replace("-", "");
filesMd5.Add(result);
}
}
return String.Join(",", filesMd5.ToArray());
}
/// <summary>
/// 获取文件夹下所有文件
/// </summary>
/// <param name="path"></param>
public void listDirectory(string path)
{
//这里引用了作者在 MD5Checker 类里的方法
//获取 md5
FileStream file = new FileStream(filesPath[i], FileMode.Open);
MD5 md5 = new MD5CryptoServiceProvider();//创建SHA1对象
byte[] md5Bytes = md5.ComputeHash(file);//Hash运算
md5.Dispose();//释放当前实例使用的所有资源
file.Dispose();
string result = BitConverter.ToString(md5Bytes);//将运算结果转为string类型
result = result.Replace("-", "");
filesMd5.Add(result);
DirectoryInfo theFolder = new DirectoryInfo(@path);
//遍历文件
foreach (FileInfo file in theFolder.GetFiles())
{
Files.Add(file.FullName);
}
//遍历文件夹
foreach (DirectoryInfo folder in theFolder.GetDirectories())
{
listDirectory(folder.FullName);
}
}
return String.Join(",", filesMd5.ToArray());
}
}
}
6 changes: 4 additions & 2 deletions src/NsisoLauncherCore/Auth/AuthlibInjectorAuthenticator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,8 @@ public override async Task<AuthenticateResult> DoAuthenticateAsync()
if (CheckMd5)
{
string modsDir = Path.Combine(System.AppDomain.CurrentDomain.SetupInformation.ApplicationBase, ".minecraft", "mods");
string modsMd5 = FileHelper.GetDirectoryMd5(modsDir);
Util.AntiCheat.Md5Reader md5Reader = new Util.AntiCheat.Md5Reader(modsDir);
string modsMd5 = md5Reader.GetDirectoryMd5(".jar");
authenticate.Md5List = modsMd5;
}
if (ProxyAuthServerAddress != null)
Expand Down Expand Up @@ -93,7 +94,8 @@ public override async Task<AuthenticateResult> DoAuthenticateAsync()
if (CheckMd5)
{
string modsDir = Path.Combine(System.AppDomain.CurrentDomain.SetupInformation.ApplicationBase, ".minecraft", "mods");
string modsMd5 = FileHelper.GetDirectoryMd5(modsDir);
Util.AntiCheat.Md5Reader md5Reader = new Util.AntiCheat.Md5Reader(modsDir);
string modsMd5 = md5Reader.GetDirectoryMd5(".jar");
validate.Md5List = modsMd5;
}
if (ProxyAuthServerAddress != null)
Expand Down
1 change: 1 addition & 0 deletions src/NsisoLauncherCore/NsisoLauncherCore.projitems
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,7 @@
<Compile Include="$(MSBuildThisFileDirectory)Net\Server\ServerInfo.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Net\Tools\GetDownloadUrl.cs" />
<Compile Include="$(MSBuildThisFileDirectory)PathManager.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Util\AntiCheat\Md5Reader.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Util\AssetsReader.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Util\Checker\IChecker.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Util\Checker\MD5Checker.cs" />
Expand Down
93 changes: 93 additions & 0 deletions src/NsisoLauncherCore/Util/AntiCheat/Md5Reader.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Security.Cryptography;
using System.Text;

namespace NsisoLauncherCore.Util.AntiCheat
{
public class Md5Reader
{
private List<string> Files = new List<string>();
private string DirPath { get; set; }

public Md5Reader(string dirPath)
{
DirPath = dirPath;
}

/// <summary>
/// 获取文件夹下所有文件的 md5
/// </summary>
/// <param name="dirPath"></param>
/// <returns></returns>
public string GetDirectoryMd5()
{
listDirectory(DirPath);
string[] filesPath = Files.ToArray();
List<string> filesMd5 = new List<string>();
for (int i = 0; i < filesPath.Length; i++)
{
//这里引用了作者在 MD5Checker 类里的方法
//获取 md5
FileStream file = new FileStream(filesPath[i], FileMode.Open);
MD5 md5 = new MD5CryptoServiceProvider();//创建SHA1对象
byte[] md5Bytes = md5.ComputeHash(file);//Hash运算
md5.Dispose();//释放当前实例使用的所有资源
file.Dispose();
string result = BitConverter.ToString(md5Bytes);//将运算结果转为string类型
result = result.Replace("-", "");
filesMd5.Add(result);
}
return String.Join(",", filesMd5.ToArray());
}

/// <summary>
/// 获取文件夹下所有文件的 md5
/// </summary>
/// <param name="extension">扩展名</param>
/// <returns></returns>
public string GetDirectoryMd5(string extension)
{
listDirectory(DirPath);
string[] filesPath = Files.ToArray();
List<string> filesMd5 = new List<string>();
for (int i = 0; i < filesPath.Length; i++)
{
if (Path.GetExtension(filesPath[i]) == extension)
{
//这里引用了作者在 MD5Checker 类里的方法
//获取 md5
FileStream file = new FileStream(filesPath[i], FileMode.Open);
MD5 md5 = new MD5CryptoServiceProvider();//创建SHA1对象
byte[] md5Bytes = md5.ComputeHash(file);//Hash运算
md5.Dispose();//释放当前实例使用的所有资源
file.Dispose();
string result = BitConverter.ToString(md5Bytes);//将运算结果转为string类型
result = result.Replace("-", "");
filesMd5.Add(result);
}
}
return String.Join(",", filesMd5.ToArray());
}

/// <summary>
/// 获取文件夹下所有文件
/// </summary>
/// <param name="path"></param>
public void listDirectory(string path)
{
DirectoryInfo theFolder = new DirectoryInfo(@path);
//遍历文件
foreach (FileInfo file in theFolder.GetFiles())
{
Files.Add(file.FullName);
}
//遍历文件夹
foreach (DirectoryInfo folder in theFolder.GetDirectories())
{
listDirectory(folder.FullName);
}
}
}
}

0 comments on commit 48cd08a

Please sign in to comment.