-
Notifications
You must be signed in to change notification settings - Fork 0
/
cos.go
83 lines (66 loc) · 1.5 KB
/
cos.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
79
80
81
82
83
package healthcard
import (
"fmt"
"strings"
)
type APDUHeader struct {
Cla byte
Ins byte
P1 byte
P2 byte
}
var DO_FCP = BerTag{0x62}
var DO_AID = BerTag{0x84}
func (a *APDUHeader) Bytes() []byte {
return []byte{a.Cla, a.Ins, a.P1, a.P2}
}
func (a *APDUHeader) String() string {
return fmt.Sprintf("%02X %02X %02X %02X", a.Cla, a.Ins, a.P1, a.P2)
}
var selectMF = APDUHeader{0x00, 0xA4, 0x04, 0x04}
var selectDF = APDUHeader{0x00, 0xA4, 0x04, 0x0C}
type APDU struct {
Header APDUHeader
Body []byte
}
func (a *APDU) Bytes() []byte {
return append(a.Header.Bytes(), a.Body...)
}
func (a *APDU) String() string {
return fmt.Sprintf("%s %02X", a.Header.String(), a.Body)
}
// prettyHex returns a pretty-printed hex string of the given data.
// each byte is separated by a space.
func prettyHex(data []byte) string {
var sb strings.Builder
for ind, b := range data {
sb.WriteString(fmt.Sprintf("%02X", b))
if ind < len(data)-1 {
sb.WriteString(" ")
}
}
return sb.String()
}
type apduBuilder struct {
apdu APDU
}
func (a *apduBuilder) header(header APDUHeader) {
a.apdu.Header = header
a.apdu.Body = make([]byte, 0)
}
func (a *apduBuilder) Body(body ...byte) *apduBuilder {
a.apdu.Body = body
return a
}
func (a *apduBuilder) RawBytes(b ...byte) *apduBuilder {
a.apdu.Body = append(a.apdu.Body, b...)
return a
}
func (a *apduBuilder) APDU() APDU {
return a.apdu
}
func Command(header APDUHeader) *apduBuilder {
apduBuilder := apduBuilder{}
apduBuilder.header(header)
return &apduBuilder
}