Created
February 9, 2025 14:32
-
-
Save LolloDev5123/0231b8dcb0ded4da84b42ef1345e2711 to your computer and use it in GitHub Desktop.
verhoeff.lua
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| --[[ | |
| Verhoeff Gumm Check Digit Library in Lua | |
| ----------------------------------------- | |
| @lollodev5123 | |
| MIT License | |
| Copyright (c) 2025 | |
| 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. | |
| --]] | |
| local verhoeff = {} | |
| -- Multiplication table d (1-indexed; use [digit+1] to index) | |
| local d = { | |
| {0,1,2,3,4,5,6,7,8,9}, | |
| {1,2,3,4,0,6,7,8,9,5}, | |
| {2,3,4,0,1,7,8,9,5,6}, | |
| {3,4,0,1,2,8,9,5,6,7}, | |
| {4,0,1,2,3,9,5,6,7,8}, | |
| {5,9,8,7,6,0,4,3,2,1}, | |
| {6,5,9,8,7,1,0,4,3,2}, | |
| {7,6,5,9,8,2,1,0,4,3}, | |
| {8,7,6,5,9,3,2,1,0,4}, | |
| {9,8,7,6,5,4,3,2,1,0} | |
| } | |
| -- Permutation table p (1-indexed; row: ((position mod 8)+1), column: (digit+1)) | |
| local p = { | |
| {0,1,2,3,4,5,6,7,8,9}, | |
| {1,5,7,6,2,8,3,0,9,4}, | |
| {5,8,0,3,7,9,6,1,4,2}, | |
| {8,9,1,6,0,4,3,5,2,7}, | |
| {9,4,5,3,1,2,6,8,7,0}, | |
| {4,2,8,6,5,7,3,9,0,1}, | |
| {2,7,9,3,8,0,6,4,1,5}, | |
| {7,0,4,6,9,1,3,2,5,8} | |
| } | |
| -- Inverse table inv (1-indexed; use [digit+1] to index) | |
| local inv = {0,4,3,2,1,5,6,7,8,9} | |
| ---------------------------------------------------------------------- | |
| -- Generate the Verhoeff check digit. | |
| -- | |
| -- @param input (string|table) A number as a string of digits or an array of digits (0–9) | |
| -- @return number The computed check digit (0–9) | |
| ---------------------------------------------------------------------- | |
| function verhoeff.generate(input) | |
| local c = 0 | |
| if type(input) == "string" then | |
| local n = #input | |
| for i = n, 1, -1 do | |
| local ch = input:sub(i, i) | |
| local digit = ch:byte() - 48 -- convert ASCII '0'-'9' to number 0-9 | |
| if digit < 0 or digit > 9 then | |
| error("Invalid character '" .. ch .. "' in input string. Expected digits 0–9.") | |
| end | |
| local pos = ((n - i) % 8) + 1 | |
| c = d[c + 1][ p[pos][digit + 1] ] | |
| end | |
| elseif type(input) == "table" then | |
| local n = #input | |
| for i = n, 1, -1 do | |
| local digit = input[i] | |
| if type(digit) ~= "number" or digit < 0 or digit > 9 or digit ~= math.floor(digit) then | |
| error("Invalid digit at position " .. i .. " in input table. Expected an integer 0–9.") | |
| end | |
| local pos = ((n - i) % 8) + 1 | |
| c = d[c + 1][ p[pos][digit + 1] ] | |
| end | |
| else | |
| error("Input must be a string or a table of digits (0–9).") | |
| end | |
| return inv[c + 1] | |
| end | |
| ---------------------------------------------------------------------- | |
| -- Validate a number that includes a Verhoeff check digit. | |
| -- | |
| -- @param input (string|table) A number (with check digit) as a string or an array of digits (0–9) | |
| -- @return boolean True if the input is valid, false otherwise. | |
| ---------------------------------------------------------------------- | |
| function verhoeff.validate(input) | |
| local c = 0 | |
| if type(input) == "string" then | |
| local n = #input | |
| for i = n, 1, -1 do | |
| local ch = input:sub(i, i) | |
| local digit = ch:byte() - 48 | |
| if digit < 0 or digit > 9 then | |
| error("Invalid character '" .. ch .. "' in input string. Expected digits 0–9.") | |
| end | |
| local pos = ((n - i) % 8) + 1 | |
| c = d[c + 1][ p[pos][digit + 1] ] | |
| end | |
| elseif type(input) == "table" then | |
| local n = #input | |
| for i = n, 1, -1 do | |
| local digit = input[i] | |
| if type(digit) ~= "number" or digit < 0 or digit > 9 or digit ~= math.floor(digit) then | |
| error("Invalid digit at position " .. i .. " in input table. Expected an integer 0–9.") | |
| end | |
| local pos = ((n - i) % 8) + 1 | |
| c = d[c + 1][ p[pos][digit + 1] ] | |
| end | |
| else | |
| error("Input must be a string or a table of digits (0–9).") | |
| end | |
| return c == 0 | |
| end | |
| ---------------------------------------------------------------------- | |
| -- Append the Verhoeff check digit to the given number. | |
| -- | |
| -- @param input (string|table) A number without the check digit (as a string or table) | |
| -- @return (string|table) The input with the computed check digit appended (same format as input) | |
| ---------------------------------------------------------------------- | |
| function verhoeff.append(input) | |
| local checkDigit = verhoeff.generate(input) | |
| if type(input) == "string" then | |
| return input .. tostring(checkDigit) | |
| elseif type(input) == "table" then | |
| local result = {} | |
| for i = 1, #input do | |
| result[i] = input[i] | |
| end | |
| result[#result + 1] = checkDigit | |
| return result | |
| else | |
| error("Input must be a string or a table of digits (0–9).") | |
| end | |
| end | |
| return verhoeff |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment