Collection of checksum algorithms on Go
Find a file
Alexander Kiryukhin dd42100aa2 Barcode and ISIN
2019-08-30 02:53:06 +03:00
barcode Barcode and ISIN 2019-08-30 02:53:06 +03:00
damm Barcode and ISIN 2019-08-30 02:53:06 +03:00
isin Barcode and ISIN 2019-08-30 02:53:06 +03:00
luhn Barcode and ISIN 2019-08-30 02:53:06 +03:00
verhoeff Added Damm algorithm 2019-08-30 01:43:12 +03:00
errors.go Barcode and ISIN 2019-08-30 02:53:06 +03:00
go.mod Initial 2019-08-30 01:26:24 +03:00
LICENSE Create LICENSE 2019-08-30 01:27:18 +03:00
README.md Barcode and ISIN 2019-08-30 02:53:06 +03:00

Collection of checksum algorithms on Go

Pure Go implementations.

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

Usage

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

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
}

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

Usage

import (
    "github.com/neonxp/checksum"
    "github.com/neonxp/checksum/luhn"
)
...
err := luhn.Check("4561261212345467")
switch err {
    case checksum.ErrInvalidNumber:
    // Not a number
    case checksum.ErrInvalidChecksum:
    // 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

Usage

import (
    "github.com/neonxp/checksum"
    "github.com/neonxp/checksum/verhoeff"
)
...
numberWithoutChecksum := "4561261212345467"
err := verhoeff.Check(number)
switch err {
    case checksum.ErrInvalidNumber:
    // Not a number
    case checksum.ErrInvalidChecksum:
    // Invalid checksum
    case nil:
    // Valid number
}

checksum, err := verhoeff.Generate(numberWithoutChecksum)
if err != nil {
    panic(err)
}
numberWithChecksum := numberWithoutChecksum + checksum
if err := verhoeff.Check(numberWithChecksum); err != nil {
    panic(err)
}

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

Usage

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)
}