mirror of
https://gitlab.com/fabinfra/fabaccess/borepin.git
synced 2025-06-11 02:53:23 +02:00
Added: Create STD File, ReadData, WriteData
This commit is contained in:
44
NFC/Crypto/CRC/CRC16.cs
Normal file
44
NFC/Crypto/CRC/CRC16.cs
Normal file
@ -0,0 +1,44 @@
|
||||
using System;
|
||||
|
||||
namespace NFC.Crypto
|
||||
{
|
||||
/// <summary>
|
||||
/// CRC16 for DESFire Card
|
||||
/// </summary>
|
||||
public class CRC16
|
||||
{
|
||||
public UInt16 Polynomial { get; } = 0x8408;
|
||||
|
||||
public UInt16 InitValue { get; } = 0x6363;
|
||||
|
||||
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 ^= Polynomial;
|
||||
}
|
||||
}
|
||||
}
|
||||
return crc16;
|
||||
}
|
||||
|
||||
public byte[] Calculate(params byte[][] data)
|
||||
{
|
||||
UInt16 crc16 = InitValue;
|
||||
|
||||
foreach(byte[] d in data)
|
||||
{
|
||||
crc16 = Calculate(d, crc16);
|
||||
}
|
||||
|
||||
return BitConverter.GetBytes(crc16);
|
||||
}
|
||||
}
|
||||
}
|
44
NFC/Crypto/CRC/CRC32.cs
Normal file
44
NFC/Crypto/CRC/CRC32.cs
Normal file
@ -0,0 +1,44 @@
|
||||
using System;
|
||||
|
||||
namespace NFC.Crypto
|
||||
{
|
||||
/// <summary>
|
||||
/// CRC32 for DESFire Card
|
||||
/// </summary>
|
||||
public class CRC32
|
||||
{
|
||||
public UInt32 Polynomial { get; } = 0xEDB88320;
|
||||
|
||||
public UInt32 InitValue { get; } = 0xFFFFFFFF;
|
||||
|
||||
public UInt32 Calculate(byte[] data, UInt32 crc32)
|
||||
{
|
||||
for (int i = 0; i < data.Length; i++)
|
||||
{
|
||||
crc32 ^= data[i];
|
||||
for (int b = 0; b < 8; b++)
|
||||
{
|
||||
bool b_Bit = (crc32 & 0x01) > 0;
|
||||
crc32 >>= 1;
|
||||
if (b_Bit)
|
||||
{
|
||||
crc32 ^= Polynomial;
|
||||
}
|
||||
}
|
||||
}
|
||||
return crc32;
|
||||
}
|
||||
|
||||
public byte[] Calculate(params byte[][] data)
|
||||
{
|
||||
UInt32 crc32 = InitValue;
|
||||
|
||||
foreach(byte[] d in data)
|
||||
{
|
||||
crc32 = Calculate(d, crc32);
|
||||
}
|
||||
|
||||
return BitConverter.GetBytes(crc32);
|
||||
}
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user