https://github.com/jancajthaml-go/luhn
luhn check-digit validator and generator
https://github.com/jancajthaml-go/luhn
check-digit check-digits checksum luhn luhn-algorithm transposition
Last synced: 3 months ago
JSON representation
luhn check-digit validator and generator
- Host: GitHub
- URL: https://github.com/jancajthaml-go/luhn
- Owner: jancajthaml-go
- License: unlicense
- Created: 2017-08-03T06:53:23.000Z (over 8 years ago)
- Default Branch: master
- Last Pushed: 2021-02-18T07:51:43.000Z (about 5 years ago)
- Last Synced: 2025-08-14T17:54:10.706Z (9 months ago)
- Topics: check-digit, check-digits, checksum, luhn, luhn-algorithm, transposition
- Language: Go
- Homepage:
- Size: 26.4 KB
- Stars: 2
- Watchers: 1
- Forks: 3
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
## luhn check-digit mod10 algorithm
[](https://goreportcard.com/report/jancajthaml-go/luhn)
Algorithm will detect any single-digit error, as well as almost all transpositions of adjacent digits. It will not, however, detect transposition of the two-digit sequence 09 to 90 (or vice versa).
It is not intended to be a cryptographically secure hash function. It is mostly used for pre-flight credit card number validation as specified in [ISO/IEC 7812-1:2015](http://www.iso.org/iso/catalogue_detail?csnumber=66011)
### Usage ###
```
import "github.com/jancajthaml-go/luhn"
ok := luhn.Validate("00123014764700968325")
digit, error := luhn.Digit("x")
checksum := luhn.Generate("1")
```
### Performance ###
```
BenchmarkLuhnSmall 200000000 13.9 ns/op 0 B/op 0 allocs/op
BenchmarkLuhnLarge 50000000 31.8 ns/op 0 B/op 0 allocs/op
BenchmarkLuhnSmallParallel 300000000 4.06 ns/op 0 B/op 0 allocs/op
BenchmarkLuhnLargeParallel 100000000 15.0 ns/op 0 B/op 0 allocs/op
```
verify your performance by running `make benchmark`
### Resources ###
* [Credit Card Validation - Check Digits](https://web.eecs.umich.edu/~bartlett/credit_card_number.html)
* [Wikipedia - Luhn algorithm](https://en.wikipedia.org/wiki/Luhn_algorithm)