-
Notifications
You must be signed in to change notification settings - Fork 0
/
common.go
32 lines (26 loc) · 848 Bytes
/
common.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
package kid
import "errors"
var (
// KID has an invalid length
ErrorInvalidCodeLength = errors.New("invalid code length")
// KID has an invalid character (not a digit)
ErrorInvalidNumber = errors.New("kid contains an invalid number")
)
// Generator interface for KID generation
type Generator interface {
Generate(code string) (rune, error)
}
// Verify code using a generator (MOD10 or MOD11)
func Verify(code string, generator Generator) bool {
if len(code) < 2 {
return false
}
i, err := generator.Generate(code[:len(code)-1])
return err == nil && i == rune(code[len(code)-1])
}
// A KID is a code between 2 and 25 digits.
// Excluding the check digit, the range is [1, 24].
func validateKidLengthWithoutCheckDigit(code string) (int, bool) {
codeLength := len(code)
return codeLength, codeLength >= 1 && codeLength <= 24
}