Luhn Extended N Algorithm (LENA)

/* The Luhn algorithm or Luhn formula, also known as the “modulus 10” or “mod 10” algorithm, 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 US and Canadian Social Insurance Numbers. It was created by IBM scientist Hans Peter Luhn and described in U.S. Patent No. 2,950,048, filed on January 6, 1954, and granted on August 23, 1960.

The algorithm is in the public domain and is in wide use today. It is specified in ISO/IEC 7812-1[1]. It is not intended to be a cryptographically secure hash function; it was designed to protect against accidental errors, not malicious attacks. Most credit cards and many government identification numbers use the algorithm as a simple method of distinguishing valid numbers from collections of random digits.

This version:
Luhn Extended N Algorithm (LENA) 1.0 L
copyright © 2010 by Economy-x-Talk
Mark Schonewille
Nijmegen, the Netherlands

For more information, see
http://en.wikipedia.org/wiki/Luhn_mod_N_algorithm

This script can be redistributed freely, if unmodified and including al comments and the copyright statement.
*/
local lMap

private function map theData
     put empty into myMap
     repeat for each char myChar in theData
          if myChar is not in myMap then put myChar after myMap
     end repeat
     return myMap
end map

private on luhnInitialise theInput
     put map(theInput) into lMap
end luhnInitialise

private function codePointFromChar theChar
     return offset(theChar,lMap) – 1
end codePointFromChar

private function charFromCodePoint thePoint
     return char thePoint + 1 of lMap
end charFromCodePoint

function generateCheckChar theInput
     put replaceText(theInput,”[W]”,empty) into theInput
     luhnInitialise theInput
     put 2 into myFactor
     put 0 into mySum
     put length(lMap) into n
     repeat with i = length(theInput) down to 1
          put codePointFromChar(char i of theInput) into myCodePoint
          put myFactor * myCodePoint into myAddend
          if myFactor is 2 then put 1 into myFactor else put 2 into myFactor
          add trunc((myAddend / n) + (myAddend mod n)) to mySum
     end repeat
     put mySum mod n into myRemainder
     put n – myRemainder into myCheckCodePoint
     put myCheckCodePoint mod n into myCheckCodePoint
     return charFromCodePoint(myCheckCodePoint)
end generateCheckChar

function validateCheckChar theInput
     put replaceText(theInput,”[W]”,empty) into theInput
     luhnInitialise char 1 to -1 of theInput
     put 1 into myFactor
     put 0 into mySum
     put length(lMap) into n
     repeat with i = length(theInput) down to 1
          put codePointFromChar(char i of theInput) into myCodePoint
          put myFactor * myCodePoint into myAddend
          if myFactor is 2 then put 1 into myFactor else put 2 into myFactor
          add trunc((myAddend / n) + (myAddend mod n)) to mySum
     end repeat
     put mySum mod n into myRemainder
     return myRemainder is 0
end validateCheckChar

/* end of the Luhn Extended N Algorithm */

 

Leave a Reply

Your email address will not be published. Required fields are marked *


*