-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathThaiCIDHelper.py
352 lines (273 loc) · 13.5 KB
/
ThaiCIDHelper.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
"""
Class ThaiCIDHelper
ไฟล์ประกอบของการอ่านข้อมูลบัตรประชาชน
วรเพชร เรืองพรวิสุทธิ์
09/01/2567
Python 3.11.5
pyscard==2.0.7
update : 10/01/2567 15:00 น
"""
import time, codecs , subprocess
# pyscard 2.0.7
from smartcard.System import readers
from smartcard.util import toHexString
from DataThaiCID import *
from imgToClipboard import copyImageToClipboard
####- --------------------------------------------------
# Thai CID Smartcard Helper
####- --------------------------------------------------
class ThaiCIDHelper():
def __init__(self,
apduSELECT = [0x00, 0xA4, 0x04, 0x00, 0x08],
apduTHCard = [0xA0, 0x00, 0x00, 0x00, 0x54, 0x48, 0x00, 0x01],
showThaiDate=True):
# Initialize
self.cardReaderList = readers() # PC/SC
self.cardReader = None
self.cardReaderIndex = -1
self.apduSELECT = apduSELECT
self.apduTHCard = apduTHCard
self.apduRequest = []
self.ATR = ""
self.showThaiDate = showThaiDate
self.lastError = ""
print (f'Reader: Available Count = {len(self.cardReaderList)}')
####- --------------------------------------------------
#### connectReader
####- --------------------------------------------------
def connectReader(self,index):
"""
connectReader ติดต่อเครื่องอ่านบัตร \n
พารามิเตอร์ :
index ลำดับของเครื่องอ่านบัตร \n
Default 0 (ตัวที่ 1) ** ถ้ามี ** \n
return >> Reader-connection , Connected Status
"""
print(f'Reader: Select ... [{index}] = [{self.cardReaderList[index]}]')
_HWcardReader = self.cardReaderList[index]
_connected = False
try:
# Create Connection
self.cardReader = _HWcardReader.createConnection()
# Reader Connection [OK]
self.cardReader.connect()
self.cardReaderIndex = index
_connected = True
print(f'Reader: Connected ... [{self.cardReaderList[index]}]')
except Exception as err:
self.lastError = f'{err}'
print(f'Connection : Error = {err}')
# 0x80100069 Unable to connect: The smart card has been removed,so that further communication is not possible
# Unable to connect with protocol: T0 or T1. The smart card is not responding to a reset.
if _connected == True:
### read ATR (format for storage cards)
# 3B 8N 80 01 {XX} {XX}
# 3B 8N 80 01 80 4F 0C A0 00 00 03 06 {SS} {NN} 00 00 00 00 {XX}
atr = self.cardReader.getATR()
self.ATR = toHexString(atr)
print (f"Reader: ATR = {self.ATR}")
### Check Version
# Get-Data command format
if (atr[0] == 0x3B & atr[1] == 0x67):
self.apduRequest = [0x00, 0xc0, 0x00, 0x01]
else:
self.apduRequest = [0x00, 0xc0, 0x00, 0x00]
return self.cardReader ,_connected
return None,False
####- --------------------------------------------------
#### readData
####- --------------------------------------------------
def readData(self,readPhoto=True,
saveText : SaveType = SaveType.FILE,
savePhoto: SaveType = SaveType.FILE):
"""
readData อ่านข้อมูลจากบัตร ตาม apdu ที่กำหนด \n
พารามิเตอร์ : \n
readPhoto อ่านรูปภาพ ? \n
saveText บันทึกข้อความ ? None-File-Clip \n
savePhoto บันทึกข้อมูลภาพ ? None-File-Clip \n
"""
start_time = time.time()
# เริ่มอ่านข้อมูลบัตร
# SELECT + CARD TYPE
data, sw1, sw2 = self.cardReader.transmit(self.apduSELECT + self.apduTHCard)
print ("Reader: Send `SELECT` Response = %02X %02X" % (sw1, sw2))
responseJson = []
_jsonThaiDesc , _Json4Dev , _JsonRawData = {},{},{}
_textThaiDesc , _textJson = "",""
##- -----------------------------------------------------
### Read Value
print("Reader: อ่านข้อมูล เริ่มแล้ว...")
apduCount = len(APDU_DATA)
for index,data in enumerate(APDU_DATA):
_apdu = searchDATAValue('key',data['key'],'apdu')
print('Reader: อ่าน ',data['desc'])
response = self.getValue(_apdu,data['type'])
##- ----------------------------------------------------
# make Json
_jsonThaiDesc[data['desc']] = response[0]
_Json4Dev[data['id']] = response[0]
#_JsonRawData['raw'] = response[1]
# make Text
if index == (apduCount-1):
_textThaiDesc += f'"{index}":"{data["desc"]}={response[0]}"\n'
_textJson += f'"{data["id"]}":"{response[0]}"\n'
else:
_textThaiDesc += f'"{index}":"{data["desc"]}={response[0]}",\n'
_textJson += f'"{data["id"]}":"{response[0]}",\n'
### make Json List
responseJson.append(_jsonThaiDesc)
responseJson.append(_Json4Dev)
# responseJson.append(_JsonRawData)
### Copy Text To Clipboard
if saveText == SaveType.CLIPBOARD:
print("Reader: บันทึกข้อมูลข้อความ [ไปคลิปบอร์ด] ...")
copyTextToClipboard(f'{_textThaiDesc}\n{_textJson}')
if saveText == SaveType.FILE:
print("Reader: บันทึกข้อมูลข้อความ [ลงไฟล์] ...")
### Save Text To File
filename = f"{responseJson[1]['CID']}.json"
with open(filename, "wb" ) as f:
f.write ('[{\n'.encode('utf-8'))
f.write (_textThaiDesc.encode('utf-8'))
f.write ('},\n'.encode('utf-8'))
f.write ('{\n'.encode('utf-8'))
f.write (_textJson.encode('utf-8'))
f.write ('}]'.encode('utf-8'))
f.close
##- -----------------------------------------------------
### Read Photo
if readPhoto:
print("Reader: อ่าน รูปภาพ...")
photoStr = []
for data in APDU_PHOTO:
_apdu = searchAPDUPhoto(data['key'])
photoStr += self.getPhoto(_apdu)
### Save Photo to File
if savePhoto == SaveType.FILE:
# use bytes แปลง [] เป็น Bytes ** ถ้าเป็น String จะ Error
print("Reader: บันทึกข้อมูลรูป [ลงไฟล์] ...")
imageData = bytes(photoStr)
filename = f"{responseJson[1]['CID']}.jpg"
with codecs.open(filename, "wb" ) as f:
f.write (imageData)
f.close
## Copy Photo to Clipboard
if savePhoto == SaveType.CLIPBOARD:
print("Reader: บันทึกข้อมูลรูป [ไปคลิปบอร์ด] ...")
copyImageToClipboard(filename)
##- -----------------------------------------------------
end_time = time.time()
elapsed_time = end_time - start_time
elapsed_str = time.strftime("%S.{}".format(str(elapsed_time % 1)[2:])[:6], time.gmtime(elapsed_time))
print(f"Reader: อ่านข้อมูล เสร็จแล้ว... [{elapsed_str} ms]")
####- --------------------------------------------------
#### getValue
####- --------------------------------------------------
def getValue(self,apdu,dataType):
"""
getValue อ่านข้อมูลจากบัตร ตาม APDU ที่กำหนด \n
พารามิเตอร์ : \n
apdu คำสั่ง APDU ของฟิลด์ที่ต้องการอ่าน \n
dataType ประเภทของข้อมูล เพื่อจัดรูปแบบตามต้องการ
"""
#print(f"Reader: Send Command")
_data, _sw1, sw2 = self.cardReader.transmit(apdu)
#print(f"Reader: Card Response1 >> Size= {sw2} ")
#ขอข้อมูล ขนาดที่บอกมา Size == sw2
rawdata, _sw1 , _sw2 = self.cardReader.transmit(self.apduRequest + [sw2])
#print(f"Reader: Card Response2 >> data")
text = self.encodeTextThai(rawdata)
if dataType == ThaiCIDDataType.ADDRESS:
# ข้อมูลที่อยู่
text = text.replace('#######',' ')
text = text.replace('######',' ')
text = text.replace('#####',' ')
text = text.replace('####',' ')
text = text.replace('###',' ')
text = text.replace('##',' ')
text = text.replace('#',' ')
data = text
elif dataType == ThaiCIDDataType.NAME:
# ข้อมูลชื่อ-นามสกุล ไทย และ อังกฤษ
text = text.replace('##',' ')
text = text.replace('#','')
data = text
elif dataType == ThaiCIDDataType.DATE:
# ปีพ.ศ. + เดือน + ปี ระวังข้อมูลที่มีแต่ พ.ศ.
if self.showThaiDate == True:
# dd/mm/2567
_date = textToThaiDate(text)
else:
# 2024-mm-dd
_date = textToEngDate(text)
data = _date
elif dataType == ThaiCIDDataType.GENDER:
# '-' , 'ชาย' ,'หญิง'
data = GENDER[int(text)]
elif dataType == ThaiCIDDataType.RELIGION:
# '-' , 'พุทธ' ,'อิสลาม'
data = RELIGION[int(text)]
elif dataType == ThaiCIDDataType.DOCNUMBER:
# เลขใต้รูปภาพ
data = setformatDocNumber(text)
else:
data = text
return [data, rawdata]
def getPhoto(self,apdu):
"""
getPhoto อ่านข้อมูลรูปภาพจากบัตร ตาม APDU ที่กำหนด \n
พารามิเตอร์ : \n
apdu คำสั่ง APDU ของฟิลด์ที่ต้องการอ่าน \n
"""
_ , _ , sw2 = self.cardReader.transmit(apdu)
#ขอข้อมูล ขนาดที่บอกมา Size == sw2
rawdata, _ , _ = self.cardReader.transmit(self.apduRequest + [sw2])
return rawdata
def encodeTextThai(self,data):
# แปลงเป็นตัวอักษรไทย TIS-620
result = bytes(data).decode('tis-620')
# ตัดช่องว่าง trim
return result.strip();
## External Method ##
def textToThaiDate(txt:str):
# ปีพ.ศ. + เดือน + ปี
# บัตร คนมีปัญหา มีแต่ปีเกิด
# บัตร ตลอดชีพ มีแต่ปีเกิด
if len(txt) == 8:
_year = txt[:4] # 1-4
_month = txt[4:6] # 5-6
_day = txt[6:8] # 7-8
# แปลงเป็น วัน/เดือน/ปี
return f"{_day}/{_month}/{_year}"
else:
return txt
def textToEngDate(txt:str):
# ปีพ.ศ. + เดือน + ปี
if len(txt) == 8:
_year = txt[:4] # 1-4
_month = txt[4:6] # 5-6
_day = txt[6:8] # 7-8
_yearEN = int(_year) - 543
# แปลงเป็น ปี - เดือน - วัน
return f"{_yearEN}-{_month}-{_day}"
else:
return txt
def setformatDocNumber(txt:str):
# 0000-00-00000000
t1 = txt[:4] # 1-4
t2 = txt[4:6] # 5-6
t3 = txt[6:] # 6+
return f"{t1}-{t2}-{t3}"
def searchDATAValue(type,value,response):
for data in APDU_DATA:
if data[type] == value:
return data[response]
return None
def searchAPDUPhoto(value):
for data in APDU_PHOTO:
if data['key'] == value:
return data['apdu']
return None
def copyTextToClipboard(txt):
return subprocess.run("clip", input=txt, check=True, encoding="tis-620")