feat : CRC工具类增加获取整形值的方法

This commit is contained in:
何冠峰
2025-08-27 19:19:00 +08:00
parent 6fc82bb55a
commit c798250258
2 changed files with 47 additions and 0 deletions

View File

@@ -73,6 +73,17 @@ namespace YooAsset
{
private uint _currentCrc;
/// <summary>
/// Gets the computed hash value.
/// </summary>
public uint CRCValue
{
get
{
return _currentCrc;
}
}
/// <summary>
/// Initializes a new instance of the <see cref="CRC32Algorithm"/> class.
/// </summary>

View File

@@ -339,6 +339,11 @@ namespace YooAsset
byte[] buffer = Encoding.UTF8.GetBytes(str);
return BytesCRC32(buffer);
}
public static uint StringCRC32Value(string str)
{
byte[] buffer = Encoding.UTF8.GetBytes(str);
return BytesCRC32Value(buffer);
}
/// <summary>
/// 获取文件的CRC32
@@ -350,6 +355,13 @@ namespace YooAsset
return StreamCRC32(fs);
}
}
public static uint FileCRC32Value(string filePath)
{
using (FileStream fs = new FileStream(filePath, FileMode.Open, FileAccess.Read, FileShare.Read))
{
return StreamCRC32Value(fs);
}
}
/// <summary>
/// 获取文件的CRC32
@@ -366,6 +378,18 @@ namespace YooAsset
return string.Empty;
}
}
public static uint FileCRC32ValueSafely(string filePath)
{
try
{
return FileCRC32Value(filePath);
}
catch (Exception e)
{
YooLogger.Exception(e);
return 0;
}
}
/// <summary>
/// 获取数据流的CRC32
@@ -376,6 +400,12 @@ namespace YooAsset
byte[] hashBytes = hash.ComputeHash(stream);
return ToString(hashBytes);
}
public static uint StreamCRC32Value(Stream stream)
{
CRC32Algorithm hash = new CRC32Algorithm();
hash.ComputeHash(stream);
return hash.CRCValue;
}
/// <summary>
/// 获取字节数组的CRC32
@@ -386,6 +416,12 @@ namespace YooAsset
byte[] hashBytes = hash.ComputeHash(buffer);
return ToString(hashBytes);
}
public static uint BytesCRC32Value(byte[] buffer)
{
CRC32Algorithm hash = new CRC32Algorithm();
hash.ComputeHash(buffer);
return hash.CRCValue;
}
#endregion
}
}