Jump to content

Luhn Numbers


blindwig
 Share

Recommended Posts

I've just written 2 functions to work with Luhn numbers. Luhn numbers are (usually) many digits long, and have a built-in checksum. This checksum allows the number to be checked against someone accidently entering it in wrong. Luhn numbers are good for account numbers (in fact the 16-digit credit card numbers issued by MasterCard and Visa are Luhn numbers)

Here's the functions:

;===============================================================================
;
; Description:    Returns weather a string contains a Luhn number
; Parameter(s):  $s_Num is a string expected to be only digits (0-9)
; Requirement(s):   None
; Return Value(s):  On Success - Returns a boolean True or False
;                               Set @EXTENDED to the Luhn Checksum
;                  On Failure - Return a plain-english description of the error
;                                  @ERROR to:  1 - Input is not a valid numeric string
; Author(s):        Mike Ratzlaff AKA BlindWig
; Note(s):
;
;===============================================================================
Func _LuhnValidate($s_Num)
    If IsString($s_Num) And StringIsDigit($s_Num) Then
        Local $a_Digit = StringSplit($s_Num,'')
        Local $i_Count, $i_State = 0, $i_Temp, $i_CheckSum, $RetVal
        For $i_Count = $a_Digit[0] To 1 Step -1
            If $i_State Then
                $i_Temp = $a_Digit[$i_Count] * 2
                If $i_Temp > 9 Then
                    $i_Temp = Int($i_temp/10) + mod($i_Temp, 10)
                EndIf
            Else
                $i_Temp = $a_Digit[$i_Count]
            EndIf
            $i_CheckSum += $i_Temp
            $i_State = Not $i_State
        Next
        $RetVal = Mod($i_Checksum,10) = 0
        SetExtended($i_CheckSum)
        Return $RetVal
    Else
        SetError(1)
        Return 'Input "' & $s_Num & '" is not a valid numeric string'
    EndIf
EndFunc

;===============================================================================
;
; Description:    Returns a string containing a Luhn number of a given length
; Parameter(s):  $i_Length = number of digits for the Luhn number
; Requirement(s):   Calls _LuhnValidate()
; Return Value(s):  On Success - Returns a Luhn number of the given length
;                  On Failure - Returns '0'
; Author(s):        Mike Ratzlaff AKA BlindWig
; Note(s):
;
;===============================================================================
Func _LuhnGenerate($i_Length)
    Local $i_Count, $s_Num, $i_CheckSum
    If $i_Length >= 2 Then
        For $i_Count = 1 To $i_Length - 1
            $s_Num &= String(Random(0,9,1))
        Next
        _LuhnValidate($s_Num & '0')
        $i_CheckSum = @extended
        $s_Num &= String(10-Mod($i_CheckSum,10))
        Return $s_Num
    Else
        Return '0'
    EndIf
EndFunc

So for example if someone gives you a creditcard number, you can verify that it's a valid account number like this:

_LuhnValidate($s_GivenNumber)

And if you need a random creditcard number (for example, to test an entry form) you can use this:

$s_CCnum = _LuhnGenerate(16)

EDIT

Removed ConsoleWrite() call - that was for debugging, didn't mean to keep it in the final function

Edited by blindwig
Link to comment
Share on other sites

Question

$s_Num is a string expected to be only digits (0-9)

So for example if someone gives you a creditcard number, you can verify that it's a valid account number like this:

_LuhnValidate($s_GivenNumber)

with those thoughts and credit cards having 16 digits, how do we use this to check a credit card number??

thx

8)

NEWHeader1.png

Link to comment
Share on other sites

  • Moderators

This is really good! Wish you were doing this permutation code I can't get to go faster :think:...

Great work!

Common sense plays a role in the basics of understanding AutoIt... If you're lacking in that, do us all a favor, and step away from the computer.

Link to comment
Share on other sites

  • 1 year later...

I've just written 2 functions to work with Luhn numbers. Luhn numbers are (usually) many digits long, and have a built-in checksum. This checksum allows the number to be checked against someone accidently entering it in wrong. Luhn numbers are good for account numbers (in fact the 16-digit credit card numbers issued by MasterCard and Visa are Luhn numbers)

Thank you for the script it's very usefull to me

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...