pax_global_header00006660000000000000000000000064131605400510014505gustar00rootroot0000000000000052 comment=5b2abb343e70180dbf456397c5fd93f14471b08e luhn-2.0.0/000077500000000000000000000000001316054005100124525ustar00rootroot00000000000000luhn-2.0.0/LICENSE000066400000000000000000000020421316054005100134550ustar00rootroot00000000000000Copyright (C) 2014 Jakob Borg Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: - The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. luhn-2.0.0/luhn.go000066400000000000000000000031021316054005100137430ustar00rootroot00000000000000// Copyright (C) 2014 Jakob Borg // Package luhn generates and validates Luhn mod N check digits. package luhn import ( "fmt" "strings" ) // An alphabet is a string of N characters, representing the digits of a given // base N. type Alphabet string var ( Base32 Alphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZ234567" ) // Generate returns a check digit for the string s, which should be composed // of characters from the Alphabet a. func (a Alphabet) Generate(s string) (rune, error) { factor := 1 if len(s)%2 == 1 { factor = 2 } sum := 0 n := len(a) for i := range s { codepoint := strings.IndexByte(string(a), s[i]) if codepoint == -1 { return 0, fmt.Errorf("Digit %q not valid in alphabet %q", s[i], a) } addend := factor * codepoint if factor == 2 { factor = 1 } else { factor = 2 } addend = (addend / n) + (addend % n) sum += addend } remainder := sum % n checkCodepoint := (n - remainder) % n return rune(a[checkCodepoint]), nil } // Validate returns true if the last character of the string s is correct, for // a string s composed of characters in the alphabet a. func (a Alphabet) Validate(s string) bool { t := s[:len(s)-1] c, err := a.Generate(t) if err != nil { return false } return rune(s[len(s)-1]) == c } // NewAlphabet converts the given string an an Alphabet, verifying that it // is correct. func NewAlphabet(s string) (Alphabet, error) { cm := make(map[byte]bool, len(s)) for i := range s { if cm[s[i]] { return "", fmt.Errorf("Digit %q non-unique in alphabet %q", s[i], s) } cm[s[i]] = true } return Alphabet(s), nil } luhn-2.0.0/luhn_test.go000066400000000000000000000027521316054005100150140ustar00rootroot00000000000000// Copyright (C) 2014 Jakob Borg package luhn_test import ( "testing" "github.com/calmh/luhn" ) func TestGenerate(t *testing.T) { // Base 6 Luhn a := luhn.Alphabet("abcdef") c, err := a.Generate("abcdef") if err != nil { t.Fatal(err) } if c != 'e' { t.Errorf("Incorrect check digit %c != e", c) } // Base 10 Luhn a = luhn.Alphabet("0123456789") c, err = a.Generate("7992739871") if err != nil { t.Fatal(err) } if c != '3' { t.Errorf("Incorrect check digit %c != 3", c) } } func TestInvalidString(t *testing.T) { a := luhn.Alphabet("ABC") _, err := a.Generate("7992739871") t.Log(err) if err == nil { t.Error("Unexpected nil error") } } func TestBadAlphabet(t *testing.T) { _, err := luhn.NewAlphabet("01234566789") t.Log(err) if err == nil { t.Error("Unexpected nil error") } } func TestValidate(t *testing.T) { a := luhn.Alphabet("abcdef") if !a.Validate("abcdefe") { t.Errorf("Incorrect validation response for abcdefe") } if a.Validate("abcdefd") { t.Errorf("Incorrect validation response for abcdefd") } } func TestValidateRosetta(t *testing.T) { // http://rosettacode.org/wiki/Luhn_test_of_credit_card_numbers a := luhn.Alphabet("0123456789") cases := []struct { v string ok bool }{ {"49927398716", true}, {"49927398717", false}, {"1234567812345678", false}, {"1234567812345670", true}, } for _, tc := range cases { if res := a.Validate(tc.v); res != tc.ok { t.Errorf("Validate(%q) => %v, expected %v", tc.v, res, tc.ok) } } }