dotnet / iot

This repo includes .NET Core implementations for various IoT boards, chips, displays and PCBs.
MIT License
2.12k stars 574 forks source link

add support for Mifare Classic with 7-byte UIDs #2319

Closed jdbruner closed 3 weeks ago

jdbruner commented 1 month ago

Mifare Classic cards can have either 4-byte or 7-byte UIDs (serial numbers). The existing implementation only supports cards with 4-byte UIDs (which are far more common). This PR adds support for cards with 7-byte UIDs.

There are two sets of changes in this PR:

  1. Update SetCapacity to match an ATQA of either 0x0004 (4-byte UID) or 0x0044 (7-byte UID).
  2. Change the serialization of authentication commands in Serialize to work with either length of the SerialNumber. The arguments to the AuthenticationA and AuthenticationB commands are the command byte, block number, key (6 bytes), and last four digits of the serial number (which is the entire serial number in the case of a 4-byte UID). This code was also refactored so that the same code is used for both commands (differing only in the key that is used).

Tested with Mifare Classic 1K (4-byte UID), Mifare Classic 1K (7-byte UID), and Mifare Classic 4K (4-byte UID) cards using the PN532 sample code (and a PN532 transceiver :-)).

Microsoft Reviewers: Open in CodeFlow