aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--README.md57
-rw-r--r--errors.go8
-rw-r--r--go.mod3
-rw-r--r--luhn/luhn.go32
-rw-r--r--luhn/luhn_test.go20
-rw-r--r--verhoeff/verhoeff.go69
-rw-r--r--verhoeff/verhoeff_test.go34
7 files changed, 223 insertions, 0 deletions
diff --git a/README.md b/README.md
new file mode 100644
index 0000000..abb8c6c
--- /dev/null
+++ b/README.md
@@ -0,0 +1,57 @@
+# Go implementation of collection of checksum algorithms
+
+## Luhn algorithm
+
+> The Luhn algorithm or Luhn formula, also known as the "modulus 10" or "mod 10" algorithm, named after its creator, IBM scientist Hans Peter Luhn, is a simple checksum formula used to validate a variety of identification numbers, such as credit card numbers, IMEI numbers, National Provider Identifier numbers in the United States, Canadian Social Insurance Numbers, Israel ID Numbers, Greek Social Security Numbers (ΑΜΚΑ), and survey codes appearing on McDonald's, Taco Bell, and Tractor Supply Co. receipts.
+
+[Wikipedia](https://en.wikipedia.org/wiki/Luhn_algorithm)
+
+Simple implementation on pure Go.
+
+### Usage
+
+```golang
+import "github.com/neonxp/checksum/luhn"
+...
+err := luhn.Check("4561261212345467")
+switch err {
+ case luhn.ErrInvalidNumber:
+ // Not a number
+ case luhn.ErrInvalidChecksum:
+ // Invalid checksum
+ case nil:
+ // Valid number
+}
+```
+
+## Verhoeff algorithm
+
+> The Verhoeff algorithm is a checksum formula for error detection developed by the Dutch mathematician Jacobus Verhoeff and was first published in 1969. It was the first decimal check digit algorithm which detects all single-digit errors, and all transposition errors involving two adjacent digits, which was at the time thought impossible with such a code.
+
+[Wikipedia](https://en.wikipedia.org/wiki/Verhoeff_algorithm)
+
+### Usage
+
+```golang
+import "github.com/neonxp/checksum/verhoeff"
+...
+numberWithoutChecksum := "4561261212345467"
+err := verhoeff.Check(number)
+switch err {
+ case luhn.ErrInvalidNumber:
+ // Not a number
+ case luhn.ErrInvalidChecksum:
+ // Invalid checksum
+ case nil:
+ // Valid number
+}
+
+checksum, err := verhoeff.Generate(numberWithoutChecksum)
+if err != nil {
+ panic(err)
+}
+numberWithChecksum := numberWithoutChecksum + checksum
+if err := verhoeff.Generate(numberWithChecksum); err != nil {
+ panic(err)
+}
+```
diff --git a/errors.go b/errors.go
new file mode 100644
index 0000000..fd57ccc
--- /dev/null
+++ b/errors.go
@@ -0,0 +1,8 @@
+package checksum
+
+import "errors"
+
+var (
+ ErrInvalidNumber = errors.New("invalid number") // number contains non numeric symbols
+ ErrInvalidChecksum = errors.New("invalid checksum") // number not correct by luhn algorithm
+)
diff --git a/go.mod b/go.mod
new file mode 100644
index 0000000..479ac1e
--- /dev/null
+++ b/go.mod
@@ -0,0 +1,3 @@
+module github.com/neonxp/checksum
+
+go 1.12
diff --git a/luhn/luhn.go b/luhn/luhn.go
new file mode 100644
index 0000000..0724320
--- /dev/null
+++ b/luhn/luhn.go
@@ -0,0 +1,32 @@
+package luhn
+
+import (
+ "strconv"
+
+ "github.com/neonxp/checksum"
+)
+
+// Check number is correct by luhn algorithm
+func Check(number string) error {
+ mod := len(number) % 2
+ sum := 0
+ for i, ch := range number {
+ num, err := strconv.Atoi(string(ch))
+ if err != nil {
+ return checksum.ErrInvalidNumber
+ }
+ if i%2 == mod {
+ if num < 5 {
+ sum += num * 2
+ } else {
+ sum += num*2 - 9
+ }
+ } else {
+ sum += num
+ }
+ }
+ if sum%10 != 0 {
+ return checksum.ErrInvalidChecksum
+ }
+ return nil
+}
diff --git a/luhn/luhn_test.go b/luhn/luhn_test.go
new file mode 100644
index 0000000..ea36119
--- /dev/null
+++ b/luhn/luhn_test.go
@@ -0,0 +1,20 @@
+package luhn
+
+import (
+ "testing"
+
+ "github.com/neonxp/checksum"
+)
+
+func TestLuhn(t *testing.T) {
+ samples := map[string]error{
+ "4561261212345464": checksum.ErrInvalidChecksum,
+ "A561261212345464": checksum.ErrInvalidNumber,
+ "4561261212345467": nil,
+ }
+ for num, result := range samples {
+ if err := Check(num); err != result {
+ t.Errorf("Expected %+v actual %+v", result, err)
+ }
+ }
+}
diff --git a/verhoeff/verhoeff.go b/verhoeff/verhoeff.go
new file mode 100644
index 0000000..259a7df
--- /dev/null
+++ b/verhoeff/verhoeff.go
@@ -0,0 +1,69 @@
+package verhoeff
+
+import (
+ "strconv"
+ "strings"
+
+ "github.com/neonxp/checksum"
+)
+
+var d = [10][10]int{
+ {0, 1, 2, 3, 4, 5, 6, 7, 8, 9},
+ {1, 2, 3, 4, 0, 6, 7, 8, 9, 5},
+ {2, 3, 4, 0, 1, 7, 8, 9, 5, 6},
+ {3, 4, 0, 1, 2, 8, 9, 5, 6, 7},
+ {4, 0, 1, 2, 3, 9, 5, 6, 7, 8},
+ {5, 9, 8, 7, 6, 0, 4, 3, 2, 1},
+ {6, 5, 9, 8, 7, 1, 0, 4, 3, 2},
+ {7, 6, 5, 9, 8, 2, 1, 0, 4, 3},
+ {8, 7, 6, 5, 9, 3, 2, 1, 0, 4},
+ {9, 8, 7, 6, 5, 4, 3, 2, 1, 0},
+}
+
+var p = [8][10]int{
+ {0, 1, 2, 3, 4, 5, 6, 7, 8, 9},
+ {1, 5, 7, 6, 2, 8, 3, 0, 9, 4},
+ {5, 8, 0, 3, 7, 9, 6, 1, 4, 2},
+ {8, 9, 1, 6, 0, 4, 3, 5, 2, 7},
+ {9, 4, 5, 3, 1, 2, 6, 8, 7, 0},
+ {4, 2, 8, 6, 5, 7, 3, 9, 0, 1},
+ {2, 7, 9, 3, 8, 0, 6, 4, 1, 5},
+ {7, 0, 4, 6, 9, 1, 3, 2, 5, 8},
+}
+
+var inv = [10]int{0, 4, 3, 2, 1, 5, 6, 7, 8, 9}
+
+// Check number is correct by luhn algorithm
+func Check(number string) error {
+ c := 0
+ numbers := strings.Split(number, "")
+ l := len(numbers)
+ for i := l; i > 0; i-- {
+ ch := numbers[i-1]
+ num, err := strconv.Atoi(string(ch))
+ if err != nil {
+ return checksum.ErrInvalidNumber
+ }
+ c = d[c][p[(l-i)&7][num]]
+ }
+ if c != 0 {
+ return checksum.ErrInvalidChecksum
+ }
+ return nil
+}
+
+// Generate checksum (must be added to number at right)
+func Generate(number string) (string, error) {
+ c := 0
+ numbers := strings.Split(number, "")
+ l := len(numbers)
+ for i := l; i > 0; i-- {
+ ch := numbers[i-1]
+ num, err := strconv.Atoi(string(ch))
+ if err != nil {
+ return "", checksum.ErrInvalidNumber
+ }
+ c = d[c][p[(l-i+1)&7][num]]
+ }
+ return strconv.Itoa(inv[c]), nil
+}
diff --git a/verhoeff/verhoeff_test.go b/verhoeff/verhoeff_test.go
new file mode 100644
index 0000000..7678b1f
--- /dev/null
+++ b/verhoeff/verhoeff_test.go
@@ -0,0 +1,34 @@
+package verhoeff
+
+import (
+ "testing"
+
+ "github.com/neonxp/checksum"
+)
+
+func TestVerhoeff(t *testing.T) {
+ samples := map[string]error{
+ "4561261212345464": checksum.ErrInvalidChecksum,
+ "A561261212345464": checksum.ErrInvalidNumber,
+ "758722": nil,
+ "123451": nil,
+ "1428570": nil,
+ "1234567890120": nil,
+ "84736430954837284567892": nil,
+ }
+ for num, result := range samples {
+ if err := Check(num); err != result {
+ t.Errorf("Expected %+v actual %+v", result, err)
+ }
+ }
+
+ num := "4561261212345467"
+ checksum, err := Generate(num)
+ if err != nil {
+ t.Error(err)
+ }
+ numberWithChecksum := num + checksum
+ if err := Check(numberWithChecksum); err != nil {
+ t.Errorf("Expected no error actual %+v", err)
+ }
+}