/
chinaid.go
259 lines (231 loc) · 4.93 KB
/
chinaid.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
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
package chinaid
import (
"encoding/json"
"errors"
"fmt"
"strconv"
"unicode"
"unsafe"
)
// ErrInvalidChinaID error
var ErrInvalidChinaID = errors.New("invalid China ID")
// ChinaID Chinese identity card number
type ChinaID struct {
whole []byte
body []byte
addr []byte
birth []byte
seq []byte
sex int // 1 male / 0 female
check []byte
}
// Make18 generates an 18-character length number
func Make18(addr [6]byte, birth [8]byte, seq [3]byte) (*ChinaID, bool) {
body := string(addr[:]) + string(birth[:]) + string(seq[:])
if !isDigit(body, -1) {
return nil, false
}
c := &ChinaID{
body: []byte(body),
addr: addr[:],
birth: birth[:],
seq: seq[:],
}
c.check = []byte{c.createCheckCode()}
c.whole = append(c.body, c.check...)
c.setSex()
return c, true
}
// Make15 generates an 15-character length number
func Make15(addr [6]byte, birth [6]byte, seq [3]byte) (*ChinaID, bool) {
body := string(addr[:]) + string(birth[:]) + string(seq[:])
if !isDigit(body, -1) {
return nil, false
}
c := &ChinaID{
body: []byte(body),
addr: addr[:],
birth: birth[:],
seq: seq[:],
}
c.whole = c.body
c.setSex()
return c, true
}
// Validate judge whether chinese identity card number is valid.
func Validate(id string) bool {
l := len(id)
switch l {
case 15:
return true
case 18:
var sum int
for k, v := range id[:l-1] {
d, _ := strconv.Atoi(string(v))
sum += weight[k] * d
}
return validate[sum%11] == id[l-1]
default:
return false
}
}
// CheckCode generates check code of chinese identity card number.
func CheckCode(idBody string) (string, bool) {
if len(idBody) == 17 {
var sum int
for k, v := range idBody {
d, _ := strconv.Atoi(string(v))
sum += weight[k] * d
}
return string(validate[sum%11]), true
}
return "", false
}
// Parse parse chinese identity card number to *ChinaID
func Parse(id string) (*ChinaID, error) {
if !isDigit(id, 17) {
return nil, ErrInvalidChinaID
}
whole := []byte(id)
switch len(whole) {
case 18:
c := &ChinaID{
whole: whole,
body: whole[:17],
addr: whole[:6],
birth: whole[6:14],
seq: whole[14:17],
check: whole[17:],
}
if !c.validate() {
return nil, ErrInvalidChinaID
}
c.setSex()
return c, nil
case 15:
c := &ChinaID{
whole: whole,
body: whole,
addr: whole[:6],
birth: whole[6:12],
seq: whole[12:],
}
c.setSex()
return c, nil
default:
return nil, ErrInvalidChinaID
}
}
// String returns chinese identity card number
func (c *ChinaID) String() string {
return bytes2String(c.whole)
}
// MarshalJSON implements json.Marshaler
func (c *ChinaID) MarshalJSON() ([]byte, error) {
var m = map[string]interface{}{
"whole": c.String(),
"body": c.Body(),
"addr": c.Addr(),
"birth": c.Birth(),
"seq": c.Seq(),
"sex": c.sex,
}
if len(c.check) > 0 {
m["check"] = c.Check()
}
return json.Marshal(m)
}
// func (c *ChinaID) GoString() string {
// b, _ := c.MarshalJSON()
// return bytes2String(b)
// }
// Format implements fmt.Formatter
func (c *ChinaID) Format(f fmt.State, r rune) {
if r == 'v' && (f.Flag('+') || f.Flag('#')) {
b, _ := c.MarshalJSON()
f.Write(b)
return
}
f.Write(c.whole)
}
// Body returns main part of chinese identity card number
func (c *ChinaID) Body() string {
return bytes2String(c.body)
}
// Addr returns address code
func (c *ChinaID) Addr() string {
return bytes2String(c.addr)
}
// Birth returns date of birth
func (c *ChinaID) Birth() string {
return bytes2String(c.birth)
}
// Seq returns sequence number in the persons whose date of birth are same
func (c *ChinaID) Seq() string {
return bytes2String(c.seq)
}
// Check returns check code
func (c *ChinaID) Check() string {
return bytes2String(c.check)
}
// Sex returns sex
// 1 male / 0 female
func (c *ChinaID) Sex() int {
return c.sex
}
// SexText returns sex text, male or female
func (c *ChinaID) SexText() string {
if c.IsMale() {
return "male"
}
return "female"
}
// IsMale judge whether is male or not
func (c *ChinaID) IsMale() bool {
return c.sex == 1
}
// IsMale judge whether is female or not
func (c *ChinaID) IsFemale() bool {
return c.sex == 0
}
func (c *ChinaID) setSex() {
i, _ := strconv.Atoi(bytes2String(c.seq[2:]))
c.sex = i % 2
}
var (
weight = [17]int{7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2}
validate = [17]byte{'1', '0', 'X', '9', '8', '7', '6', '5', '4', '3', '2'}
)
func (c *ChinaID) createCheckCode() byte {
var sum int
for k, v := range c.body {
d, _ := strconv.Atoi(string(v))
sum += weight[k] * d
}
return validate[sum%11]
}
func (c *ChinaID) validate() bool {
return c.check[0] == c.createCheckCode()
}
func isDigit(s string, maxLen int) bool {
if maxLen < 0 {
for _, r := range s {
if !unicode.IsDigit(r) {
return false
}
}
} else {
for i, r := range s {
if i >= maxLen {
break
}
if !unicode.IsDigit(r) {
return false
}
}
}
return true
}
func bytes2String(b []byte) string {
return *(*string)(unsafe.Pointer(&b))
}