-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.go
65 lines (52 loc) · 1.44 KB
/
main.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
package gocodicefiscale
import (
"strings"
"regexp"
)
func ValidCF(input string) bool {
PATTERN := `(?:[B-DF-HJ-NP-TV-Z](?:[AEIOU]{2}|[AEIOU]X)|[AEIOU]{2}[AEIOUX]|[B-DF-HJ-NP-TV-Z]{2}[A-Z]){2}[\dLMNP-V]{2}(?:[A-EHLMPR-T](?:[04LQ][1-9MNP-V]|[1256LMRS][\dLMNP-V])|[DHPS][37PT][0L]|[ACELMRT][37PT][01LM])(?:[A-MZ][1-9MNP-V][\dLMNP-V]{2}|[A-M][0L](?:[\dLMNP-V][1-9MNP-V]|[1-9MNP-V][0L]))[A-Z]`
input = strings.TrimSpace(input)
if len(input) != 16 {
return false
}
match, err := regexp.MatchString(PATTERN, input)
if err != nil || !match {
return false
}
// checksum
cfInput := input[:15]
controlCode := input[15]
checksum := computeChecksum(cfInput)
if checksum != controlCode {
return false
}
return true
}
func computeChecksum(cf string) uint8 {
digits := "0123456789"
chars := "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
evenControlcode := make(map[int32]int)
for idx, char := range digits {
evenControlcode[char] = idx
}
for idx, char := range chars {
evenControlcode[char] = idx
}
values := []int{1, 0, 5, 7, 9, 13, 15, 17, 19, 21, 2, 4, 18, 20, 11, 3, 6, 8, 12, 14, 16, 10, 22, 25, 24, 23}
odd_controlcode := make(map[int32]int)
for idx, char := range digits {
odd_controlcode[char] = values[idx]
}
for idx, char := range chars {
odd_controlcode[char] = values[idx]
}
code := 0
for idx, char := range cf {
if idx%2 == 0 {
code += odd_controlcode[char]
} else {
code += evenControlcode[char]
}
}
return chars[code%26]
}