Git Product home page Git Product logo

luhn's Introduction

Luhn algorithm

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)

Character mapping

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.

Pros and cons

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 and import

Install with:

go get -u github.com/decadenza/luhn

Import as:

import "github.com/decadenza/luhn"

Basic example

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.

References

luhn's People

Contributors

decadenza avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.