nfctester 0.0.35__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (48) hide show
  1. nfctester-0.0.35/LICENSE +201 -0
  2. nfctester-0.0.35/PKG-INFO +87 -0
  3. nfctester-0.0.35/README.md +75 -0
  4. nfctester-0.0.35/pyproject.toml +53 -0
  5. nfctester-0.0.35/setup.cfg +4 -0
  6. nfctester-0.0.35/src/nfctester/__init__.py +4 -0
  7. nfctester-0.0.35/src/nfctester/cards/__init__.py +8 -0
  8. nfctester-0.0.35/src/nfctester/cards/base_card.py +46 -0
  9. nfctester-0.0.35/src/nfctester/cards/base_tag.py +25 -0
  10. nfctester-0.0.35/src/nfctester/cards/mifare_classic.py +71 -0
  11. nfctester-0.0.35/src/nfctester/cards/ntag21x.py +55 -0
  12. nfctester-0.0.35/src/nfctester/cards/ntag224.py +111 -0
  13. nfctester-0.0.35/src/nfctester/cards/type2tag.py +97 -0
  14. nfctester-0.0.35/src/nfctester/crypto/__init__.py +5 -0
  15. nfctester-0.0.35/src/nfctester/crypto/aes128.py +27 -0
  16. nfctester-0.0.35/src/nfctester/crypto/base_crypto.py +26 -0
  17. nfctester-0.0.35/src/nfctester/crypto/crypto1.py +163 -0
  18. nfctester-0.0.35/src/nfctester/drivers/__init__.py +4 -0
  19. nfctester-0.0.35/src/nfctester/drivers/card_reader.py +98 -0
  20. nfctester-0.0.35/src/nfctester/drivers/pn532_hsu.py +304 -0
  21. nfctester-0.0.35/src/nfctester/hardware/__init__.py +4 -0
  22. nfctester-0.0.35/src/nfctester/hardware/base.py +33 -0
  23. nfctester-0.0.35/src/nfctester/hardware/serial_transport.py +27 -0
  24. nfctester-0.0.35/src/nfctester/main.py +6 -0
  25. nfctester-0.0.35/src/nfctester/parsers/__init__.py +6 -0
  26. nfctester-0.0.35/src/nfctester/parsers/base_parser.py +45 -0
  27. nfctester-0.0.35/src/nfctester/parsers/mifare_classic_parser.py +53 -0
  28. nfctester-0.0.35/src/nfctester/parsers/pn532_hsu_parser.py +128 -0
  29. nfctester-0.0.35/src/nfctester/parsers/t2t_parser.py +47 -0
  30. nfctester-0.0.35/src/nfctester/pn532_test.py +131 -0
  31. nfctester-0.0.35/src/nfctester/tools/__init__.py +1 -0
  32. nfctester-0.0.35/src/nfctester/tools/aes128_cli.py +29 -0
  33. nfctester-0.0.35/src/nfctester/tools/pn532_scanner.py +74 -0
  34. nfctester-0.0.35/src/nfctester/tools/pn532_transceive.py +77 -0
  35. nfctester-0.0.35/src/nfctester/trace/__init__.py +6 -0
  36. nfctester-0.0.35/src/nfctester/trace/decoder.py +56 -0
  37. nfctester-0.0.35/src/nfctester/trace/formatter.py +92 -0
  38. nfctester-0.0.35/src/nfctester/trace/handler.py +44 -0
  39. nfctester-0.0.35/src/nfctester/trace/manager.py +90 -0
  40. nfctester-0.0.35/src/nfctester/utils/__init__.py +4 -0
  41. nfctester-0.0.35/src/nfctester/utils/bitops.py +87 -0
  42. nfctester-0.0.35/src/nfctester/utils/crc.py +12 -0
  43. nfctester-0.0.35/src/nfctester.egg-info/PKG-INFO +87 -0
  44. nfctester-0.0.35/src/nfctester.egg-info/SOURCES.txt +46 -0
  45. nfctester-0.0.35/src/nfctester.egg-info/dependency_links.txt +1 -0
  46. nfctester-0.0.35/src/nfctester.egg-info/entry_points.txt +4 -0
  47. nfctester-0.0.35/src/nfctester.egg-info/requires.txt +3 -0
  48. nfctester-0.0.35/src/nfctester.egg-info/top_level.txt +1 -0
