-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathluhn.go
61 lines (53 loc) · 1.26 KB
/
luhn.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
// Package luhn implements the Luhn algorithm, which is commonly used to add
// checksums to credit cards and identity codes.
//
// See https://en.wikipedia.org/wiki/Luhn_algorithm
package luhn
func sum(n int, parity bool) (total int) {
for n > 0 {
d := n % 10
if parity {
d *= 2
if d > 9 {
d -= 9
}
}
total += d
parity = !parity
n /= 10
}
return total
}
// IsValidInt returns true if the input is valid according to the Luhn algorithm.
func IsValidInt(n int) bool {
return sum(n, false)%10 == 0
}
// CheckDigitInt returns the digit needed to make its input valid.
func CheckDigitInt(n int) int {
return sum(n, true) * 9 % 10
}
func sumb(b []byte, parity bool) (total int) {
for n := len(b) - 1; n >= 0; n-- {
d := b[n] - '0'
if parity {
d *= 2
if d > 9 {
d -= 9
}
}
total += int(d)
parity = !parity
}
return total
}
// IsValid returns true if the input is valid according to the Luhn algorithm.
//
// Note that IsValid assumes input characters are all digits. Output is
// undefined for unexpected characters.
func IsValid(s string) bool {
return sumb([]byte(s), false)%10 == 0
}
// CheckDigit returns the digit needed to make its input valid.
func CheckDigit(s string) byte {
return byte(sumb([]byte(s), true)*9%10) + '0'
}