Implementation of Luhn Mod N algorithm for any base (modulus) divisible by 2 in the range from 2 to 36. Most used configurations are:
- Luhn Mod 10
- Luhn Mod 16 (hexadecimal)
- Luhn Mod 36 (all 0-9 and A-Z characters)
The character mapping is based on the standard strconv package.
Letter a
is mapped to 10 and all other letters are mapped subsequentially, up to letter z
which is mapped to 36.
The expected input is a string where each character is treated independently.
The Luhn algorithm will detect all single-digit errors, as well as almost all transpositions of adjacent digits. It will not, however, detect transposition of the two-digit sequence base-1-0 to 0-base-1 (or vice versa), e.g. swapping 09 with 90 in Mod 10. It will detect most of the possible twin errors, but not all of them (e.g. in Mod 10 it will not detect 22 ↔ 55, 33 ↔ 66 or 44 ↔ 77).
Install with:
go get -u github.com/decadenza/luhn
Import as:
import "github.com/decadenza/luhn"
package main
import (
"fmt"
"github.com/decadenza/luhn"
)
func main() {
base := 16
myPayload := "14FAD"
luhn, err := luhn.New(base)
if err != nil {
panic(err)
}
// Generate and concatenate checksum.
checksum, err := luhn.GetChecksum(myPayload)
if err != nil {
panic(err)
}
fullCode := myPayload + checksum
// Check its validity.
valid := luhn.IsValid(fullCode)
fmt.Printf("%s is valid? %t\n", fullCode, valid)
// A mistyping will not be valid.
wrongCode := "1AFAD" + checksum
valid = luhn.IsValid(wrongCode)
fmt.Printf("%s is valid? %t\n", wrongCode, valid)
}
Refer to test files for further examples.