Goluhn: A simple package for generating, calculating and verifying luhn numbers

Shiraaz Moollatjie
Back to blog


3 minute read

What is a luhn number

 

A luhn number is a number whose last digit satisfies the luhn checksum formula. It is named after its inventor Hans Peter Luhn. For example, in the luhn number 1111 2222 3333 4444, the last 4 digit is the luhn checksum digit of the number.

 

Intended use and application

 

The most popular use of a luhn number is a credit card number. Every credit card number satisfies the luhn algorithm. Luhn numbers are also used for other identification methods such as IMEI numbers, electricity meter numbers, various countries' ID numbers and so on.

 

The luhn check is mainly designed to protect against accidentally entered numbers, basically typos. It is not designed to protect against malicious checks. Therefore validation of these number is the main use case.

 

Using goluhn

 

Using goluhn is extremely simple. It exposes three functions Validate, Calculate and Generate.

 

To kick things off, we start with the package import:

 

 

Validate

 

We call the Validate function to validate whether a provided string is a luhn number. Invalid luhn strings return errors.

 

 

Calculation

 

This is used when we have a numeric string whose luhn check digit needs to be calculated.

 

 

This function will return the check digit as well as the original with the luhn digit appended to it.

 

Generate

 

The Generate function will generate a random luhn number of a provided length. This is useful for when you are generating your own numbers in your system. In the example below, we generate luhn numbers of length 16 (effectively a credit card number).

 

 

Random Thoughts

 

This project was quick and fun to implement. I decided to use strings over integers because it is possible to generate luhn numbers for numbers that are greater than the int64 type (this is fun to try out actually). There are no real numeric operations that apply to luhn numbers, so this also motivated me to use strings instead. Strings also allow for zero padded prefixed numbers to be generated.

 

The Validate function arguably could have returned a bool, error result. I don't really like returning that specific combination of types because either you have an error or the result of the function is free from errors which implies true. Additionally, I think that if you returned a boolean false in this specific case, you're very likely to return an error too.

References

 

The wikipedia entry

 

This article was originally published on dev.to. See more articles from Shiraaz here.

Avatar Shiraaz Moollatjie
Author

Shiraaz Moollatjie

Software Engineer at Luno (Level 11 Approved)

It’s never too late to get started

Buy, store and learn about Bitcoin, Ethereum, XRP and Litecoin now

Desktop Icon Apple App Store Logo Google Play Store Logo