goluhn

package module
v0.0.0-...-0d86158 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Oct 17, 2021 License: MIT Imports: 6 Imported by: 7

README

goluhn: A luhn number generator, validator and calculator in Go

Go Report Card Go Doc Build status

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.

goluhn is a package that is able to:

  • Validate a number to see if it is valid according to the luhn algorithm
  • Calculate luhn check digits for a given number
  • Generate random luhn numbers of any size

How to use goluhn

Validation

err := goluhn.Validate("1111222233334444")
if err != nil {
  return err
}

Calculation

The Calculate function returns the luhnCheckDigit, the luhnNumber (input number + check digit) or an error

cd, n, err := goluhn.Calculate("111122223333444")
if err != nil {
  return err
}

fmt.Printf("Check digit: %s\n", cd)
fmt.Printf("Luhn number: %s\n", n)

Generation

The Generate function returns a random luhn number of a provided size.

n, err := goluhn.Generate(9)
if err != nil {
  return err
}

fmt.Printf("Luhn number: %s\n", n)

References

Luhn algorithm on wikipedia

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Calculate

func Calculate(number string) (string, string, error)

Calculate returns luhn check digit and the provided string number with its luhn check digit appended.

func Generate

func Generate(length int) string

Generate will generate a valid luhn number of the provided length

func GenerateWithPrefix

func GenerateWithPrefix(prefix string, length int) string

Generate will generate a valid luhn number of the provided length with prefix

func Validate

func Validate(number string) error

Validate returns an error if the provided string does not pass the luhn check.

Types

This section is empty.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL