Skip to content

Commit

Permalink
Release 1.0.3
Browse files Browse the repository at this point in the history
  • Loading branch information
SALTWOOD committed Mar 9, 2024
1 parent e61ce64 commit 71a2820
Show file tree
Hide file tree
Showing 15 changed files with 592 additions and 63 deletions.
8 changes: 4 additions & 4 deletions Data/BinaryTree.cs
Original file line number Diff line number Diff line change
Expand Up @@ -35,18 +35,18 @@ public BinaryTree(T data)
// 插入数据
public void Insert(T data)
{
// 如果没有数据则直接设置数据
// 如果没有数据,则直接设置数据
if (!this.data.HasItem)
{
this.data.SetData(data);
return;
}
// 如果数据不为空则比较数据和当前数据
// 如果数据不为空,则比较数据和当前数据
if (this.data.Value != null)
{
if (this.data.Value.CompareTo(data) > 0)
{
// 如果左子树不为空则插入
// 如果左子树不为空,则插入
if (this.left == null)
{
this.left = new BinaryTree<T>(data);
Expand All @@ -58,7 +58,7 @@ public void Insert(T data)
}
else
{
// 如果右子树不为空则插入
// 如果右子树不为空,则插入
if (this.right == null)
{
this.right = new BinaryTree<T>(data);
Expand Down
24 changes: 12 additions & 12 deletions Data/ErrorCorrector.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ namespace TeraIO.Data
{
/// <summary>
/// This class is not recommended to use because it consumes too much memory when validating and generating big data
/// 这个类不建议使用因为其在验证、生成大数据时占用内存过大
/// 这个类不建议使用,因为其在验证、生成大数据时占用内存过大
/// This class is not recommended to use because it consumes too much memory when validating and generating big data
/// </summary>
public class ErrorCorrector
Expand All @@ -27,7 +27,7 @@ public BitArray VerificationCode
}

/// <summary>
/// 构造函数接收一个 IList<bool> 作为输入数据
/// 构造函数,接收一个 IList<bool> 作为输入数据
/// </summary>
public ErrorCorrector(IList<bool> data)
{
Expand All @@ -36,7 +36,7 @@ public ErrorCorrector(IList<bool> data)
}

/// <summary>
/// 构造函数接收一个 IList<byte> 作为输入数据
/// 构造函数,接收一个 IList<byte> 作为输入数据
/// </summary>
public ErrorCorrector(IList<byte> data)
{
Expand All @@ -45,7 +45,7 @@ public ErrorCorrector(IList<byte> data)
}

/// <summary>
/// 构造函数接收一个 BitArray 作为输入数据
/// 构造函数,接收一个 BitArray 作为输入数据
/// </summary>
public ErrorCorrector(BitArray data)
{
Expand Down Expand Up @@ -201,43 +201,43 @@ private List<bool> CompleteList(IList<bool> data)

public override string ToString()
{
// 创建一个空的字符串列表用于存储每6个bool值转换成的二进制字符串
// 创建一个空的字符串列表,用于存储每6个bool值转换成的二进制字符串
List<string> binaryStrings = new List<string>();

// 遍历verificationCode列表中的每个bool值
for (int i = 0; i < verificationCode.Count; i++)
{/*
// 如果当前索引是6的倍数则在字符串列表中添加一个空格
// 如果当前索引是6的倍数,则在字符串列表中添加一个空格
if (i > 0 && i % 6 == 0)
{
binaryStrings.Add(" ");
}*/

// 将当前bool值转换为二进制字符串并添加到字符串列表中
// 将当前bool值转换为二进制字符串,并添加到字符串列表中
binaryStrings.Add(verificationCode[i] ? "1" : "0");
}

// 将字符串列表中的所有字符串连接在一起形成一个完整的字符串
// 将字符串列表中的所有字符串连接在一起,形成一个完整的字符串
string result = string.Join("", binaryStrings);

// 计算剩余的字符数量并使用空字符串填充
// 计算剩余的字符数量,并使用空字符串填充
int remainder = result.Length % 6;
if (remainder > 0)
{
result += new string('0', 6 - remainder);
}

// 创建一个新的字符列表用于存储转换后的Base64编码字符
// 创建一个新的字符列表,用于存储转换后的Base64编码字符
List<char> chars = new List<char>();

// 遍历转换后的字符串将其转换为Base64编码字符并添加到字符列表中
// 遍历转换后的字符串,将其转换为Base64编码字符,并添加到字符列表中
for (int i = 0; i < result.Length; i += 6)
{
int value = Convert.ToInt32(result.Substring(i, 6), 2);
chars.Add((char)('A' + value));
}

// 将字符列表中的所有字符连接在一起形成一个完整的Base64编码字符串
// 将字符列表中的所有字符连接在一起,形成一个完整的Base64编码字符串
return new string(chars.ToArray());
}
}
Expand Down
203 changes: 203 additions & 0 deletions Data/MD5.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,203 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace TeraIO.Data
{
public class MD5
{
public const uint hashValue1 = 0x67452301;
public const uint hashValue2 = 0xEFCDAB89;
public const uint hashValue3 = 0x98BACDFE;
public const uint hashValue4 = 0x10325476;

public static readonly uint[] constK = {
0xd76aa478, 0xf57c0faf, 0xe8c7b756, 0x4787c62a,
0x698098d8, 0x8b44f7af, 0x242070db, 0xc1bdceee,
0xa8304613, 0xfd469501, 0xffff5bb1, 0x895cd7be,
0x6b901122, 0x49b40821, 0x265e5a51, 0xe9b6c7aa,
0xfd987193, 0xa679438e, 0xf61e2562, 0xc040b340,
0xd8a1e681, 0xe7d3fbc8, 0xd62f105d, 0x02441453,
0x21e1cde6, 0xc33707d6, 0xf4d50d87, 0x455a14ed,
0xa9e3e905, 0xfcefa3f8, 0x676f02d9, 0x8d2a4c8a,
0x6d9d6122, 0xfde5380c, 0xfffa3942, 0x8771f681,
0xa4beea44, 0x4bdecfa9, 0xf6bb4b60, 0xbebfbc70,
0x289b7ec6, 0xeaa127fa, 0xd4ef3085, 0x04881d05,
0xd9d4d039, 0xe6db99e5, 0x1fa27cf8, 0xc4ac5665,
0xf4292244, 0x432aff97, 0xfc93a039, 0xab9423a7,
0x655b59c3, 0x6fa87e4f, 0xf7537e82, 0xbd3af235,
0x2ad7d2bb, 0xeb86d391, 0xffeff47d, 0x8f0ccc92,
0x85845dd1, 0xfe2ce6e0, 0xa3014314, 0x4e0811a1
};

public static readonly int[] constS =
{
7, 12, 17, 22, 7, 12, 17, 22, 7, 12, 17, 22, 7, 12, 17, 22,
5, 9, 14, 20, 5, 9, 14, 20, 5, 9, 14, 20, 5, 9, 14, 20,
4, 11, 16, 23, 4, 11, 16, 23, 4, 11, 16, 23, 4, 11, 16, 23,
6, 10, 15, 21, 6, 10, 15, 21, 6, 10, 15, 21, 6, 10, 15, 21
};

public static byte[] Pad(byte[] data)
{
byte[] result = new byte[GetLength(data) * 64];
data.CopyTo(result, 0);
result[data.LongLength] = 0x80;
GetByteArrayFromLong(data.LongLength).CopyTo(result, result.LongLength - 8);
return result;
}

public static byte[] ComputeMD5(byte[] data)
{
byte[] paddedData = Pad(data);
var hashSet = (
hashValue1,
hashValue2,
hashValue3,
hashValue4
);
var (a, b, c, d) = hashSet;
byte[] last = new byte[4];
for (int i = 0; i < paddedData.Length; i += 64)
{
last = Compute64(paddedData[i..(i + 64)],
ref hashSet,
hashSet
);
}
return last;
}

public static byte[] Compute64
(byte[] data,
ref (uint, uint, uint, uint) endValues,
(uint, uint, uint, uint) startValues = default
)
{
uint a, b, c, d;
if (startValues == default)
{
a = hashValue1;
b = hashValue2;
c = hashValue3;
d = hashValue4;
}
else
(a, b, c, d) = startValues;

for (int i = 0; i < 64; i++)
{
uint f;
uint g;

if (0 <= i && i <= 15)
{
int index = i * 4;
f = (uint)((b & c) | ((-b) & d));
g = ByteArrayToUInt32LittleEndian(data[index..(index + 4)]);
}
else if (16 <= i && i <= 31)
{
int index = (5 * i + 1) % 16;
f = (uint)((d & b) | ((-d) & c));
g = ByteArrayToUInt32LittleEndian(data[index..(index + 4)]);
}
else if (32 <= i && i <= 47)
{
int index = (3 * i + 5) % 16;
f = b ^ c ^ d;
g = ByteArrayToUInt32LittleEndian(data[index..(index + 4)]);
}
else
{
int index = (7 * i) % 16;
f = (uint)(c ^ (b | (-d)));
g = ByteArrayToUInt32LittleEndian(data[index..(index + 4)]);
}
a = ((a + f + constK[i] + g) << constS[i]) + b;

(b, c, d, a) = (a, b, c, d);
}

endValues = (a, b, c, d);

byte[] result = new byte[16];

UInt32ToByteArray(hashValue1 + a).CopyTo(result, 0);
UInt32ToByteArray(hashValue2 + b).CopyTo(result, 4);
UInt32ToByteArray(hashValue3 + c).CopyTo(result, 8);
UInt32ToByteArray(hashValue4 + d).CopyTo(result, 12);

return result;
}

public static byte[] UInt32ToByteArray(uint value)
{
byte[] byteArray = new byte[4];
byteArray[3] = (byte)(value >> 24);
byteArray[2] = (byte)((value >> 16) & 0xFF);
byteArray[1] = (byte)((value >> 8) & 0xFF);
byteArray[0] = (byte)(value & 0xFF);
return byteArray;
}

public static uint ByteArrayToUInt32LittleEndian(byte[] bytes)
{
if (bytes == null || bytes.Length != 4)
{
throw new ArgumentException("Byte array must be 4 bytes long.");
}

return (uint)((bytes[0] << 24) |
(bytes[1] << 16) |
(bytes[2] << 8) |
bytes[3]);
}

public static byte[] GetByteArrayFromLong(long value)
{
byte[] byteArray = new byte[8];
byteArray[7] = (byte)((value >> 56) & 0xFF);
byteArray[6] = (byte)((value >> 48) & 0xFF);
byteArray[5] = (byte)((value >> 40) & 0xFF);
byteArray[4] = (byte)((value >> 32) & 0xFF);
byteArray[3] = (byte)((value >> 24) & 0xFF);
byteArray[2] = (byte)((value >> 16) & 0xFF);
byteArray[1] = (byte)((value >> 8) & 0xFF);
byteArray[0] = (byte)(value & 0xFF);
return byteArray;
}

public static void GetByteArrayFromLong(long value, byte[] byteArray)
{
byteArray[0] = (byte)(value >> 56);
byteArray[1] = (byte)(value >> 48);
byteArray[2] = (byte)(value >> 40);
byteArray[3] = (byte)(value >> 32);
byteArray[4] = (byte)(value >> 24);
byteArray[5] = (byte)(value >> 16);
byteArray[6] = (byte)(value >> 8);
byteArray[7] = (byte)value;
}

public static void GetByteArrayFromLong(long value, out byte[] byteArray)
{
byteArray = new byte[8];
byteArray[0] = (byte)(value >> 56);
byteArray[1] = (byte)(value >> 48);
byteArray[2] = (byte)(value >> 40);
byteArray[3] = (byte)(value >> 32);
byteArray[4] = (byte)(value >> 24);
byteArray[5] = (byte)(value >> 16);
byteArray[6] = (byte)(value >> 8);
byteArray[7] = (byte)value;
}

public static int GetLength(byte[] data)
{
return (int)(Math.Ceiling((data.Length + 9) / 64.0));
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,12 @@

namespace TeraIO.Network.Http
{
public class HttpHandlerFunction : Attribute
public class HttpHandlerAttribute : Attribute
{
public string Route { get; set; }
public List<string> Methods { get; set; }

public HttpHandlerFunction(string route, string methods = "GET")
public HttpHandlerAttribute(string route, string methods = "GET")
{
this.Route = route;
this.Methods = methods.Split(' ').ToList();
Expand Down
19 changes: 10 additions & 9 deletions Network/Http/HttpServer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,27 +7,28 @@

namespace TeraIO.Network.Http
{
[Obsolete("请直接使用 HttpServerBase.Start")]
public static class HttpServer
{
/// <summary>
/// 通过一个继承自 <see cref="HttpServerAppBase"/> 的子类来简单创建一个 <see cref="HttpServer"/>
/// </summary>
/// <param name="app"></param>
/// <returns>
/// 生成的 <see cref="HttpServerAppBase"/> 对象调用 <see cref="HttpServerAppBase.Run"/> 来启动 <see cref="HttpServer"/>
/// 生成的 <see cref="HttpServerAppBase"/> 对象,调用 <see cref="HttpServerAppBase.Run"/> 来启动 <see cref="HttpServer"/>
/// </returns>
static HttpServerAppBase LoadNew(HttpServerAppBase app)
public static HttpServerAppBase LoadNew(HttpServerAppBase app)
{
Type type = app.GetType();

// 获取所有被标记为HttpHandlerFunction的方法
MethodInfo[] methodInfos = type.GetMethods(BindingFlags.Public | BindingFlags.Instance | BindingFlags.DeclaredOnly);
Dictionary<HttpHandlerFunction, MethodInfo> methods = new();
Dictionary<HttpHandlerAttribute, MethodInfo> methods = new();

// 遍历方法数组检查每个方法是否被标记为HttpHandlerFunction
// 遍历方法数组,检查每个方法是否被标记为HttpHandlerFunction
foreach (MethodInfo methodInfo in methodInfos)
{
HttpHandlerFunction? attr = Attribute.GetCustomAttribute(methodInfo, typeof(HttpHandlerFunction)) as HttpHandlerFunction;
HttpHandlerAttribute? attr = Attribute.GetCustomAttribute(methodInfo, typeof(HttpHandlerAttribute)) as HttpHandlerAttribute;
ParameterInfo[] parameters = methodInfo.GetParameters();
if (attr != null && parameters.Length > 0 && parameters[0].ParameterType == typeof(HttpClientRequest))
{
Expand All @@ -43,16 +44,16 @@ static HttpServerAppBase LoadNew(HttpServerAppBase app)
/// </summary>
/// <param name="app"></param>
/// <returns>
/// 生成的 <see cref="HttpServerAppBase"/> 对象调用 <see cref="HttpServerAppBase.Run"/> 来启动 <see cref="HttpServer"/>
/// 生成的 <see cref="HttpServerAppBase"/> 对象,调用 <see cref="HttpServerAppBase.Run"/> 来启动 <see cref="HttpServer"/>
/// </returns>
static HttpServerAppBase LoadNew(IList<MethodInfo> methodInfos)
{
HttpServerAppBase app = new HttpServerAppBase();
Dictionary<HttpHandlerFunction, MethodInfo> methods = new();
// 遍历方法数组检查每个方法是否被标记为HttpHandlerFunction
Dictionary<HttpHandlerAttribute, MethodInfo> methods = new();
// 遍历方法数组,检查每个方法是否被标记为HttpHandlerFunction
foreach (MethodInfo methodInfo in methodInfos)
{
HttpHandlerFunction? attr = Attribute.GetCustomAttribute(methodInfo, typeof(HttpHandlerFunction)) as HttpHandlerFunction;
HttpHandlerAttribute? attr = Attribute.GetCustomAttribute(methodInfo, typeof(HttpHandlerAttribute)) as HttpHandlerAttribute;
ParameterInfo[] parameters = methodInfo.GetParameters();
if (attr != null && parameters.Length > 0 && parameters[0].ParameterType == typeof(HttpClientRequest))
{
Expand Down
Loading

0 comments on commit 71a2820

Please sign in to comment.