checksum/README.md

147 lines
3.9 KiB
Markdown
Raw Permalink Normal View History

2019-08-30 01:27:58 +03:00
# Collection of checksum algorithms on Go
2019-08-30 01:26:24 +03:00
2019-08-30 01:29:20 +03:00
Pure Go implementations.
2019-08-30 02:53:06 +03:00
## ISIN
> An International Securities Identification Number (ISIN) uniquely identifies a security. Its structure is defined in ISO 6166. The ISIN code is a 12-character alphanumeric code that serves for uniform identification of a security through normalization of the assigned National Number, where one exists, at trading and settlement.
[Wikipedia](https://en.wikipedia.org/wiki/International_Securities_Identification_Number)
### Usage
```golang
import (
"github.com/neonxp/checksum"
"github.com/neonxp/checksum/isin"
)
...
err := isin.Check("4000000000006") //
switch err {
case checksum.ErrInvalidNumber:
// Not a number
case checksum.ErrInvalidChecksum:
// Invalid checksum
case nil:
// Valid number
}
```
## Barcode EAN-8, UPC-12, EAN-13
Validate barcode's checksum
### Usage
```golang
import (
"github.com/neonxp/checksum"
"github.com/neonxp/checksum/barcode"
)
...
err := barcode.Check("041689300494") // UPC-12 barcode
switch err {
case checksum.ErrInvalidNumber:
// Not a number
case checksum.ErrInvalidChecksum:
// Invalid checksum
case nil:
// Valid number
}
```
2019-08-30 01:26:24 +03:00
## Luhn algorithm
> 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, such as credit card numbers, IMEI numbers, National Provider Identifier numbers in the United States, Canadian Social Insurance Numbers, Israel ID Numbers, Greek Social Security Numbers (ΑΜΚΑ), and survey codes appearing on McDonald's, Taco Bell, and Tractor Supply Co. receipts.
[Wikipedia](https://en.wikipedia.org/wiki/Luhn_algorithm)
### Usage
```golang
2019-08-30 01:43:12 +03:00
import (
"github.com/neonxp/checksum"
"github.com/neonxp/checksum/luhn"
)
2019-08-30 01:26:24 +03:00
...
err := luhn.Check("4561261212345467")
switch err {
2019-08-30 01:43:12 +03:00
case checksum.ErrInvalidNumber:
2019-08-30 01:26:24 +03:00
// Not a number
2019-08-30 01:43:12 +03:00
case checksum.ErrInvalidChecksum:
2019-08-30 01:26:24 +03:00
// Invalid checksum
case nil:
// Valid number
}
```
## Verhoeff algorithm
> The Verhoeff algorithm is a checksum formula for error detection developed by the Dutch mathematician Jacobus Verhoeff and was first published in 1969. It was the first decimal check digit algorithm which detects all single-digit errors, and all transposition errors involving two adjacent digits, which was at the time thought impossible with such a code.
[Wikipedia](https://en.wikipedia.org/wiki/Verhoeff_algorithm)
### Usage
```golang
2019-08-30 01:43:12 +03:00
import (
"github.com/neonxp/checksum"
"github.com/neonxp/checksum/verhoeff"
)
2019-08-30 01:26:24 +03:00
...
numberWithoutChecksum := "4561261212345467"
err := verhoeff.Check(number)
switch err {
2019-08-30 01:43:12 +03:00
case checksum.ErrInvalidNumber:
2019-08-30 01:26:24 +03:00
// Not a number
2019-08-30 01:43:12 +03:00
case checksum.ErrInvalidChecksum:
2019-08-30 01:26:24 +03:00
// Invalid checksum
case nil:
// Valid number
}
checksum, err := verhoeff.Generate(numberWithoutChecksum)
if err != nil {
panic(err)
}
numberWithChecksum := numberWithoutChecksum + checksum
2019-08-30 01:29:20 +03:00
if err := verhoeff.Check(numberWithChecksum); err != nil {
2019-08-30 01:26:24 +03:00
panic(err)
}
```
2019-08-30 01:43:12 +03:00
# Damm algorithm
> In error detection, the Damm algorithm is a check digit algorithm that detects all single-digit errors and all adjacent transposition errors. It was presented by H. Michael Damm in 2004.
[Wikipedia](https://en.wikipedia.org/wiki/Damm_algorithm)
### Usage
```golang
import (
"github.com/neonxp/checksum"
"github.com/neonxp/checksum/damm"
)
...
numberWithoutChecksum := "4561261212345467"
err := damm.Check(number)
switch err {
case checksum.ErrInvalidNumber:
// Not a number
case checksum.ErrInvalidChecksum:
// Invalid checksum
case nil:
// Valid number
}
checksum, err := damm.Generate(numberWithoutChecksum)
if err != nil {
panic(err)
}
numberWithChecksum := numberWithoutChecksum + checksum
if err := damm.Check(numberWithChecksum); err != nil {
panic(err)
}
```