-
Notifications
You must be signed in to change notification settings - Fork 1
/
utils.go
44 lines (35 loc) · 1.06 KB
/
utils.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
package panitizer
import "regexp"
var spacesAndDashesRegexp = regexp.MustCompile(`[ -]*`)
var numberRegexp = regexp.MustCompile(`\d`)
func normalize(s string) string {
return spacesAndDashesRegexp.ReplaceAllString(s, "")
}
// maskCreditCardNumber will return a masked credit card number with
func maskCreditCardNumberWithSymbol(number, symbol string) string {
numLen := len(number)
if numLen < 4 {
return number
}
last4 := numLen - 4
return numberRegexp.ReplaceAllString(number, symbol)[:last4] + number[last4:]
}
// PassesLuhnCheck will return true if the passed in credit card number passes the Luhn Algorithm.
func PassesLuhnCheck(s string) bool {
// Based on http://rosettacode.org/wiki/Luhn_test_of_credit_card_numbers#Go
number := normalize(s)
var mapping = [...]int{0, 2, 4, 6, 8, 1, 3, 5, 7, 9}
sum := 0
odd := len(number)%2 == 1
for i, c := range number {
if c < '0' || c > '9' { // Check that we only have digits
return false
}
if (i%2 == 1) == odd {
sum += mapping[c-'0']
} else {
sum += int(c - '0')
}
}
return sum%10 == 0
}