Browse Source

BytesUtility:增加 CRC16 方法。

master
王厅 1 week ago
parent
commit
f878c7336e
  1. 3
      Apewer/BytesUtility.cs
  2. 54
      Apewer/Internals/CRC16.cs

3
Apewer/BytesUtility.cs

@ -1079,6 +1079,9 @@ namespace Apewer
/// <summary>获取 SHA512 值。</summary> /// <summary>获取 SHA512 值。</summary>
public static byte[] SHA512(this Stream stream, Action<long> progress = null) => ComputeHash<SHA512CryptoServiceProvider>(stream, progress); public static byte[] SHA512(this Stream stream, Action<long> progress = null) => ComputeHash<SHA512CryptoServiceProvider>(stream, progress);
/// <summary>计算 CRC16 校验和。</summary>
public static ushort CRC16(this byte[] bytes) => Internals.CRC16.Instance.Compute(bytes);
#endregion #endregion
} }

54
Apewer/Internals/CRC16.cs

@ -0,0 +1,54 @@
using System;
using System.Collections.Generic;
using System.Text;
namespace Apewer.Internals
{
class CRC16
{
public static CRC16 Instance = new CRC16();
const ushort origin = 0xFFFF;
const ushort polynomial = 0xA001;
ushort[] table = new ushort[256];
public CRC16()
{
ushort value, temp;
for (ushort i = 0; i < table.Length; ++i)
{
value = 0;
temp = i;
for (byte j = 0; j < 8; ++j)
{
if (((value ^ temp) & 0x0001) != 0)
{
value = (ushort)((value >> 1) ^ polynomial);
}
else
{
value >>= 1;
}
temp >>= 1;
}
table[i] = value;
}
}
public ushort Compute(byte[] bytes)
{
ushort crc = origin;
for (int i = 0; i < bytes.Length; ++i)
{
byte index = (byte)(crc ^ bytes[i]);
crc = (ushort)((crc >> 8) ^ table[index]);
}
return crc;
}
}
}
Loading…
Cancel
Save