forked from frozzare/go-personnummer
-
-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathpersonnummer.go
396 lines (326 loc) · 8.1 KB
/
personnummer.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
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
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
package personnummer
import (
"errors"
"fmt"
"math"
"strconv"
"strings"
"time"
"unicode"
)
const (
lengthWithoutCentury = 10
lengthWithCentury = 12
)
var (
errInvalidSecurityNumber = errors.New("Invalid swedish personal identity number")
monthDays = map[int]int{
1: 31,
3: 31,
4: 30,
5: 31,
6: 30,
7: 31,
8: 31,
9: 30,
10: 31,
11: 30,
12: 31,
}
now = time.Now
rule3 = [...]int{0, 2, 4, 6, 8, 1, 3, 5, 7, 9}
interimLetters = []rune("TRSUWXJKLMN")
)
// charsToDigit converts char bytes to a digit
// example: ['1', '1'] => 11
func charsToDigit(chars []byte) int {
l := len(chars)
r := 0
for i, c := range chars {
p := int((c - '0'))
for j := 0; j < l-i-1; j++ {
p *= 10
}
r += p
}
return r
}
func runeInSlice(a rune, list []rune) bool {
for _, b := range list {
if b == a {
return true
}
}
return false
}
// getCleanNumber will return clean numbers.
func getCleanNumber(in string) []byte {
cleanNumber := make([]byte, 0, len(in))
for _, c := range in {
if c == '+' {
continue
}
if c == '-' {
continue
}
if !runeInSlice(c, interimLetters) {
if c > '9' {
return nil
}
if c < '0' {
return nil
}
}
cleanNumber = append(cleanNumber, byte(c))
}
return cleanNumber
}
// getCoOrdinationDay will return co-ordination day.
func getCoOrdinationDay(day []byte) []byte {
d := charsToDigit(day)
if d < 60 {
return day
}
d -= 60
if d < 10 {
return []byte{'0', byte(d) + '0'}
}
return []byte{
byte(d)/10 + '0',
byte(d)%10 + '0',
}
}
// luhn will test if the given string is a valid luhn string.
func luhn(s []byte) bool {
for _, c := range s {
if !unicode.IsNumber(rune(c)) {
return false
}
}
odd := len(s) & 1
var sum int
for i, c := range s {
if i&1 == odd {
sum += rule3[c-'0']
} else {
sum += int(c - '0')
}
}
return sum%10 == 0
}
// toString converts int to string.
func toString(in interface{}) string {
switch v := in.(type) {
case int, int32, int64, uint, uint32, uint64:
return fmt.Sprint(v)
case string:
return v
default:
return ""
}
}
// input time without centry.
func validateTime(time []byte) bool {
length := len(time)
date := charsToDigit(time[length-2 : length])
month := charsToDigit(time[length-4 : length-2])
if month != 2 {
days, ok := monthDays[month]
if !ok {
return false
}
return date <= days
}
year := charsToDigit(time[:length-4])
leapYear := year%4 == 0 && year%100 != 0 || year%400 == 0
if leapYear {
return date <= 29
}
return date <= 28
}
// Personnummer represents the personnummer struct.
type Personnummer struct {
Century string
FullYear string
Year string
Month string
Day string
Sep string
Num string
Check string
leapYear bool
coordinationNumber bool
}
// Options represents the personnummer options.
type Options struct {
AllowInterimNumber bool
DisableCoordinationNumber bool
}
// New parse a Swedish personal identity numbers and returns a new struct or a error.
func New(pin string, options ...*Options) (*Personnummer, error) {
p := &Personnummer{}
o := &Options{}
if len(options) > 0 {
o = options[0]
}
if err := p.parse(pin, o); err != nil {
return nil, err
}
return p, nil
}
// parse Swedish personal identity numbers and set struct properpties or return a error.
func (p *Personnummer) parse(pin string, options *Options) error {
var century, year, num, check string
if pin == "" {
return errInvalidSecurityNumber
}
dateBytes := getCleanNumber(pin)
switch len(dateBytes) {
case lengthWithCentury:
century = string(dateBytes[0:2])
year = string(dateBytes[2:4])
num = string(dateBytes[8:11])
check = string(dateBytes[11:])
dateBytes = dateBytes[2:8]
break
case lengthWithoutCentury:
year = string(dateBytes[0:2])
num = string(dateBytes[6:9])
check = string(dateBytes[9:])
dateBytes = dateBytes[0:6]
break
default:
return errInvalidSecurityNumber
}
if num == "000" {
return errInvalidSecurityNumber
}
length := len(dateBytes)
day := charsToDigit(dateBytes[length-2 : length])
month := charsToDigit(dateBytes[length-4 : length-2])
if month != 2 {
if _, ok := monthDays[month]; !ok {
return errInvalidSecurityNumber
}
}
plus := strings.Contains(pin, "+")
p.Century = century
p.Year = year
p.FullYear = toString(century + year)
p.Check = check
p.Num = num
p.Sep = "-"
p.Day = toString(fmt.Sprintf("%02d", day))
p.Month = toString(fmt.Sprintf("%02d", month))
if p.Century == "" {
year := charsToDigit(dateBytes[:length-4])
var baseYear int
if plus {
baseYear = now().Year() - 100
p.Sep = "+"
} else {
baseYear = now().Year()
}
centuryStr := strconv.Itoa((baseYear - ((baseYear - year) % 100)))
century, err := strconv.Atoi(centuryStr[0:2])
if err != nil {
return err
}
p.Century = toString(century)
p.FullYear = toString(p.Century + p.Year)
} else {
fullYear, err := strconv.Atoi(century + year)
if err != nil {
return err
}
if now().Year()-fullYear < 100 {
p.Sep = "-"
} else {
p.Sep = "+"
}
}
if !p.valid() {
return errInvalidSecurityNumber
}
if p.IsCoordinationNumber() && options.DisableCoordinationNumber {
return errInvalidSecurityNumber
}
if p.IsInterimNumber() && !options.AllowInterimNumber {
return errInvalidSecurityNumber
}
return nil
}
// Valid will validate Swedish personal identity numbers.
func (p *Personnummer) valid() bool {
num := p.Num
if p.IsInterimNumber() {
num = "1" + p.Num[1:]
}
pin := fmt.Sprintf("%s%s%s%s%s%s", p.Century, p.Year, p.Month, p.Day, num, p.Check)
bytes := []byte(pin)
if !luhn(bytes[2:]) {
return false
}
var dateBytes = append(bytes[:6], getCoOrdinationDay(bytes[6:8])...)
return validateTime(dateBytes)
}
// Format a Swedish personal identity number as one of the official formats,
// a long format or a short format.
func (p *Personnummer) Format(longFormat ...bool) (string, error) {
if len(longFormat) > 0 && longFormat[0] {
return fmt.Sprintf("%s%s%s%s%s%s", p.Century, p.Year, p.Month, p.Day, p.Num, p.Check), nil
}
return fmt.Sprintf("%s%s%s%s%s%s", p.Year, p.Month, p.Day, p.Sep, p.Num, p.Check), nil
}
// GetDate returns the date from a Swedish personal identity number.
func (p *Personnummer) GetDate() time.Time {
ageDay := charsToDigit([]byte(p.Day))
if p.IsCoordinationNumber() {
ageDay = ageDay - 60
}
fullYear := charsToDigit([]byte(p.FullYear))
month := charsToDigit([]byte(p.Month))
return time.Date(fullYear, time.Month(month), ageDay, 0, 0, 0, 0, time.UTC)
}
// GetAge returns the age from a Swedish personal identity number.
func (p *Personnummer) GetAge() int {
t := p.GetDate()
a := math.Floor(float64(now().Sub(t)/1e6) / 3.15576e+10)
return int(a)
}
// IsCoordinationNumber determine if a Swedish personal identity number is a coordination number or not.
// Returns true if it's a coordination number.
func (p *Personnummer) IsCoordinationNumber() bool {
day := charsToDigit([]byte(p.Day)) - 60
str := fmt.Sprintf("%s%s%s", p.Century, p.Year, p.Month)
if day < 10 {
str += fmt.Sprintf("0%d", day)
} else {
str += fmt.Sprintf("%d", day)
}
return validateTime([]byte(str))
}
// IsInterimNumber determine if a Swedish personal identity number is a interim number or not.
// Returns true if it's a interim number.
func (p *Personnummer) IsInterimNumber() bool {
return runeInSlice(rune(p.Num[0]), interimLetters)
}
// IsFemale checks if a Swedish personal identity number is for a female.
func (p *Personnummer) IsFemale() bool {
return !p.IsMale()
}
// IsMale checks if a Swedish personal identity number is for a male.
// The second argument should be a boolean
func (p *Personnummer) IsMale() bool {
sexDigit := int(p.Num[2])
return sexDigit%2 == 1
}
// Valid will validate Swedish personal identity numbers
func Valid(pin string, options ...*Options) bool {
_, err := Parse(pin, options...)
return err == nil
}
// Parse Swedish personal identity numbers and return a new struct.
func Parse(pin string, options ...*Options) (*Personnummer, error) {
return New(pin, options...)
}