forked from gogetth/sscard
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfunc.go
78 lines (64 loc) · 1.72 KB
/
func.go
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
package sscard
import (
"fmt"
)
// Transmitter is an interface that wrap the command to communicate with smart card via application protocol data unit (APDU) according to ISO/IEC 7816.
type Transmitter interface {
Transmit([]byte) ([]byte, error)
}
// APDUGetRsp Send list of APDU and get last command response
// ispadzeroOptional is optional(default = true) to replace adpu tail section
func APDUGetRsp(card Transmitter, apducmds [][]byte, ispadzeroOptional ...bool) ([]byte, error) {
var resp []byte
ispadzero := true
if len(ispadzeroOptional) > 0 {
ispadzero = ispadzeroOptional[0]
}
// Send command APDU: apducmds
for _, apducmd := range apducmds {
rsp, err := card.Transmit(apducmd)
if err != nil {
fmt.Println("Error Transmit:", err)
return nil, err
}
//printRsp(rsp)
resp = rsp
}
// pad zero
if ispadzero == true {
dlen := len(resp)
resp[dlen-2] = 0
}
return resp, nil
}
// APDUGetBlockRsp Send list of APDU and append all response
func APDUGetBlockRsp(scardCard Transmitter, apducmds [][]byte, apducmdRsp []byte) ([]byte, error) {
var respBlock []byte
card := scardCard
for _, apducmd1 := range apducmds {
// Send command APDU: apducmd1
rsp, err := card.Transmit(apducmd1)
if err != nil {
fmt.Println("Error Transmit:", err)
return nil, err
}
// printRsp(rsp)
// Send command APDU: apducmdRsp
rsp, err = card.Transmit(apducmdRsp)
if err != nil {
fmt.Println("Error Transmit:", err)
return nil, err
}
//printRsp(rsp)
respBlock = append(respBlock, rsp[:len(rsp)-2]...)
}
// fmt.Printf("% 2X\n", respBlock)
return respBlock, nil
}
func printRsp(rsp []byte) {
fmt.Println("resp: ", rsp)
for i := 0; i < len(rsp)-2; i++ {
fmt.Printf("%c", rsp[i])
}
fmt.Println()
}