Skip to content
⌘ NSIS Forum Archive

Luhn validation algorithm

1 posts

Metheny#

Luhn validation algorithm

Hello, I needed a modified version of Luhn Algorithm into one installer and I didn't see anything similar on the wiki/forum, so here's the validation algorithm, it might be useful for someone.
The example needs NSISArray and PassDialog to work.
My 2 cents...
Name    "Luhn_Algorithm"
OutFile "Luhn_Algorithm.exe"

## Include headers
!include MUI.nsh
!include NSISArray.nsh

## Pages
Page Custom NumberPageShow NumberPageLeave
!insertmacro MUI_PAGE_COMPONENTS
!insertmacro MUI_PAGE_INSTFILES

## Languages
!insertmacro MUI_LANGUAGE English

Var Checksum
Var Numbers

Function NumberPageShow
!insertmacro MUI_HEADER_TEXT "Luhn algorithm validation" ""

PassDialog:😁ialog Serial \
/HEADINGTEXT "Enter the number that you want to check." \
/CENTER \
/BOXDASH 62 70 4 '' \
/BOXDASH 142 70 4 '' \
/BOXDASH 222 70 4 '' \
/BOX 302 70 4 ''

Pop $R0
FunctionEnd

Function NumberPageLeave
Pop $R0
Pop $R1
Pop $R2
Pop $R3

StrCpy $Numbers $R0$R1$R2$R3
Goto checkKey

checkKey:
${Array} array 9 3
${ArrayFunc} WriteList
${ArrayFunc} Read
${array->Init}
StrCpy $Checksum "0"
${array->WriteList} '"0" "1" "2" "3" "4" "-4" "-3" "-2" "-1" "0"'
StrCpy $R6 15 # Numbers length - 1
${Do}
StrCpy $0 "$R0$R1$R2$R3" 1 $R6
IntOp $Checksum $Checksum + $0
Math::script "a = $R6; b = 16; c = 2; r9 = a; r1 = b; r2 = c; z = ((a - b) % c); r4 = #[z == 0, 'Valid', 'Invalid']"
${If} $4 == 'Valid'
${array->Read} $R7 $0
IntOp $Checksum $Checksum + $R7
${EndIf}
IntOp $R6 $R6 - 1
${LoopUntil} $R6 = -1
${array->Delete}
Math::script "a = $Checksum; b = 10; r0 = a; r1 = b; z = a % b; r2 = #[z == 0, 'Valid', 'Invalid']"

${If} $2 == 'Valid'
Goto success
${Else}
Goto returnError
${EndIf}

returnError:
MessageBox MB_OK|MB_ICONEXCLAMATION "Wrong number"
Abort

success:
FunctionEnd

Section "dummy"

SectionEnd
I'll try to write Verhoeff now.

See you.