2.1.2 • Published 1 year ago

@konfirm/iso7064 v2.1.2

Weekly downloads
23
License
MIT
Repository
github
Last release
1 year ago

Tests Release

ISO 7064

An implementation of check digit calculating algorithms described in the ISO/IEC 7064:2003 specification. Check digits are primarily used to quickly detect eratic input and are not intended for cryptographical purposes.

Installation

The ISO 7064 package is a scoped package, which means one needs to provide the scope (both in installation and use).

$ npm install --save @konfirm/iso7064

Usage

The ISO 7064 package provides several extendable classes of which all modulus-algorithms are ready for immediate use.

As of version 2.1 the ISO 7064 provides Typescript types and ES Modules.

Example

Using import (ES Modules, Typescript)

import { Mod97_10 } from '@konfirm/iso7064';

Or the CommonJS require

const { Mod97_10 } = require('@konfirm/iso7064');

Available exports

exportextendspurpose
ISO7064The common mechanics for all implementations
PureISO7064ISO7064Provide the pure modulus specification
HybridISO7064ISO7064Provide the hybrid modulus specification
Mod11_2PureISO7064The MOD 11-2 implementation
Mod11_10HybridISO7064The MOD 11,10 implementation
Mod27_26HybridISO7064The MOD 27,26 implementation
Mod37_2PureISO7064The MOD 37-2 implementation
Mod37_36HybridISO7064The MOD 37,36 implementation
Mod97_10PureISO7064The MOD 97-10 implementation
Mod661_26PureISO7064The MOD 661-26 implementation
Mod1271_36PureISO7064The MOD 1271-36 implementation

Methods

All algorithm implementations have the same methods, please refer to their respective documentation for the details on the exact in- and outputs.

methodinputoutputdescription
normalizestring\|numberstringthe normalized value used for checksum calculations
checksumstring\|numberstringthe single or double digit/character checksum
validatestring\|numberbooleanvalidate the provided string (including checksum)
generatestring\|numberstringcalculate and append the checksum to the (normalized) input
factoryobjectISO7064Create a new instance, override provided options, inherit the rest

Example using import (ES Modules, Typescript)

import { Mod11_2, Mod11_10 } from '@konfirm/iso7064';

console.log(Mod11_2.checksum('079')); //  'X'
console.log(Mod11_2.generate('079')); //  '079X'
console.log(Mod11_2.validate('079X')); //  true

console.log(Mod11_10.checksum('079')); // '2'
console.log(Mod11_10.generate('079')); // '0792'
console.log(Mod11_10.validate('0792')); // true

Example using require (CommonJS)

const { Mod11_2, Mod11_10 } = require('@konfirm/iso7064');

console.log(Mod11_2.checksum('079')); //  'X'
console.log(Mod11_2.generate('079')); //  '079X'
console.log(Mod11_2.validate('079X')); //  true

console.log(Mod11_10.checksum('079')); // '2'
console.log(Mod11_10.generate('079')); // '0792'
console.log(Mod11_10.validate('0792')); // true

Extending

The class architecture allows for easy implementation of your custom algorithm, it mostly consists of picking the right system (either pure or hybrid) and creating the appropriate configuration.

Example

The specification for International Standard Audiovisual Number (ISAN, ISO 15706-2:2007) details the use of the ISO 7064, MOD 17,16 algorithm, which in itself is not described in the ISO 7064 specification. The implementation however is simple to do.

The ISO 15706-2:2007 specification states it uses the hybrid system, with hexadecimal characters for the input and a single character checksum (from the same hexadecimal characters). This means the HybridISO7064 should be used, and the indices (allowed input characters) and alphabet should be provided. As the alphabet is the same as the indices we can omit it.

This leads to the following code

// const { HybridISO7064, Alphabet } = require('@konfirm/iso7064');
import { HybridISO7064, Alphabet } form '@konfirm/iso7064';

const Mod17_16 = new HybridISO7064({
	alphabet: Alphabet.from('0123456789ABCDEF')
});

console.log(Mod17_17.checksum('D98989898909898')); // 'B'
console.log(Mod17_17.generate('D98989898909898')); // 'D98989898909899B'
console.log(Mod17_17.validate('D98989898909898B')); // true
console.log(Mod17_17.validate('D98-989-898-909-898-B')); // true

NOTE Searching online for the contents of the ISO 15706-2:2007 specification may lead to a version of the specification which includes an error in the example output. The example number shown below the calculation steps is D98989898909899 with the supposed outcome of B (which should be 9), while the ISAN used used in the calculation steps table is D98989898909898 (last digit being 8 instead of 9).

