checksum

package module
v0.0.0-...-dd42100 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Aug 29, 2019 License: MIT Imports: 1 Imported by: 1

README

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

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrInvalidNumber   = errors.New("invalid number")   // number contains non numeric symbols
	ErrInvalidChecksum = errors.New("invalid checksum") // number not correct by luhn algorithm
	ErrNotImplemented  = errors.New("not implemented")  // not implemented currently
)

Functions

This section is empty.

Types

This section is empty.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL