aboutsummaryrefslogtreecommitdiff
path: root/README.md
blob: e74c5108129b0570b5318372513bad1bb7da1c1e (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
# 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](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
}
```

## 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
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](https://en.wikipedia.org/wiki/Verhoeff_algorithm)

### Usage

```golang
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](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)
}
```