Module mod11
mod11 is a Lua module to calculate and verify modulo11 checksums.
Modulo 11 is commonly used to prevent human errors. Common usecases are; bank account numbers, creditcard numbers, social security numbers.
Info:
- Release: Version 1.0, mod11 module to calculate and verify checksums
- License: mod11 is free software under the MIT license.
- Author: Thijs Schreijer, http://www.thijsschreijer.nl
- Copyright: 2014 Thijs Schreijer
Functions
new (verify, allowed) | Creates a new object based on the given verify weights and allowed characters. |
getverify (self) | Returns the string with verify weights as passed to new |
getallowed (self) | Returns the string with allowed characters as passed to new . |
calc (self, inp) | Calculates the checksum. |
split (self, inp) | Splits a number with checksum into number and checksum. |
check (self, inp) | Checks a number to be a valid one. |
foreach (self, text, minsize, maxsize) | Iterator factory. |
Functions
- new (verify, allowed)
-
Creates a new object based on the given verify weights and allowed characters.
Parameters:
- verify string A string of size 8, containing the digits 2-9, in some random order (no doubles).
- allowed string (optional) A string of characters allowed in between the digits. The digits (0-9) will automatically be added to the allowed set.
Returns:
- mod11 object that can be used to generate and verify checksums
- Throws an error on invalid input
Usage:
local mod11 = require("mod11") local m = mod11("25834967","-/") -- mod11() is a shortcut to mod11:new() local invoicenr = "2013-12-23/013-" -- a date and a sequential nr invoicenr = invoicenr..m:calc(invoicenr) -- append the checksum print("Invoicenr including check:",invoicenr)
- getverify (self)
-
Returns the string with verify weights as passed to new
Parameters:
- self The mod11 object created by new .
- getallowed (self)
-
Returns the string with allowed characters as passed to new .
The returned string will also contain the digits (0-9), which are also allowed. Hence
the string returned will not be identical to the string passed to new .
Parameters:
- self The mod11 object created by new .
- calc (self, inp)
-
Calculates the checksum. See new for an example.
Parameters:
- self The mod11 object created by new .
- inp string The string to calculate the checksum for. The string can only contain digits and 'allowed' characters
Returns:
- 2 values; 1) input with checksum appended, 2) checksum only
- Throws an error on invalid input
- split (self, inp)
-
Splits a number with checksum into number and checksum.
It will not test validity of the checksum, use check for testing validity.
Parameters:
- self The mod11 object created by new .
- inp The string containing the number sequence to split.
Returns:
- 2 strings; number and checksum. Both will be digits only, all other characters will be removed
- Throws an error on invalid input
- check (self, inp)
-
Checks a number to be a valid one.
The check will only succeed if the
verify
number used is the exact same number as the one used to create the checksum.Parameters:
- self The mod11 object created by new .
- inp The string containing the number sequence to verify.
Returns:
-
true
if the checksum matches -
false
if the checksum failed -
nil
+ error message if bad input was provided
- foreach (self, text, minsize, maxsize)
-
Iterator factory. Return an iterator, which returns all valid numbers in the provided string.
The optional
minsize
andmaxsize
parameters can be used to minimize the chance of false positives.Parameters:
- self The mod11 object created by new .
- text The string to search. If text is not a string it will be 'tostring'ed
- minsize (optional) The minimum length (in digits, excluding 'allowed' characters)
- maxsize (optional) The maximum length (in digits, excluding 'allowed' characters)
Returns:
- an iterator function that will return 3 values; 1) value (incl chk nr), 2) startpos, 3) endpos
-
Throws an error on invalid input (
minsize < 2
, ormaxsize < minsize
)
Usage:
-- see the sample directory for an example of the iterator