Metadata-Version: 2.4
Name: mifarepy
Version: 3.4.0
Summary: Python library for interfacing with MIFARE RFID card readers using the GNetPlus® protocol
Author-email: Spark Drago <huzaifa.farooq05@gmail.com>
License-Expression: LGPL-3.0-or-later
Project-URL: Homepage, https://github.com/SparkDrago05/mifarepy
Project-URL: Documentation, https://sparkdrago05.github.io/mifarepy/
Project-URL: Source, https://github.com/SparkDrago05/mifarepy
Project-URL: Issues, https://github.com/SparkDrago05/mifarepy/issues
Keywords: mifare,rfid,nfc,gnetplus,card-reader,serial,pyserial
Classifier: Development Status :: 5 - Production/Stable
Classifier: Intended Audience :: Developers
Classifier: Operating System :: OS Independent
Classifier: Programming Language :: Python :: 3
Classifier: Programming Language :: Python :: 3 :: Only
Classifier: Programming Language :: Python :: 3.9
Classifier: Programming Language :: Python :: 3.10
Classifier: Programming Language :: Python :: 3.11
Classifier: Programming Language :: Python :: 3.12
Classifier: Programming Language :: Python :: 3.13
Classifier: Programming Language :: Python :: 3.14
Classifier: Topic :: Communications
Classifier: Topic :: Software Development :: Libraries :: Python Modules
Classifier: Topic :: System :: Hardware
Classifier: Typing :: Typed
Requires-Python: >=3.9
Description-Content-Type: text/markdown
License-File: LICENSE
License-File: COPYING
Requires-Dist: pyserial
Provides-Extra: dev
Requires-Dist: pytest; extra == "dev"
Requires-Dist: pytest-cov; extra == "dev"
Requires-Dist: ruff; extra == "dev"
Requires-Dist: mypy; extra == "dev"
Requires-Dist: types-pyserial; extra == "dev"
Provides-Extra: docs
Requires-Dist: mkdocs-material; extra == "docs"
Requires-Dist: mkdocstrings[python]; extra == "docs"
Requires-Dist: mike; extra == "docs"
Requires-Dist: mkdocs-git-authors-plugin; extra == "docs"
Dynamic: license-file

# mifarepy