There is an addendum available for the specification, ISO 15706-1:2002/Amd 1:2008, in which we assume the mistake was corrected (we haven't purchased the specification to verify).

API

ISO7064

This is the base class of the package, it contains all properties and methods used by both the Pure- and HybridISO7064 classes and extends (the Mod-algorithm extensions).

Constructor

New instances (such as the prepared Mod* implementations) are configured using an options object, the table below describes the type, defaults, purpose and whether the values are inherited by the factory method

optiontypedefaultfactory inheritspurpose
algorithmstringCustomnothe name of the algorithm
designationnumber0nothe designation number
modulusnumberalphabet.lengthyesthe modules to use
indicesAlphabetalphabetyesthe indices (allowed characters)
alphabetAlphabetundefinedyesThe characters to use for the checksum
radixnumberundefinedyesthe radix to use
doublebooleanfalseyesuse a double digit checksum

Properties

propertyvaluedescription
algorithmCustomthe name of the algorithm
specificationISO 7064, Customthe full specification and algorithm name
designation0the designated number in the ISO 7064 standard (0 is the described value for non-designations)
modulusundefinedthe modulus of the algorithm
indicesAlphabetthe Alphabet instance with allowed input digits/characters (usesalphabet if ommited)
alphabetAlphabetthe Alphabet instance allowed checksum digits/characters
radixundefinedThe radix used by the algorithm (used by PureISO7064 implementations)
doublefalsedoes the checksum consist of two digits/characters instead of one

Methods

methodinputoutputdescription
normalizestring\|numberstringif no indices where provided during construction, normalization will remove any non-alphanumeric character
checksumstring\|numberthrows Errorwill throw an Error, implementation in an extend is required
validatestring\|numberbooleanvalidate the provided string (including checksum)
generatestring\|numberstringcalculate and append the checksum to the (normalized) input
factoryobjectISO7064Create a new instance, override provided options, inherit the rest

PureISO7064

This is the base class for all pure systems. A pure system uses a single modulus for every stage of the checksum calculation.

As only the pure systems require the radix property, this class implements its default value.

Properties

The PureISO7064 class inherits all properties from ISO7064 and adds radix.

propertyvaluedescription
radix2The radix used by the algorithm

Methods

methodinputoutputdescription
normalizestring\|numberstringthe normalized value used for checksum calculations
checksumstring\|numberstringthe single or double digit/character checksum
validatestring\|numberbooleanvalidate the provided string (including checksum)
generatestring\|numberstringcalculate and append the checksum to the (normalized) input
factoryobjectISO7064Create a new instance, override provided options, inherit the rest

HybridISO7064

This is the base class for all hybrid systems. The hybrid systems use two different modulus operations, one equal to the number of characters in the checksum characters (indices) and the greater then that.

Properties

The Hybrid7064 class inherits all properties from ISO7064.

Methods

methodinputoutputdescription
normalizestring\|numberstringthe normalized value used for checksum calculations
checksumstring\|numberstringthe single or double digit/character checksum
validatestring\|numberbooleanvalidate the provided string (including checksum)
generatestring\|numberstringcalculate and append the checksum to the (normalized) input
factoryobjectISO7064Create a new instance, override provided options, inherit the rest

MOD 11-2

The ISO 7064, MOD 11-2 algorithm is used by several standards, including

Properties

propertytypevalue
algorithmstringMOD 11-2
specificationstringISO 7064, MOD 11-2
designationnumber1
modulusnumber11
radixnumber2
indicesAlphabet0123456789
alphabetAlphabet0123456789X
doublebooleanfalse

Methods

methodinputoutputdescription
normalizestring\|numberstringthe normalized numeric value used for checksum calculations
checksumstring\|numberstringthe single digit or X character checksum
validatestring\|numberbooleanvalidate the provided string (including checksum)
generatestring\|numberstringcalculate and append the checksum to the (normalized) input
factoryobjectISO7064Create a new instance, override provided options, inherit the rest

MOD 11,10

The ISO 7064, MOD 11,10 algorithm is used by several standards, including

Properties

propertytypevalue
algorithmstringMOD 11,10
specificationstringISO 7064, MOD 11,10
designationnumber6
modulusnumber10
indicesAlphabet0123456789
alphabetAlphabet0123456789
doublebooleanfalse

Methods

methodinputoutputdescription
normalizestring\|numberstringthe normalized numeric value used for checksum calculations
checksumstring\|numberstringthe single digit checksum
validatestring\|numberbooleanvalidate the provided string (including checksum)
generatestring\|numberstringcalculate and append the checksum to the (normalized) input
factoryobjectISO7064Create a new instance, override provided options, inherit the rest

MOD 27,26

Properties

propertytypevalue
algorithmstringMOD 27,26
specificationstringISO 7064, MOD 27,26
designationnumber7
modulusnumber26
indicesAlphabetABCDEFGHIJKLMNOPQRSTUVWXYZ
alphabetAlphabetABCDEFGHIJKLMNOPQRSTUVWXYZ
doublebooleanfalse

Methods

methodinputoutputdescription
normalizestring\|numberstringthe normalized alphabetic value used for checksum calculations
checksumstring\|numberstringthe single character alphabetic checksum
validatestring\|numberbooleanvalidate the provided string (including checksum)
generatestring\|numberstringcalculate and append the checksum to the (normalized) input
factoryobjectISO7064Create a new instance, override provided options, inherit the rest

MOD 37-2

The ISO 7064, MOD 37-2 algorithm is used by several standards, including

Properties

propertytypevalue
algorithmstringMOD 37-2
specificationstringISO 7064, MOD 37-2
designationnumber2
modulusnumber37
radixnumber2
indicesAlphabet0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ
alphabetAlphabet0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ*
doublebooleanfalse

Methods

methodinputoutputdescription
normalizestring\|numberstringthe normalized alphanumeric value used for checksum calculations
checksumstring\|numberstringthe single character alphanumeric or * checksum
validatestring\|numberbooleanvalidate the provided string (including checksum)
generatestring\|numberstringcalculate and append the checksum to the (normalized) input
factoryobjectISO7064Create a new instance, override provided options, inherit the rest

MOD 37,36

The ISO 7064, MOD 37,36 algorithm is used by several standards, including

Properties

propertytypevalue
algorithmstringMOD 37,36
specificationstringISO 7064, MOD 37,36
designationnumber8
modulusnumber36
indicesAlphabet0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ
alphabetAlphabet0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ
doublebooleanfalse

Methods

methodinputoutputdescription
normalizestring\|numberstringthe normalized alphanumeric value used for checksum calculations
checksumstring\|numberstringthe single character alphanumeric checksum
validatestring\|numberbooleanvalidate the provided string (including checksum)
generatestring\|numberstringcalculate and append the checksum to the (normalized) input
factoryobjectISO7064Create a new instance, override provided options, inherit the rest

MOD 97-10

Properties

propertytypevalue
algorithmstringMOD 97-10
specificationstringISO 7064, MOD 97-10
designationnumber3
modulusnumber97
radixnumber10
indicesAlphabet0123456789
alphabetAlphabet0123456789
doublebooleantrue

Methods

methodinputoutputdescription
normalizestring\|numberstringthe normalized numeric value used for checksum calculations
checksumstring\|numberstringthe double digit checksum
validatestring\|numberbooleanvalidate the provided string (including checksum)
generatestring\|numberstringcalculate and append the checksum to the (normalized) input
factoryobjectISO7064Create a new instance, override provided options, inherit the rest

MOD 661-26

Properties

propertytypevalue
algorithmstringMOD 661-26
specificationstringISO 7064, MOD 661-26
designationnumber4
modulusnumber661
radixnumber26
indicesAlphabetABCDEFGHIJKLMNOPQRSTUVWXYZ
alphabetAlphabetABCDEFGHIJKLMNOPQRSTUVWXYZ
doublebooleantrue

Methods

methodinputoutputdescription
normalizestring\|numberstringthe normalized alphabetic value used for checksum calculations
checksumstring\|numberstringthe double character alphabetic checksum
validatestring\|numberbooleanvalidate the provided string (including checksum)
generatestring\|numberstringcalculate and append the checksum to the (normalized) input
factoryobjectISO7064Create a new instance, override provided options, inherit the rest

MOD 1271-36

Properties

propertytypevalue
algorithmstringMOD 1271-36
specificationstringISO 7064, MOD 1271-36
designationnumber5
modulusnumber1271
radixnumber36
indicesAlphabet0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ
alphabetAlphabet0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ*
doublebooleantrue

Methods

methodinputoutputdescription
normalizestring\|numberstringthe normalized alphanumeric value used for checksum calculations
checksumstring\|numberstringthe double character alphanumeric checksum
validatestring\|numberbooleanvalidate the provided string (including checksum)
generatestring\|numberstringcalculate and append the checksum to the (normalized) input
factoryobjectISO7064Create a new instance, override provided options, inherit the rest

Credits

There a quite a few implementations of the ISO 7064 specification in various programming languages. From some we've taken the libery of using (parts of) the data samples for the unit tests.

License

MIT License Copyright (c) 2019-2022 Rogier Spieker (Konfirm)

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.