pythonbeid 0.1.0__py3-none-any.whl
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.
- pythonbeid/__init__.py +0 -0
- pythonbeid/card_reader.py +232 -0
- pythonbeid-0.1.0.dist-info/LICENSE +21 -0
- pythonbeid-0.1.0.dist-info/METADATA +74 -0
- pythonbeid-0.1.0.dist-info/RECORD +9 -0
- pythonbeid-0.1.0.dist-info/WHEEL +5 -0
- pythonbeid-0.1.0.dist-info/top_level.txt +2 -0
- tests/__init__.py +0 -0
- tests/test_card_reader.py +11 -0
pythonbeid/__init__.py
ADDED
|
File without changes
|
|
@@ -0,0 +1,232 @@
|
|
|
1
|
+
#!/usr/bin/env python3
|
|
2
|
+
# -*- coding: utf-8 -*-
|
|
3
|
+
"""
|
|
4
|
+
Card Reader Script
|
|
5
|
+
|
|
6
|
+
This script reads information from a smart card using a card reader. It can retrieve card information, address, and optionally photo data.
|
|
7
|
+
|
|
8
|
+
Dependencies:
|
|
9
|
+
- pyscard (python-smartcard)
|
|
10
|
+
|
|
11
|
+
Usage:
|
|
12
|
+
Run the script and it will attempt to read the card information and print it.
|
|
13
|
+
|
|
14
|
+
Author:
|
|
15
|
+
Your Name (youremail@example.com)
|
|
16
|
+
"""
|
|
17
|
+
|
|
18
|
+
from datetime import datetime
|
|
19
|
+
from pprint import pprint
|
|
20
|
+
from smartcard.System import readers
|
|
21
|
+
from smartcard.Exceptions import NoCardException, CardConnectionException
|
|
22
|
+
|
|
23
|
+
MAP_MOIS = {
|
|
24
|
+
"JANV": "01", "JAN": "01",
|
|
25
|
+
"FEVR": "02", "FEV": "02",
|
|
26
|
+
"MARS": "03", "MAR": "03",
|
|
27
|
+
"AVRI": "04", "AVR": "04",
|
|
28
|
+
"MAI": "05",
|
|
29
|
+
"JUIN": "06",
|
|
30
|
+
"JUIL": "07",
|
|
31
|
+
"AOUT": "08", "AOU": "08",
|
|
32
|
+
"SEPT": "09", "SEP": "09",
|
|
33
|
+
"OCTO": "10", "OCT": "10",
|
|
34
|
+
"NOVE": "11", "NOV": "11",
|
|
35
|
+
"DECE": "12", "DEC": "12"
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
ID = [0x3F, 0x00, 0xDF, 0x01, 0x40, 0x31]
|
|
39
|
+
ADDRESS = [0x3F, 0x00, 0xDF, 0x01, 0x40, 0x33]
|
|
40
|
+
PHOTO = [0x3F, 0x00, 0xDF, 0x01, 0x40, 0x35]
|
|
41
|
+
|
|
42
|
+
class CardReader:
|
|
43
|
+
"""
|
|
44
|
+
A class to read information from a smart card using a card reader.
|
|
45
|
+
|
|
46
|
+
Methods
|
|
47
|
+
-------
|
|
48
|
+
read_informations(photo=False)
|
|
49
|
+
Reads card information, including optional photo data.
|
|
50
|
+
"""
|
|
51
|
+
|
|
52
|
+
def __init__(self) -> None:
|
|
53
|
+
"""
|
|
54
|
+
Initializes the CardReader by connecting to the first available card reader.
|
|
55
|
+
"""
|
|
56
|
+
try:
|
|
57
|
+
self._cnx = readers()[0].createConnection()
|
|
58
|
+
self._cnx.connect()
|
|
59
|
+
except IndexError:
|
|
60
|
+
raise RuntimeError("Pas de lecteur disponible")
|
|
61
|
+
except NoCardException:
|
|
62
|
+
raise RuntimeError("Pas de carte disponible")
|
|
63
|
+
except CardConnectionException as e:
|
|
64
|
+
raise RuntimeError(f"Erreur de connexion à la carte: {e}")
|
|
65
|
+
|
|
66
|
+
def _send_apdu(self, apdu):
|
|
67
|
+
"""
|
|
68
|
+
Sends an APDU command to the card and returns the response.
|
|
69
|
+
|
|
70
|
+
Parameters
|
|
71
|
+
----------
|
|
72
|
+
apdu : list
|
|
73
|
+
The APDU command to send.
|
|
74
|
+
|
|
75
|
+
Returns
|
|
76
|
+
-------
|
|
77
|
+
tuple
|
|
78
|
+
The response data, SW1, and SW2 status bytes.
|
|
79
|
+
"""
|
|
80
|
+
response, sw1, sw2 = self._cnx.transmit(apdu)
|
|
81
|
+
return response, sw1, sw2
|
|
82
|
+
|
|
83
|
+
def _read_data(self, file_id):
|
|
84
|
+
"""
|
|
85
|
+
Reads data from the card file specified by the file_id.
|
|
86
|
+
|
|
87
|
+
Parameters
|
|
88
|
+
----------
|
|
89
|
+
file_id : list
|
|
90
|
+
The file identifier to select and read.
|
|
91
|
+
|
|
92
|
+
Returns
|
|
93
|
+
-------
|
|
94
|
+
tuple
|
|
95
|
+
The data read from the file, SW1, and SW2 status bytes.
|
|
96
|
+
"""
|
|
97
|
+
cmd = [0x00, 0xA4, 0x08, 0x0C, len(file_id)] + file_id
|
|
98
|
+
_, sw1, sw2 = self._send_apdu(cmd)
|
|
99
|
+
if sw1 == 0x6C:
|
|
100
|
+
cmd = [0x00, 0xB0, 0x00, 0x00, sw2]
|
|
101
|
+
else:
|
|
102
|
+
cmd = [0x00, 0xB0, 0x00, 0x00, 256]
|
|
103
|
+
data, sw1, sw2 = self._send_apdu(cmd)
|
|
104
|
+
if sw1 == 0x6C:
|
|
105
|
+
cmd = [0x00, 0xB0, 0x00, 0x00, sw2]
|
|
106
|
+
data, sw1, sw2 = self._send_apdu(cmd)
|
|
107
|
+
return data, sw1, sw2
|
|
108
|
+
|
|
109
|
+
def _parse_information(self, data, num_info_limit):
|
|
110
|
+
"""
|
|
111
|
+
Parses the information from the raw data read from the card.
|
|
112
|
+
|
|
113
|
+
Parameters
|
|
114
|
+
----------
|
|
115
|
+
data : list
|
|
116
|
+
The raw data read from the card.
|
|
117
|
+
num_info_limit : int
|
|
118
|
+
The number of information fields to parse.
|
|
119
|
+
|
|
120
|
+
Returns
|
|
121
|
+
-------
|
|
122
|
+
list
|
|
123
|
+
The parsed information fields as strings.
|
|
124
|
+
"""
|
|
125
|
+
idx = 0
|
|
126
|
+
infos = []
|
|
127
|
+
while len(infos) <= num_info_limit:
|
|
128
|
+
num_info = data[idx]
|
|
129
|
+
idx += 1
|
|
130
|
+
len_info = data[idx]
|
|
131
|
+
idx += 1
|
|
132
|
+
chaine_bytes = data[idx:idx + len_info]
|
|
133
|
+
idx += len_info
|
|
134
|
+
try:
|
|
135
|
+
infos.append(bytes(chaine_bytes).decode("utf-8"))
|
|
136
|
+
except UnicodeDecodeError:
|
|
137
|
+
infos.append("")
|
|
138
|
+
return infos
|
|
139
|
+
|
|
140
|
+
def _parse_date(self, date_str):
|
|
141
|
+
"""
|
|
142
|
+
Parses a date string in the format 'DD MON YYYY'.
|
|
143
|
+
|
|
144
|
+
Parameters
|
|
145
|
+
----------
|
|
146
|
+
date_str : str
|
|
147
|
+
The date string to parse.
|
|
148
|
+
|
|
149
|
+
Returns
|
|
150
|
+
-------
|
|
151
|
+
datetime
|
|
152
|
+
The parsed date as a datetime object.
|
|
153
|
+
"""
|
|
154
|
+
jour, mois, annee = date_str.split()
|
|
155
|
+
mois = MAP_MOIS.get(mois, "01")
|
|
156
|
+
return datetime.strptime(f"{jour}/{mois}/{annee}", "%d/%m/%Y")
|
|
157
|
+
|
|
158
|
+
def read_informations(self, photo=False):
|
|
159
|
+
"""
|
|
160
|
+
Reads and returns the card information, including optional photo data.
|
|
161
|
+
|
|
162
|
+
Parameters
|
|
163
|
+
----------
|
|
164
|
+
photo : bool, optional
|
|
165
|
+
Whether to read the photo data (default is False).
|
|
166
|
+
|
|
167
|
+
Returns
|
|
168
|
+
-------
|
|
169
|
+
dict
|
|
170
|
+
The card information including number, validity dates, personal details, and optionally the photo.
|
|
171
|
+
"""
|
|
172
|
+
data, sw1, sw2 = self._read_data(ID)
|
|
173
|
+
infos = self._parse_information(data, 12)
|
|
174
|
+
|
|
175
|
+
informations = {
|
|
176
|
+
"num_carte": infos[0],
|
|
177
|
+
"debut_val": datetime.strptime(infos[2], "%d.%m.%Y"),
|
|
178
|
+
"fin_val": datetime.strptime(infos[3], "%d.%m.%Y"),
|
|
179
|
+
"commune_delivrance": infos[4],
|
|
180
|
+
"num_nat": infos[5],
|
|
181
|
+
"nom": infos[6],
|
|
182
|
+
"prenoms": infos[7],
|
|
183
|
+
"suffixe": infos[8],
|
|
184
|
+
"nationalite": infos[9],
|
|
185
|
+
"lieu_naissance": infos[10],
|
|
186
|
+
"date_naissance": self._parse_date(infos[11]),
|
|
187
|
+
"sexe": infos[12],
|
|
188
|
+
}
|
|
189
|
+
|
|
190
|
+
data, sw1, sw2 = self._read_data(ADDRESS)
|
|
191
|
+
address_infos = self._parse_information(data, 2)
|
|
192
|
+
|
|
193
|
+
informations["adresse"] = address_infos[0]
|
|
194
|
+
informations["code_postal"] = address_infos[1]
|
|
195
|
+
informations["localite"] = address_infos[2]
|
|
196
|
+
|
|
197
|
+
if photo:
|
|
198
|
+
photo_data = self._read_photo()
|
|
199
|
+
informations["photo"] = photo_data
|
|
200
|
+
|
|
201
|
+
for attribute, value in informations.items():
|
|
202
|
+
setattr(self, attribute, value)
|
|
203
|
+
|
|
204
|
+
return informations
|
|
205
|
+
|
|
206
|
+
def _read_photo(self):
|
|
207
|
+
"""
|
|
208
|
+
Reads the photo data from the card.
|
|
209
|
+
|
|
210
|
+
Returns
|
|
211
|
+
-------
|
|
212
|
+
bytearray
|
|
213
|
+
The photo data as a bytearray.
|
|
214
|
+
"""
|
|
215
|
+
data, sw1, sw2 = self._read_data(PHOTO)
|
|
216
|
+
photo_bytes = []
|
|
217
|
+
offset = 0
|
|
218
|
+
while sw1 == 0x90:
|
|
219
|
+
cmd = [0x00, 0xB0, offset, 0x00, 256]
|
|
220
|
+
data, sw1, sw2 = self._send_apdu(cmd)
|
|
221
|
+
photo_bytes.extend(data)
|
|
222
|
+
offset += 1
|
|
223
|
+
if sw1 == 0x6C:
|
|
224
|
+
offset -= 1
|
|
225
|
+
cmd = [0x00, 0xB0, offset, 0x00, sw2]
|
|
226
|
+
data, sw1, sw2 = self._send_apdu(cmd)
|
|
227
|
+
photo_bytes.extend(data)
|
|
228
|
+
return bytearray(photo_bytes)
|
|
229
|
+
|
|
230
|
+
if __name__ == '__main__':
|
|
231
|
+
cr = CardReader()
|
|
232
|
+
pprint(cr.read_informations(False))
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2024 Fabien Toune
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
|
@@ -0,0 +1,74 @@
|
|
|
1
|
+
Metadata-Version: 2.1
|
|
2
|
+
Name: pythonbeid
|
|
3
|
+
Version: 0.1.0
|
|
4
|
+
Summary: Un module pour lire les informations des cartes d'identité belges
|
|
5
|
+
Home-page: https://github.com/Lapin-Blanc/pythonbeid
|
|
6
|
+
Author: Fabien Toune
|
|
7
|
+
Author-email: fabien.toune@gmail.com
|
|
8
|
+
Classifier: Programming Language :: Python :: 3
|
|
9
|
+
Classifier: License :: OSI Approved :: MIT License
|
|
10
|
+
Classifier: Operating System :: OS Independent
|
|
11
|
+
Requires-Python: >=3.6
|
|
12
|
+
Description-Content-Type: text/markdown
|
|
13
|
+
License-File: LICENSE
|
|
14
|
+
Requires-Dist: pyscard
|
|
15
|
+
|
|
16
|
+
|
|
17
|
+
# PythonBEID
|
|
18
|
+
|
|
19
|
+
PythonBEID est un module Python pour lire les informations essentielles des cartes d'identité belge à l'aide d'un lecteur de cartes et de la bibliothèque `pyscard`.
|
|
20
|
+
|
|
21
|
+
## Installation
|
|
22
|
+
|
|
23
|
+
Vous pouvez installer ce module via pip :
|
|
24
|
+
|
|
25
|
+
```bash
|
|
26
|
+
pip install pythonbeid
|
|
27
|
+
```
|
|
28
|
+
|
|
29
|
+
## Utilisation
|
|
30
|
+
|
|
31
|
+
Voici un exemple simple d'utilisation du module `pythonbeid` pour lire les informations d'une carte :
|
|
32
|
+
|
|
33
|
+
```python
|
|
34
|
+
from pythonbeid.card_reader import CardReader
|
|
35
|
+
from pprint import pprint
|
|
36
|
+
|
|
37
|
+
def main():
|
|
38
|
+
try:
|
|
39
|
+
cr = CardReader()
|
|
40
|
+
informations = cr.read_informations(photo=False)
|
|
41
|
+
pprint(informations)
|
|
42
|
+
except RuntimeError as e:
|
|
43
|
+
print(f"Erreur: {e}")
|
|
44
|
+
|
|
45
|
+
if __name__ == "__main__":
|
|
46
|
+
main()
|
|
47
|
+
```
|
|
48
|
+
|
|
49
|
+
## Dépendances
|
|
50
|
+
|
|
51
|
+
Ce module nécessite la bibliothèque suivante :
|
|
52
|
+
- `pyscard`
|
|
53
|
+
|
|
54
|
+
Vous pouvez installer les dépendances avec pip :
|
|
55
|
+
|
|
56
|
+
```bash
|
|
57
|
+
pip install -r requirements.txt
|
|
58
|
+
```
|
|
59
|
+
|
|
60
|
+
## Tests
|
|
61
|
+
|
|
62
|
+
Les tests unitaires sont situés dans le répertoire `tests`. Vous pouvez exécuter les tests avec `unittest` :
|
|
63
|
+
|
|
64
|
+
```bash
|
|
65
|
+
python -m unittest discover -s tests
|
|
66
|
+
```
|
|
67
|
+
|
|
68
|
+
## Contribuer
|
|
69
|
+
|
|
70
|
+
Les contributions et améliorations sont les bienvenues !
|
|
71
|
+
|
|
72
|
+
## Licence
|
|
73
|
+
|
|
74
|
+
Ce projet est sous licence MIT. Voir le fichier `LICENSE` pour plus de détails.
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
pythonbeid/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
|
|
2
|
+
pythonbeid/card_reader.py,sha256=DBdVrwTPfxdk6cOsA3iVq1myY3K1oQo0Zfs_c-KlLZw,7062
|
|
3
|
+
tests/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
|
|
4
|
+
tests/test_card_reader.py,sha256=S-WhbD8ldB_bdlELJ58LFxjW1Sybj2oKzsujFM97v3Y,300
|
|
5
|
+
pythonbeid-0.1.0.dist-info/LICENSE,sha256=8xOq3W-9w8IgGAWBbKMFTcX3fMQCV4YmgQsvC14CTPE,1090
|
|
6
|
+
pythonbeid-0.1.0.dist-info/METADATA,sha256=69kVEGb-4w3iibDYGV02DiLHzo4LtUyylCWzj-YDPKo,1804
|
|
7
|
+
pythonbeid-0.1.0.dist-info/WHEEL,sha256=cpQTJ5IWu9CdaPViMhC9YzF8gZuS5-vlfoFihTBC86A,91
|
|
8
|
+
pythonbeid-0.1.0.dist-info/top_level.txt,sha256=nwijJKuYdePBeJE_Z_Wy3dUL-h0iF2IgL5SbsVt2-AQ,17
|
|
9
|
+
pythonbeid-0.1.0.dist-info/RECORD,,
|
tests/__init__.py
ADDED
|
File without changes
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import unittest
|
|
2
|
+
from pythonbeid.card_reader import CardReader
|
|
3
|
+
|
|
4
|
+
class TestCardReader(unittest.TestCase):
|
|
5
|
+
def test_read_informations(self):
|
|
6
|
+
cr = CardReader()
|
|
7
|
+
info = cr.read_informations()
|
|
8
|
+
self.assertIsNotNone(info)
|
|
9
|
+
|
|
10
|
+
if __name__ == '__main__':
|
|
11
|
+
unittest.main()
|