123123
This commit is contained in:
@@ -1,24 +1,26 @@
|
||||
namespace ZelWiki.Security
|
||||
{
|
||||
/// <summary>
|
||||
/// Performs 32-bit reversed cyclic redundancy checks.
|
||||
///
|
||||
/// </summary>
|
||||
public class Crc32
|
||||
{
|
||||
#region Constants
|
||||
#region
|
||||
|
||||
/// <summary>
|
||||
/// Generator polynomial (modulo 2) for the reversed CRC32 algorithm.
|
||||
/// 逆CRC32算法的生成多项式(模2)
|
||||
/// </summary>
|
||||
private const UInt32 s_generator = 0xEDB88320;
|
||||
|
||||
#endregion
|
||||
|
||||
#region Constructors
|
||||
#region
|
||||
|
||||
/// <summary>
|
||||
/// Creates a new instance of the Crc32 class.
|
||||
/// 创建Crc32类的新实例
|
||||
/// </summary>
|
||||
public Crc32()
|
||||
{
|
||||
// Constructs the checksum lookup table. Used to optimize the checksum.
|
||||
m_checksumTable = Enumerable.Range(0, 256).Select(i =>
|
||||
{
|
||||
var tableEntry = (uint)i;
|
||||
@@ -28,46 +30,53 @@
|
||||
? (s_generator ^ (tableEntry >> 1))
|
||||
: (tableEntry >> 1);
|
||||
}
|
||||
|
||||
return tableEntry;
|
||||
}).ToArray();
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region Methods
|
||||
#region
|
||||
|
||||
/// <summary>
|
||||
/// Calculates the checksum of the byte stream.
|
||||
/// 计算字节流的校验和
|
||||
/// </summary>
|
||||
/// <param name="byteStream">The byte stream to calculate the checksum for.</param>
|
||||
/// <returns>A 32-bit reversed checksum.</returns>
|
||||
/// <param name="byteStream"></param>
|
||||
/// <typeparam name="T"></typeparam>
|
||||
/// <returns></returns>
|
||||
/// <exception cref="Exception"></exception>
|
||||
public UInt32 Get<T>(IEnumerable<T> byteStream)
|
||||
{
|
||||
try
|
||||
{
|
||||
// Initialize checksumRegister to 0xFFFFFFFF and calculate the checksum.
|
||||
return ~byteStream.Aggregate(0xFFFFFFFF, (checksumRegister, currentByte) =>
|
||||
(m_checksumTable[(checksumRegister & 0xFF) ^ Convert.ToByte(currentByte)] ^ (checksumRegister >> 8)));
|
||||
(m_checksumTable[(checksumRegister & 0xFF) ^ Convert.ToByte(currentByte)] ^
|
||||
(checksumRegister >> 8)));
|
||||
}
|
||||
catch (FormatException e)
|
||||
{
|
||||
throw new Exception("Could not read the stream out as bytes.", e);
|
||||
throw new Exception("无法以字节形式读取流", e);
|
||||
}
|
||||
catch (InvalidCastException e)
|
||||
{
|
||||
throw new Exception("Could not read the stream out as bytes.", e);
|
||||
throw new Exception("无法以字节形式读取流", e);
|
||||
}
|
||||
catch (OverflowException e)
|
||||
{
|
||||
throw new Exception("Could not read the stream out as bytes.", e);
|
||||
throw new Exception("无法以字节形式读取流", e);
|
||||
}
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region Fields
|
||||
#region
|
||||
|
||||
/// <summary>
|
||||
/// Contains a cache of calculated checksum chunks.
|
||||
/// 包含计算校验和块的缓存
|
||||
/// </summary>
|
||||
private readonly UInt32[] m_checksumTable;
|
||||
|
||||
#endregion
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user