Skip to content

Commit

Permalink
Return false for bad luhn inputs
Browse files Browse the repository at this point in the history
  • Loading branch information
frozzare committed Jun 12, 2023
1 parent 618fa0d commit 11f7b7f
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 0 deletions.
7 changes: 7 additions & 0 deletions personnummer.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"strconv"
"strings"
"time"
"unicode"
)

const (
Expand Down Expand Up @@ -106,6 +107,12 @@ func getCoOrdinationDay(day []byte) []byte {

// luhn will test if the given string is a valid luhn string.
func luhn(s []byte) bool {
for _, c := range s {
if !unicode.IsNumber(rune(c)) {
return false
}
}

odd := len(s) & 1

var sum int
Expand Down
5 changes: 5 additions & 0 deletions personnummer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -224,6 +224,11 @@ func TestInterimNumbersInvalid(t *testing.T) {
}
}

func TestLuhn(t *testing.T) {
assert.True(t, luhn([]byte("1212121212")))
assert.False(t, luhn([]byte("12120111X3")))
}

func BenchmarkValid(b *testing.B) {
for i := 0; i < b.N; i++ {
Valid(testList[0].LongFormat)
Expand Down

0 comments on commit 11f7b7f

Please sign in to comment.