-
Notifications
You must be signed in to change notification settings - Fork 28
/
Copy pathvoterid.go
43 lines (35 loc) · 903 Bytes
/
voterid.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
package brdoc
import (
"fmt"
"strconv"
)
// IsVoterID verifies if the given string is a valid voter ID document.
func IsVoterID(doc string) bool {
// This function was based on the logic from [1]. It seems to be a bit
// sketchy, but it works for now.
// [1]: http://ghiorzi.org/DVnew.htm#e.
if !allDigit(doc) || len(doc) != 12 {
return false
}
docRune := []rune(doc)
docUF := fmt.Sprintf("%d%d", toInt(docRune[8]), toInt(docRune[9]))
docUFInt, _ := strconv.Atoi(docUF)
if docUFInt < 1 || docUFInt > 28 {
return false
}
sumA := 0
for i, digit := range doc[:len(doc)-4] {
sumA += toInt(digit) * (i + 2)
}
dv1 := dvMod11(sumA)
sumB := toInt(docRune[8])*7 + toInt(docRune[9])*8 + dv1*9
dv2 := dvMod11(sumB)
return dv1 == toInt(docRune[10]) && dv2 == toInt(docRune[11])
}
func dvMod11(num int) int {
mod := num % 11
if mod == 10 || mod == 11 {
return 0
}
return mod
}