Question
· May 14, 2021

Luhn Mod N in Cache?

Hi all

I'm trying to recreate this method below in Cache/IRIS. 

https://en.wikipedia.org/wiki/Luhn_mod_N_algorithm ISO-7812-1 (LUHN-10)

I've been following the javascript example. It seems simple in theory but i'm struggling with which cache methods to pick.

$LENGTH, $System.SQL.FLOOR() - seem ok

$EXTRACT - I think for .charAt (though may need to correct base)

$FIND - for .indexOf

Any suggestion which would be the best ones for the job?

Thanks

Dan

Product version: IRIS 2019.1
$ZV: IRIS for Windows (x86-64) 2019.1.1 (Build 615_1_20704U) Thu Mar 18 2021 15:49:22 EDT
Discussion (5)1
Log in or sign up to continue

Hi. Thanks for your tips! Though it has to be Mod N version which is an extension that supports non-numerical strings. 

I guess I can use a proxy class to get the result but it would be nice to do this natively in Cache/MUMPS. What I will say is this: the need to do this it is going to be very important for others I expect also. A bit more info about why it is needed is below, with the character set needed to create codepoints from.

Link

LuhnMCheckSum(input) public {
  Set input = $Piece(input, "#", 1)
  Set codePoints = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789/:"
  Set n = $Length(codePoints)

  Set sum = 0
  Set factor = 2
  Set len = $Length(input)  
  For i = len:-1:1 {
    Set codePoint = $Find(codePoints, $Extract(input, i)) - 2
    Set addend = factor * codePoint
    Set factor = $Case(factor, 2: 1, : 2)
    Set addend = (addend \ n) + (addend # n)
    Set sum = sum + addend
  }
  Set remainder = sum # n
  Set checkCodePoint = (n - remainder) # n
  Return $Extract(codePoints, checkCodePoint + 1)
}
LuhnMValidate(input) public {
  Set checksum = $Piece(input, "#", 2)
  Set input = $Piece(input, "#")
  Return $$LuhnMCheckSum(input) = checksum
}