Skip to content

A package that implements the luhn algorithm for validation, calculation and generation

License

Notifications You must be signed in to change notification settings

ShiraazMoollatjie/goluhn

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

16 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

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

About

A package that implements the luhn algorithm for validation, calculation and generation

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages