checkdigit: github.com/osamingo/checkdigit Index | Examples | Files

package checkdigit

import "github.com/osamingo/checkdigit"

Package checkdigit provides check digit algorithms and calculators written by Go.

Index

Examples

Package Files

checkdigit.go damm.go gtin.go isbn.go luhn.go verhoeff.go

Variables

var ErrInvalidArgument = errors.New("checkdigit: invalid argument")

ErrInvalidArgument is happening when given the wrong argument.

type Generator Uses

type Generator interface {
    Generate(seed string) (int, error)
}

A Generator generates a check digit by implemented algorithm or calculator.

type Provider Uses

type Provider interface {
    Verifier
    Generator
}

A Provider has Verifier and Generator interfaces.

func NewDamm Uses

func NewDamm() Provider

NewDamm returns a new Provider that implemented the Damm algorithm.

Code:

p := checkdigit.NewDamm()

const seed = "572"
cd, err := p.Generate(seed)
if err != nil {
    log.Fatalln("failed to generate check digit")
}

ok := p.Verify(seed + strconv.Itoa(cd))
fmt.Printf("seed: %s, check digit: %d, verify: %t\n", seed, cd, ok)

Output:

seed: 572, check digit: 4, verify: true

func NewEAN13 Uses

func NewEAN13() Provider

NewEAN13 returns a new Provider that implemented GTIN-13 with position correction calculator.

Code:

p := checkdigit.NewEAN13()

const seed = "590123412345"
cd, err := p.Generate(seed)
if err != nil {
    log.Fatalln("failed to generate check digit")
}

ok := p.Verify(seed + strconv.Itoa(cd))
fmt.Printf("seed: %s, check digit: %d, verify: %t\n", seed, cd, ok)

Output:

seed: 590123412345, check digit: 7, verify: true

func NewEAN8 Uses

func NewEAN8() Provider

NewEAN8 returns a new Provider that implemented GTIN-8 with position correction calculator.

Code:

p := checkdigit.NewEAN8()

const seed = "9638507"
cd, err := p.Generate(seed)
if err != nil {
    log.Fatalln("failed to generate check digit")
}

ok := p.Verify(seed + strconv.Itoa(cd))
fmt.Printf("seed: %s, check digit: %d, verify: %t\n", seed, cd, ok)

Output:

seed: 9638507, check digit: 4, verify: true

func NewISBN10 Uses

func NewISBN10() Provider

NewISBN10 returns a new Provider that implemented modulus 11 weight 10 to 2 calculator.

Code:

p := checkdigit.NewISBN10()

const seed = "155860832"
cd, err := p.Generate(seed)
if err != nil {
    log.Fatalln("failed to generate check digit")
}

digit := "X"
if cd != 10 {
    digit = strconv.Itoa(cd)
}

ok := p.Verify(seed + digit)
fmt.Printf("seed: %s, check digit: %s, verify: %t\n", seed, digit, ok)

Output:

seed: 155860832, check digit: X, verify: true

func NewISBN13 Uses

func NewISBN13() Provider

NewISBN13 returns a new Provider that implemented modulus 10 weight 3 calculator.

Code:

p := checkdigit.NewISBN13()

const seed = "978000271217"
cd, err := p.Generate(seed)
if err != nil {
    log.Fatalln("failed to generate check digit")
}

ok := p.Verify(seed + strconv.Itoa(cd))
fmt.Printf("seed: %s, check digit: %d, verify: %t\n", seed, cd, ok)

Output:

seed: 978000271217, check digit: 0, verify: true

func NewITF Uses

func NewITF() Provider

NewITF returns a new Provider that implemented GTIN-14 calculator.

Code:

p := checkdigit.NewITF()

const seed = "1456995111617"
cd, err := p.Generate(seed)
if err != nil {
    log.Fatalln("failed to generate check digit")
}

ok := p.Verify(seed + strconv.Itoa(cd))
fmt.Printf("seed: %s, check digit: %d, verify: %t\n", seed, cd, ok)

Output:

seed: 1456995111617, check digit: 6, verify: true

func NewJAN13 Uses

func NewJAN13() Provider

NewJAN13 returns a new Provider that implemented GTIN-13 with position correction calculator.

Code:

p := checkdigit.NewJAN13()

const seed = "456995111617"
cd, err := p.Generate(seed)
if err != nil {
    log.Fatalln("failed to generate check digit")
}

ok := p.Verify(seed + strconv.Itoa(cd))
fmt.Printf("seed: %s, check digit: %d, verify: %t\n", seed, cd, ok)

Output:

seed: 456995111617, check digit: 9, verify: true

func NewJAN8 Uses

func NewJAN8() Provider

NewJAN8 returns a new Provider that implemented GTIN-8 with position correction calculator.

Code:

p := checkdigit.NewJAN8()

const seed = "4996871"
cd, err := p.Generate(seed)
if err != nil {
    log.Fatalln("failed to generate check digit")
}

ok := p.Verify(seed + strconv.Itoa(cd))
fmt.Printf("seed: %s, check digit: %d, verify: %t\n", seed, cd, ok)

Output:

seed: 4996871, check digit: 2, verify: true

func NewLuhn Uses

func NewLuhn() Provider

NewLuhn returns a new Provider that implemented the Luhn algorithm.

Code:

p := checkdigit.NewLuhn()

const seed = "411111111111111"
cd, err := p.Generate(seed)
if err != nil {
    log.Fatalln("failed to generate check digit")
}

ok := p.Verify(seed + strconv.Itoa(cd))
fmt.Printf("seed: %s, check digit: %d, verify: %t\n", seed, cd, ok)

Output:

seed: 411111111111111, check digit: 1, verify: true

func NewSSCC Uses

func NewSSCC() Provider

NewSSCC returns a new Provider that implemented GTIN-18 calculator.

Code:

p := checkdigit.NewSSCC()

const seed = "04569951110000001"
cd, err := p.Generate(seed)
if err != nil {
    log.Fatalln("failed to generate check digit")
}

ok := p.Verify(seed + strconv.Itoa(cd))
fmt.Printf("seed: %s, check digit: %d, verify: %t\n", seed, cd, ok)

Output:

seed: 04569951110000001, check digit: 6, verify: true

func NewUPC Uses

func NewUPC() Provider

NewUPC returns a new Provider that implemented GTIN-12 with position correction calculator.

Code:

p := checkdigit.NewUPC()

const seed = "01234567890"
cd, err := p.Generate(seed)
if err != nil {
    log.Fatalln("failed to generate check digit")
}

ok := p.Verify(seed + strconv.Itoa(cd))
fmt.Printf("seed: %s, check digit: %d, verify: %t\n", seed, cd, ok)

Output:

seed: 01234567890, check digit: 5, verify: true

func NewVerhoeff Uses

func NewVerhoeff() Provider

NewVerhoeff returns a new Provider that implemented the Verhoeff algorithm.

Code:

p := checkdigit.NewVerhoeff()

const seed = "236"
cd, err := p.Generate(seed)
if err != nil {
    log.Fatalln("failed to generate check digit")
}

ok := p.Verify(seed + strconv.Itoa(cd))
fmt.Printf("seed: %s, check digit: %d, verify: %t\n", seed, cd, ok)

Output:

seed: 236, check digit: 3, verify: true

type Verifier Uses

type Verifier interface {
    Verify(code string) bool
}

A Verifier is verifying to code by implemented algorithm or calculator.

Package checkdigit imports 1 packages (graph). Updated 2019-12-26. Refresh now. Tools for package owners.