@@ -0,0 +1,201 @@
1
+ Apache License
2
+ Version 2.0, January 2004
3
+ http://www.apache.org/licenses/
4
+
5
+ TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
6
+
7
+ 1. Definitions.
8
+
9
+ "License" shall mean the terms and conditions for use, reproduction,
10
+ and distribution as defined by Sections 1 through 9 of this document.
11
+
12
+ "Licensor" shall mean the copyright owner or entity authorized by
13
+ the copyright owner that is granting the License.
14
+
15
+ "Legal Entity" shall mean the union of the acting entity and all
16
+ other entities that control, are controlled by, or are under common
17
+ control with that entity. For the purposes of this definition,
18
+ "control" means (i) the power, direct or indirect, to cause the
19
+ direction or management of such entity, whether by contract or
20
+ otherwise, or (ii) ownership of fifty percent (50%) or more of the
21
+ outstanding shares, or (iii) beneficial ownership of such entity.
22
+
23
+ "You" (or "Your") shall mean an individual or Legal Entity
24
+ exercising permissions granted by this License.
25
+
26
+ "Source" form shall mean the preferred form for making modifications,
27
+ including but not limited to software source code, documentation
28
+ source, and configuration files.
29
+
30
+ "Object" form shall mean any form resulting from mechanical
31
+ transformation or translation of a Source form, including but
32
+ not limited to compiled object code, generated documentation,
33
+ and conversions to other media types.
34
+
35
+ "Work" shall mean the work of authorship, whether in Source or
36
+ Object form, made available under the License, as indicated by a
37
+ copyright notice that is included in or attached to the work
38
+ (an example is provided in the Appendix below).
39
+
40
+ "Derivative Works" shall mean any work, whether in Source or Object
41
+ form, that is based on (or derived from) the Work and for which the
42
+ editorial revisions, annotations, elaborations, or other modifications
43
+ represent, as a whole, an original work of authorship. For the purposes
44
+ of this License, Derivative Works shall not include works that remain
45
+ separable from, or merely link (or bind by name) to the interfaces of,
46
+ the Work and Derivative Works thereof.
47
+
48
+ "Contribution" shall mean any work of authorship, including
49
+ the original version of the Work and any modifications or additions
50
+ to that Work or Derivative Works thereof, that is intentionally
51
+ submitted to Licensor for inclusion in the Work by the copyright owner
52
+ or by an individual or Legal Entity authorized to submit on behalf of
53
+ the copyright owner. For the purposes of this definition, "submitted"
54
+ means any form of electronic, verbal, or written communication sent
55
+ to the Licensor or its representatives, including but not limited to
56
+ communication on electronic mailing lists, source code control systems,
57
+ and issue tracking systems that are managed by, or on behalf of, the
58
+ Licensor for the purpose of discussing and improving the Work, but
59
+ excluding communication that is conspicuously marked or otherwise
60
+ designated in writing by the copyright owner as "Not a Contribution."
61
+
62
+ "Contributor" shall mean Licensor and any individual or Legal Entity
63
+ on behalf of whom a Contribution has been received by Licensor and
64
+ subsequently incorporated within the Work.
65
+
66
+ 2. Grant of Copyright License. Subject to the terms and conditions of
67
+ this License, each Contributor hereby grants to You a perpetual,
68
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
69
+ copyright license to reproduce, prepare Derivative Works of,
70
+ publicly display, publicly perform, sublicense, and distribute the
71
+ Work and such Derivative Works in Source or Object form.
72
+
73
+ 3. Grant of Patent License. Subject to the terms and conditions of
74
+ this License, each Contributor hereby grants to You a perpetual,
75
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
76
+ (except as stated in this section) patent license to make, have made,
77
+ use, offer to sell, sell, import, and otherwise transfer the Work,
78
+ where such license applies only to those patent claims licensable
79
+ by such Contributor that are necessarily infringed by their
80
+ Contribution(s) alone or by combination of their Contribution(s)
81
+ with the Work to which such Contribution(s) was submitted. If You
82
+ institute patent litigation against any entity (including a
83
+ cross-claim or counterclaim in a lawsuit) alleging that the Work
84
+ or a Contribution incorporated within the Work constitutes direct
85
+ or contributory patent infringement, then any patent licenses
86
+ granted to You under this License for that Work shall terminate
87
+ as of the date such litigation is filed.
88
+
89
+ 4. Redistribution. You may reproduce and distribute copies of the
90
+ Work or Derivative Works thereof in any medium, with or without
91
+ modifications, and in Source or Object form, provided that You
92
+ meet the following conditions:
93
+
94
+ (a) You must give any other recipients of the Work or
95
+ Derivative Works a copy of this License; and
96
+
97
+ (b) You must cause any modified files to carry prominent notices
98
+ stating that You changed the files; and
99
+
100
+ (c) You must retain, in the Source form of any Derivative Works
101
+ that You distribute, all copyright, patent, trademark, and
102
+ attribution notices from the Source form of the Work,
103
+ excluding those notices that do not pertain to any part of
104
+ the Derivative Works; and
105
+
106
+ (d) If the Work includes a "NOTICE" text file as part of its
107
+ distribution, then any Derivative Works that You distribute must
108
+ include a readable copy of the attribution notices contained
109
+ within such NOTICE file, excluding those notices that do not
110
+ pertain to any part of the Derivative Works, in at least one
111
+ of the following places: within a NOTICE text file distributed
112
+ as part of the Derivative Works; within the Source form or
113
+ documentation, if provided along with the Derivative Works; or,
114
+ within a display generated by the Derivative Works, if and
115
+ wherever such third-party notices normally appear. The contents
116
+ of the NOTICE file are for informational purposes only and
117
+ do not modify the License. You may add Your own attribution
118
+ notices within Derivative Works that You distribute, alongside
119
+ or as an addendum to the NOTICE text from the Work, provided
120
+ that such additional attribution notices cannot be construed
121
+ as modifying the License.
122
+
123
+ You may add Your own copyright statement to Your modifications and
124
+ may provide additional or different license terms and conditions
125
+ for use, reproduction, or distribution of Your modifications, or
126
+ for any such Derivative Works as a whole, provided Your use,
127
+ reproduction, and distribution of the Work otherwise complies with
128
+ the conditions stated in this License.
129
+
130
+ 5. Submission of Contributions. Unless You explicitly state otherwise,
131
+ any Contribution intentionally submitted for inclusion in the Work
132
+ by You to the Licensor shall be under the terms and conditions of
133
+ this License, without any additional terms or conditions.
134
+ Notwithstanding the above, nothing herein shall supersede or modify
135
+ the terms of any separate license agreement you may have executed
136
+ with Licensor regarding such Contributions.
137
+
138
+ 6. Trademarks. This License does not grant permission to use the trade
139
+ names, trademarks, service marks, or product names of the Licensor,
140
+ except as required for reasonable and customary use in describing the
141
+ origin of the Work and reproducing the content of the NOTICE file.
142
+
143
+ 7. Disclaimer of Warranty. Unless required by applicable law or
144
+ agreed to in writing, Licensor provides the Work (and each
145
+ Contributor provides its Contributions) on an "AS IS" BASIS,
146
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
147
+ implied, including, without limitation, any warranties or conditions
148
+ of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
149
+ PARTICULAR PURPOSE. You are solely responsible for determining the
150
+ appropriateness of using or redistributing the Work and assume any
151
+ risks associated with Your exercise of permissions under this License.
152
+
153
+ 8. Limitation of Liability. In no event and under no legal theory,
154
+ whether in tort (including negligence), contract, or otherwise,
155
+ unless required by applicable law (such as deliberate and grossly
156
+ negligent acts) or agreed to in writing, shall any Contributor be
157
+ liable to You for damages, including any direct, indirect, special,
158
+ incidental, or consequential damages of any character arising as a
159
+ result of this License or out of the use or inability to use the
160
+ Work (including but not limited to damages for loss of goodwill,
161
+ work stoppage, computer failure or malfunction, or any and all
162
+ other commercial damages or losses), even if such Contributor
163
+ has been advised of the possibility of such damages.
164
+
165
+ 9. Accepting Warranty or Additional Liability. While redistributing
166
+ the Work or Derivative Works thereof, You may choose to offer,
167
+ and charge a fee for, acceptance of support, warranty, indemnity,
168
+ or other liability obligations and/or rights consistent with this
169
+ License. However, in accepting such obligations, You may act only
170
+ on Your own behalf and on Your sole responsibility, not on behalf
171
+ of any other Contributor, and only if You agree to indemnify,
172
+ defend, and hold each Contributor harmless for any liability
173
+ incurred by, or claims asserted against, such Contributor by reason
174
+ of your accepting any such warranty or additional liability.
175
+
176
+ END OF TERMS AND CONDITIONS
177
+
178
+ APPENDIX: How to apply the Apache License to your work.
179
+
180
+ To apply the Apache License to your work, attach the following
181
+ boilerplate notice, with the fields enclosed by brackets "[]"
182
+ replaced with your own identifying information. (Don't include
183
+ the brackets!) The text should be enclosed in the appropriate
184
+ comment syntax for the file format. We also recommend that a
185
+ file or class name and description of purpose be included on the
186
+ same "printed page" as the copyright notice for easier
187
+ identification within third-party archives.
188
+
189
+ Copyright [yyyy] [name of copyright owner]
190
+
191
+ Licensed under the Apache License, Version 2.0 (the "License");
192
+ you may not use this file except in compliance with the License.
193
+ You may obtain a copy of the License at
194
+
195
+ http://www.apache.org/licenses/LICENSE-2.0
196
+
197
+ Unless required by applicable law or agreed to in writing, software
198
+ distributed under the License is distributed on an "AS IS" BASIS,
199
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
200
+ See the License for the specific language governing permissions and
201
+ limitations under the License.
@@ -0,0 +1,87 @@
1
+ Metadata-Version: 2.4
2
+ Name: nfctester
3
+ Version: 0.0.35
4
+ Summary: A RFID/NFC automation testing framework for cards and readers.
5
+ Requires-Python: >=3.14
6
+ Description-Content-Type: text/markdown
7
+ License-File: LICENSE
8
+ Requires-Dist: loguru>=0.7.3
9
+ Requires-Dist: pycryptodome>=3.23.0
10
+ Requires-Dist: pyserial>=3.5
11
+ Dynamic: license-file
12
+
13
+ # nfctester 🥕
14
+
15
+ [![License](https://img.shields.io/badge/License-Apache_2.0-blue.svg)](https://opensource.org/licenses/Apache-2.0)
16
+ [![Python](https://img.shields.io/badge/Python-3.14+-blue.svg)](https://www.python.org/downloads/)
17
+ [![Version](https://img.shields.io/badge/version-0.0.35-green.svg)](pyproject.toml)
18
+
19
+ `nfctester` 是一个专为 RFID/NFC 卡片与 PN532 读卡器设计的自动化测试框架。项目采用严谨的分层架构设计,实现了硬件通信、芯片驱动、卡片逻辑与加密算法的深度解耦,旨在为 RFID 技术研究、漏洞分析及产品测试提供一个健壮且易于扩展的基础平台。
20
+
21
+ ## 🌟 核心特性
22
+
23
+ - **分层架构**: 清晰的 8 层体系结构,模块化程度高,易于维护与扩展。
24
+ - **广泛的协议支持**:
25
+ - **卡片**: Mifare Classic, ISO14443A, NFC Forum Type 2 Tag (NTAG21x/22x 等)。
26
+ - **芯片**: 深度优化 PN532 HSU (High Speed UART) 驱动,支持位帧 (Bit-framing) 收发。
27
+ - **强大加密支持**: 内置 AES-128 (CBC)、Mifare Crypto1 算法引擎,支持 NTAG22x AES 互认证。
28
+ - **可视化跟踪**: 独特的跟踪控制层与协议解析层,提供树状结构化的通信日志输出,完美还原协议交互细节。
29
+ - **现代化工具链**: 基于 `uv` 进行包管理,集成 `pytest` 自动化测试与 `bump-my-version` 版本控制。
30
+
31
+ ## 🏗️ 架构体系 (8-Layer Architecture)
32
+
33
+ 项目遵循高度抽象的设计模式,分为以下八层:
34
+
35
+ 1. **硬件传输层 (Hardware)**: 负责底层字节流传输(如 `SerialTransport`)。
36
+ 2. **驱动层 (Driver)**: 实现特定芯片(如 PN532)的协议封装与寄存器操作。
37
+ 3. **卡片逻辑层 (Card)**: 定义各种 RFID 标签与智能卡的协议行为(Mifare, NTAG 等)。
38
+ 4. **加密算法层 (Crypto)**: 提供原子级的加密/解密操作(AES, Crypto1)。
39
+ 5. **通用工具层 (Utility)**: 包含 CRC 校验、位操作等基础辅助函数。
40
+ 6. **跟踪控制层 (Trace)**: 中心化的日志管理系统,实现业务逻辑与通信监控的分离。
41
+ 7. **协议解析层 (Parsers)**: 将字节流解析为结构化字段树,供格式化输出使用。
42
+ 8. **脚本/CLI 层 (CLI)**: 提供开箱即用的命令行工具。
43
+
44
+ ## 🚀 快速上手
45
+
46
+ ### 环境准备
47
+
48
+ 推荐使用 [uv](https://github.com/astral-sh/uv) 进行环境管理:
49
+
50
+ ```bash
51
+ # 克隆仓库
52
+ git clone https://github.com/crthu/nfctester.git
53
+ cd nfctester
54
+
55
+ # 同步依赖
56
+ uv sync
57
+ ```
58
+
59
+ ### 运行工具
60
+
61
+ 框架内置了多个实用的 CLI 工具:
62
+
63
+ - **PN532 扫描器**:
64
+ ```bash
65
+ uv run pn532-scanner
66
+ ```
67
+ - **AES-128 加密工具**:
68
+ ```bash
69
+ uv run aes128-cli -m encrypt -i <hex_data> -k <hex_key>
70
+ ```
71
+
72
+ ### 运行测试
73
+
74
+ ```bash
75
+ # 运行所有测试
76
+ uv run pytest
77
+
78
+ # 运行特定模块测试(如加密模块)
79
+ uv run pytest tests/crypto/
80
+ ```
81
+
82
+ ## 📄 开源协议
83
+
84
+ 本项目基于 **Apache License 2.0** 协议开源。详见 [LICENSE](LICENSE) 文件。
85
+
86
+ ---
87
+ *本README由 Gemini 3 Flash 生成*
@@ -0,0 +1,75 @@
1
+ # nfctester 🥕
2
+
3
+ [![License](https://img.shields.io/badge/License-Apache_2.0-blue.svg)](https://opensource.org/licenses/Apache-2.0)
4
+ [![Python](https://img.shields.io/badge/Python-3.14+-blue.svg)](https://www.python.org/downloads/)
5
+ [![Version](https://img.shields.io/badge/version-0.0.35-green.svg)](pyproject.toml)
6
+
7
+ `nfctester` 是一个专为 RFID/NFC 卡片与 PN532 读卡器设计的自动化测试框架。项目采用严谨的分层架构设计,实现了硬件通信、芯片驱动、卡片逻辑与加密算法的深度解耦,旨在为 RFID 技术研究、漏洞分析及产品测试提供一个健壮且易于扩展的基础平台。
8
+
9
+ ## 🌟 核心特性
10
+
11
+ - **分层架构**: 清晰的 8 层体系结构,模块化程度高,易于维护与扩展。
12
+ - **广泛的协议支持**:
13
+ - **卡片**: Mifare Classic, ISO14443A, NFC Forum Type 2 Tag (NTAG21x/22x 等)。
14
+ - **芯片**: 深度优化 PN532 HSU (High Speed UART) 驱动,支持位帧 (Bit-framing) 收发。
15
+ - **强大加密支持**: 内置 AES-128 (CBC)、Mifare Crypto1 算法引擎,支持 NTAG22x AES 互认证。
16
+ - **可视化跟踪**: 独特的跟踪控制层与协议解析层,提供树状结构化的通信日志输出,完美还原协议交互细节。
17
+ - **现代化工具链**: 基于 `uv` 进行包管理,集成 `pytest` 自动化测试与 `bump-my-version` 版本控制。
18
+
19
+ ## 🏗️ 架构体系 (8-Layer Architecture)
20
+
21
+ 项目遵循高度抽象的设计模式,分为以下八层:
22
+
23
+ 1. **硬件传输层 (Hardware)**: 负责底层字节流传输(如 `SerialTransport`)。
24
+ 2. **驱动层 (Driver)**: 实现特定芯片(如 PN532)的协议封装与寄存器操作。
25
+ 3. **卡片逻辑层 (Card)**: 定义各种 RFID 标签与智能卡的协议行为(Mifare, NTAG 等)。
26
+ 4. **加密算法层 (Crypto)**: 提供原子级的加密/解密操作(AES, Crypto1)。
27
+ 5. **通用工具层 (Utility)**: 包含 CRC 校验、位操作等基础辅助函数。
28
+ 6. **跟踪控制层 (Trace)**: 中心化的日志管理系统,实现业务逻辑与通信监控的分离。
29
+ 7. **协议解析层 (Parsers)**: 将字节流解析为结构化字段树,供格式化输出使用。
30
+ 8. **脚本/CLI 层 (CLI)**: 提供开箱即用的命令行工具。
31
+
32
+ ## 🚀 快速上手
33
+
34
+ ### 环境准备
35
+
36
+ 推荐使用 [uv](https://github.com/astral-sh/uv) 进行环境管理:
37
+
38
+ ```bash
39
+ # 克隆仓库
40
+ git clone https://github.com/crthu/nfctester.git
41
+ cd nfctester
42
+
43
+ # 同步依赖
44
+ uv sync
45
+ ```
46
+
47
+ ### 运行工具
48
+
49
+ 框架内置了多个实用的 CLI 工具:
50
+
51
+ - **PN532 扫描器**:
52
+ ```bash
53
+ uv run pn532-scanner
54
+ ```
55
+ - **AES-128 加密工具**:
56
+ ```bash
57
+ uv run aes128-cli -m encrypt -i <hex_data> -k <hex_key>
58
+ ```
59
+
60
+ ### 运行测试
61
+
62
+ ```bash
63
+ # 运行所有测试
64
+ uv run pytest
65
+
66
+ # 运行特定模块测试(如加密模块)
67
+ uv run pytest tests/crypto/
68
+ ```
69
+
70
+ ## 📄 开源协议
71
+
72
+ 本项目基于 **Apache License 2.0** 协议开源。详见 [LICENSE](LICENSE) 文件。
73
+
74
+ ---
75
+ *本README由 Gemini 3 Flash 生成*
@@ -0,0 +1,53 @@
1
+ [project]
2
+ name = "nfctester"
3
+ version = "0.0.35"
4
+ description = "A RFID/NFC automation testing framework for cards and readers."
5
+ readme = "README.md"
6
+ requires-python = ">=3.14"
7
+ dependencies = [
8
+ "loguru>=0.7.3",
9
+ "pycryptodome>=3.23.0",
10
+ "pyserial>=3.5",
11
+ ]
12
+
13
+ [tool.setuptools.packages.find]
14
+ where = ["src"]
15
+
16
+ [project.scripts]
17
+ nfctester = "nfctester.main:main"
18
+ pn532-scanner = "nfctester.tools.pn532_scanner:run_scanner"
19
+ aes128-cli = "nfctester.tools.aes128_cli:main"
20
+
21
+
22
+
23
+ [tool.pytest.ini_options]
24
+ pythonpath = ["src"]
25
+ testpaths = ["tests"]
26
+
27
+ [tool.uv]
28
+ package = true
29
+
30
+ [dependency-groups]
31
+ dev = [
32
+ "pytest>=9.0.3",
33
+ "pytest-dependency>=0.6.0"
34
+ ]
35
+
36
+
37
+ [tool.bumpversion]
38
+ current_version = "0.0.35"
39
+ commit = true
40
+ tag = true
41
+ allow_dirty = true
42
+ tag_name = "v{new_version}"
43
+ message = "chore(release): bump version {current_version} → {new_version}"
44
+ parse = "(?P<major>\\d+)\\.(?P<minor>\\d+)\\.(?P<patch>\\d+)"
45
+ serialize = ["{major}.{minor}.{patch}"]
46
+
47
+ [[tool.bumpversion.files]]
48
+ filename = "pyproject.toml"
49
+
50
+ [[tool.bumpversion.files]]
51
+ filename = "README.md"
52
+ search = "version-{current_version}-green.svg"
53
+ replace = "version-{new_version}-green.svg"
@@ -0,0 +1,4 @@
1
+ [egg_info]
2
+ tag_build =
3
+ tag_date = 0
4
+
@@ -0,0 +1,4 @@
1
+ from .trace.manager import trace
2
+ from .drivers.pn532_hsu import PN532_HSU
3
+
4
+ __all__ = ["trace", "PN532_HSU"]
@@ -0,0 +1,8 @@
1
+ from .base_card import BaseCard
2
+ from .base_tag import BaseTag
3
+ from .mifare_classic import MifareClassicCard
4
+ from .ntag21x import NTAG21x
5
+ from .ntag224 import NTAG224
6
+ from .type2tag import Type2Tag
7
+
8
+ __all__ = ["BaseCard", "BaseTag", "MifareClassicCard", "NTAG21x", "NTAG224", "Type2Tag"]
@@ -0,0 +1,46 @@
1
+ from abc import ABC, abstractmethod
2
+
3
+
4
+ class BaseCard(ABC):
5
+ """
6
+ 加密卡基类 (如 Mifare Classic)
7
+ """
8
+
9
+ def __init__(self, reader, uid: bytes):
10
+ self.reader = reader
11
+ self.uid = uid
12
+
13
+ @abstractmethod
14
+ def authenticate(self, block_addr: int, key: bytes, key_type: int) -> bool:
15
+ """执行认证逻辑"""
16
+ pass
17
+
18
+ @abstractmethod
19
+ def read_block(self, block_addr: int) -> bytes:
20
+ """读取块数据"""
21
+ pass
22
+
23
+ @abstractmethod
24
+ def write_block(self, block_addr: int, data: bytes) -> bool:
25
+ """写入块数据"""
26
+ pass
27
+
28
+ @abstractmethod
29
+ def increment_block(self, block_addr: int, value: int) -> bool:
30
+ """递增块"""
31
+ pass
32
+
33
+ @abstractmethod
34
+ def decrement_block(self, block_addr: int, value: int) -> bool:
35
+ """递减块"""
36
+ pass
37
+
38
+ @abstractmethod
39
+ def restore_block(self, block_addr: int) -> bool:
40
+ """恢复块"""
41
+ pass
42
+
43
+ @abstractmethod
44
+ def transfer_block(self, block_addr: int) -> bool:
45
+ """传输块"""
46
+ pass
@@ -0,0 +1,25 @@
1
+ from abc import ABC, abstractmethod
2
+
3
+
4
+ class BaseTag(ABC):
5
+ """
6
+ RFID 标签基类
7
+ """
8
+
9
+ def __init__(self, reader, uid: bytes):
10
+ self.reader = reader
11
+ self.uid = uid
12
+
13
+ def transceive(self, data: bytes) -> bytes:
14
+ """透传数据到读卡器"""
15
+ return self.reader.transceive(data)
16
+
17
+ @abstractmethod
18
+ def read_page(self, page_addr: int) -> bytes:
19
+ """读取页数据"""
20
+ pass
21
+
22
+ @abstractmethod
23
+ def write_page(self, page_addr: int, data: bytes) -> bool:
24
+ """写入页数据"""
25
+ pass
@@ -0,0 +1,71 @@
1
+ from .base_card import BaseCard
2
+
3
+
4
+ class MifareClassicCard(BaseCard):
5
+ """MIFARE Classic 完整操作实现"""
6
+
7
+ CMD_AUTHENT = 0x40
8
+ CMD_READ = 0x30
9
+ CMD_WRITE = 0xA0
10
+ CMD_INCREMENT = 0xC1
11
+ CMD_DECREMENT = 0xC0
12
+ CMD_RESTORE = 0xC2
13
+ CMD_TRANSFER = 0xB0
14
+
15
+ def __init__(self, reader, uid: bytes):
16
+ super().__init__(reader, uid)
17
+
18
+ def authenticate(self, block_addr: int, key: bytes, key_type: int = 0x60) -> bool:
19
+ """
20
+ MIFARE Classic 认证 (使用 PN532 硬件实现)
21
+ :param block_addr: 块地址
22
+ :param key: 6 字节密钥
23
+ :param key_type: 0x60 (KeyA) 或 0x61 (KeyB)
24
+ """
25
+ if len(key) != 6:
26
+ raise ValueError("Key must be 6 bytes")
27
+
28
+ # 使用读卡器提供的 exchange (InDataExchange 自动封装)
29
+ res = self.reader.exchange(bytes([key_type, block_addr]) + key + self.uid)
30
+ return res is not None
31
+
32
+ def increment_block(self, block_addr: int, value: int) -> bool:
33
+ """对块进行递增操作"""
34
+ if not (0 <= value < (1 << 32)):
35
+ raise ValueError("value must be a 32-bit unsigned integer")
36
+ cmd = bytes([self.CMD_INCREMENT, block_addr]) + value.to_bytes(4, "little")
37
+ res = self.reader.exchange(cmd)
38
+ return res is not None
39
+
40
+ def decrement_block(self, block_addr: int, value: int) -> bool:
41
+ """对块进行递减操作"""
42
+ if not (0 <= value < (1 << 32)):
43
+ raise ValueError("value must be a 32-bit unsigned integer")
44
+ cmd = bytes([self.CMD_DECREMENT, block_addr]) + value.to_bytes(4, "little")
45
+ res = self.reader.exchange(cmd)
46
+ return res is not None
47
+
48
+ def restore_block(self, block_addr: int) -> bool:
49
+ """恢复块的临时值"""
50
+ cmd = bytes([self.CMD_RESTORE, block_addr])
51
+ res = self.reader.exchange(cmd)
52
+ return res is not None
53
+
54
+ def transfer_block(self, block_addr: int) -> bool:
55
+ """将临时值写回块"""
56
+ cmd = bytes([self.CMD_TRANSFER, block_addr])
57
+ res = self.reader.exchange(cmd)
58
+ return res is not None
59
+
60
+ def read_block(self, block_addr: int) -> bytes:
61
+ """读取块数据"""
62
+ cmd = bytes([self.CMD_READ, block_addr])
63
+ return self.reader.exchange(cmd)
64
+
65
+ def write_block(self, block_addr: int, data: bytes) -> bool:
66
+ """写入块数据"""
67
+ if len(data) != 16:
68
+ raise ValueError("Data must be 16 bytes")
69
+ cmd = bytes([self.CMD_WRITE, block_addr]) + data
70
+ res = self.reader.exchange(cmd)
71
+ return res is not None
@@ -0,0 +1,55 @@
1
+ from .type2tag import Type2Tag
2
+
3
+
4
+ class NTAG21x(Type2Tag):
5
+ """
6
+ NXP NTAG21x 系列专用驱动 (如 NTAG213, NTAG215, NTAG216)
7
+ """
8
+
9
+ CMD_GET_VERSION = 0x60
10
+ CMD_PWD_AUTH = 0x1B
11
+
12
+ def __init__(self, reader, uid: bytes):
13
+ super().__init__(reader, uid)
14
+
15
+ def get_version(self) -> bytes:
16
+ """
17
+ 发送 0x60 指令,获取 8 字节版本信息
18
+ """
19
+ cmd = bytes([self.CMD_GET_VERSION])
20
+ return self.transceive(cmd)
21
+
22
+ def auth(self, password: bytes) -> bytes:
23
+ """
24
+ 发送 0x1B 指令进行密码
25
+ 认证认证流程说明:
26
+ 1. 发送 0x1B + 4字节 PWD。
27
+ 2. 如果 PWD 正确:芯片返回 2 字节的 PACK (Password Acknowledge)。
28
+ - PACK 的值存储在配置区的特定地址(如 NTAG213 的 Page 0x2C 的 Byte 0-1)。
29
+ - 默认值通常为 0x00 0x00,取决于生产商或之前的设置。
30
+ 3. 如果 PWD 错误:芯片返回 4-bit 的 NAK (通常为 0x0)。
31
+ - 注意:驱动会将 NAK 视为传输错误或超时,返回0字节数据包。
32
+
33
+ :param password: 4 字节密码
34
+ :return: 2 字节的 PACK 响应
35
+ """
36
+ if len(password) != 4:
37
+ raise ValueError("NTAG21x password must be 4 bytes")
38
+
39
+ # 1. 组装指令: 0x1B + 4字节密码
40
+ cmd = bytes([self.CMD_PWD_AUTH]) + password
41
+
42
+ # 2. 发送指令
43
+ # 注意:如果认证失败,芯片会返回 4-bit 的 NAK (0x0),
44
+ # 许多读写器驱动(如 PN532)会将 NAK 转换成通信超时或传输错误异常。
45
+ res = self.transceive(cmd)
46
+
47
+ # 3. 验证响应
48
+ if res is None or len(res) == 0:
49
+ raise PermissionError("Authentication failed: No response (NAK)")
50
+
51
+ # NTAG21x 认证成功会返回 2 字节的 PACK
52
+ if len(res) == 2:
53
+ return res
54
+ else:
55
+ raise PermissionError(f"Authentication failed: Unexpected response length {len(res)}")