[![PyPI Version](https://img.shields.io/pypi/v/mifarepy.svg)](https://pypi.org/project/mifarepy/)
[![Python Versions](https://img.shields.io/pypi/pyversions/mifarepy.svg)](https://pypi.org/project/mifarepy/)
[![PyPI Downloads](https://static.pepy.tech/badge/mifarepy)](https://pepy.tech/projects/mifarepy)
[![Tests](https://github.com/SparkDrago05/mifarepy/actions/workflows/test.yml/badge.svg)](https://github.com/SparkDrago05/mifarepy/actions/workflows/test.yml)
[![Coverage](https://img.shields.io/endpoint?url=https://raw.githubusercontent.com/SparkDrago05/mifarepy/badges/coverage.json)](https://github.com/SparkDrago05/mifarepy/actions/workflows/test.yml)
[![Typed](https://img.shields.io/badge/typing-typed-blue.svg)](https://peps.python.org/pep-0561/)
[![License](https://img.shields.io/badge/License-LGPL%20v3-blue.svg)](LICENSE)

**Read, write and manage MIFARE® Classic cards from Python** with PROMAG and
other GIGA-TMS readers that speak the **GNetPlus®** serial protocol.
Typed, tested, and its only dependency is `pyserial`.

![Terminal demo: mifarepy watch prints a card UID each time a card is tapped, then mifarepy read shows block 0 of sector 1](https://raw.githubusercontent.com/SparkDrago05/mifarepy/main/docs/assets/demo.gif)

📖 **Documentation:** <https://sparkdrago05.github.io/mifarepy/>

## Install

```bash
pip install mifarepy
```

Requires Python 3.9 or newer. Works on Linux (including Raspberry Pi), Windows and macOS.

## Command line

```bash
mifarepy info                 # reader, firmware and the card on it
mifarepy watch                # print each card as it is tapped
mifarepy read 1               # show sector 1
mifarepy dump card.json       # save the whole card (.json, .bin/.mfd, .mct)
mifarepy restore card.json    # write it back, or to another card
mifarepy access 1             # who may read, write and change each block
```

Add `--simulate` to try any command without a reader. See the
[command-line guide](https://sparkdrago05.github.io/mifarepy/latest/cli/).

## Quick start

```python
from mifarepy import MifareReader

KEY = bytes.fromhex('FFFFFFFFFFFF')   # factory default key

with MifareReader.auto() as reader:     # or MifareReader('/dev/ttyUSB0') / ('COM3')
    print('Reader:', reader.get_version())
    print('Card UID:', reader.get_uid())          # e.g. '0xEDCEF8C3'

    reader.authenticate_sector(1, KEY)
    print('Block 0:', reader.read_block(0))       # 32-char hex string
    reader.write_block(0, bytes(range(16)))
```

## Features

| | |
|---|---|
| **Card detection** | `get_uid()`, `scan_tag()`, `is_card_present()`, `wait_for_card()`, `wait_for_card_async()`, multi-card `request_all()` |
| **Authentication** | `authenticate_sector()` with Key A or B, `authenticate_sector_cached()`, `open_sector()` with a `KeyRing` |
| **Keys and access bits** | `write_trailer()` changes keys and access bits with a guard against locking a sector; `AccessBits` presets, decoding in plain language; key files (MIFARE Classic Tool / Proxmark3 lists, JSON per sector); per-card `diversify_key()` |
| **Block I/O** | `read_block()`, `write_block()`, `read_sector()`, `write_sector()` (never touches sector trailers) |
| **Bulk I/O** | `read_blocks()` / `write_blocks()` across sectors with per-sector `SectorAuth` keys |
| **Value blocks** | `create_value_block()`, `read_value()`, `increment_value()`, `decrement_value()`, `transfer()`, `restore()`, with native, verified transfer/restore |
| **Card layout** | MIFARE Classic 1K and 4K, including the 16-block sectors 32–39; `mifarepy.layout` helpers for block arithmetic |
| **Reader control** | `ping()`, `get_version()`, `card_type()`, `set_auto_mode()`, `halt()`, `rf_power()`, `store_keys()` |
| **Command line** | `mifarepy info / scan / watch / read / write / dump / restore / access`, `--keys FILE`; dumps in JSON, raw `.bin`/`.mfd` and MIFARE Classic Tool `.mct` |
| **Zero-config** | `MifareReader.auto()` finds the reader; errors name the problem and suggest a fix (`AuthenticationError`, `NoCardError`, …) |
| **Testing without hardware** | Built-in simulated reader and cards that enforce access bits, plus pytest fixtures (`mifare_reader`, `mifare_card`) |
| **Debugging** | `enable_wire_trace()` prints every GNetPlus frame, decoded, with keys hidden |
| **Developer experience** | Context manager, full type hints (`py.typed`) |

## Supported hardware

| Reader | Connection |
|---|---|
| PROMAG **PCR310U** | USB (PL2303 USB-serial) |
| PROMAG **MF5** / **MF10** | RS-232 / TTL serial |
| Other GNetPlus® readers | Serial |

Cards: MIFARE Classic 1K and 4K (13.56 MHz, ISO/IEC 14443 A).
See [Hardware & setup](https://sparkdrago05.github.io/mifarepy/hardware/) for finding the
port, Linux permissions and troubleshooting.

## Documentation

- [Usage guide](https://sparkdrago05.github.io/mifarepy/usage/): every feature with examples
- [MIFARE Classic primer](https://sparkdrago05.github.io/mifarepy/mifare-classic/): sectors, keys, access bits, value blocks
- [Keys and access bits](https://sparkdrago05.github.io/mifarepy/keys-and-access/): change keys safely, key files, per-card keys
- [GNetPlus protocol reference](https://sparkdrago05.github.io/mifarepy/protocol/): frame format, CRC, commands, error codes
- [Testing without hardware](https://sparkdrago05.github.io/mifarepy/testing/): simulator and pytest fixtures
- [API reference](https://sparkdrago05.github.io/mifarepy/api/)
- [Examples](examples/): nine runnable scripts

## Security

Please report vulnerabilities privately. See [SECURITY.md](SECURITY.md).

## Contributing

Bug reports, ideas and pull requests are welcome. See the
[contributing guide](https://sparkdrago05.github.io/mifarepy/contributing/).

```bash
git clone https://github.com/SparkDrago05/mifarepy.git && cd mifarepy
pip install -e ".[dev]"
pytest && ruff check . && mypy
```

## Credits & license

Maintained by **Spark Drago** ([@SparkDrago05](https://github.com/SparkDrago05)).

mifarepy began as a Python 3 port of [gnetplus](https://github.com/harishpillay/gnetplus)
(© 2013 Red Hat Inc., by Chow Loong Jin and Harish Pillay) and has since grown into a
separate library.

Licensed under the **GNU Lesser General Public License v3.0 or later**. See
[LICENSE](LICENSE) and [COPYING](COPYING).

MIFARE® is a registered trademark of NXP B.V., and GNetPlus® is a trademark of GIGA-TMS Inc.
mifarepy is an independent project and is not affiliated with either company.
