borepin/NFC/Crypto/CRC16.cs

45 lines
1.0 KiB
C#
Raw Normal View History

2020-10-07 19:25:51 +02:00
using System;
namespace NFC.Crypto
{
public class CRC16
{
public byte[] Calculate(byte[] data)
{
UInt16 crc16 = 0x6363;
crc16 = Calculate(data, crc16);
return BitConverter.GetBytes(crc16);
}
public byte[] Calculate(byte[] cmd, byte[] data)
{
UInt16 crc16 = 0x6363;
crc16 = Calculate(cmd, crc16);
crc16 = Calculate(data, crc16);
return BitConverter.GetBytes(crc16);
}
public UInt16 Calculate(byte[] data, UInt16 crc16)
{
for (int i = 0; i < data.Length; i++)
{
crc16 ^= data[i];
for (int b = 0; b < 8; b++)
{
bool b_Bit = (crc16 & 0x01) > 0;
crc16 >>= 1;
if (b_Bit)
{
crc16 ^= 0x8408;
}
}
}
return crc16;
}
}
}