Results 1 to 1 of 1

Thread: Luhn algorithm

  1. #1

    Thread Starter
    Powered By Medtronic dbasnett's Avatar
    Join Date
    Dec 2007
    Location
    Jefferson City, MO
    Posts
    9,897

    Luhn algorithm

    From http://en.wikipedia.org/wiki/Luhn_algorithm, "he 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."

    Code:
    Class LuhnAlgorithm
        'http://en.wikipedia.org/wiki/Luhn_algorithm
    
        ''' <summary>
        ''' add check digit to a number
        ''' </summary>
        ''' <param name="theNum">the number</param>
        ''' <returns>the number concatenated with check digit</returns>
        ''' <remarks></remarks>
        Public Shared Function AddChkDigiToNum(theNum As String) As String
            Dim chkDigit As Integer = (LuhnAlgorithm.CreateList(theNum, True).Sum * 9) Mod 10
            Return theNum & chkDigit.ToString
        End Function
    
        ''' <summary>
        ''' verify number with check digit is valid
        ''' </summary>
        ''' <param name="theNum">the number to validate</param>
        ''' <returns>true if valid</returns>
        ''' <remarks></remarks>
        Public Shared Function ValidateNum(theNum As String) As Boolean
            Dim chk As Integer = LuhnAlgorithm.CreateList(theNum, False).Sum Mod 10
            Return chk = 0
        End Function
    
        Private Shared Function CreateList(theNum As String, doubleStart As Boolean) As List(Of Integer)
            Dim dblIt As Boolean = doubleStart
            Dim sums As New List(Of Integer)
            For x As Integer = theNum.Length - 1 To 0 Step -1
                Dim s As String = theNum(x)
                'skip non-numerics
                If Integer.TryParse(s, Nothing) Then
                    If dblIt Then
                        sums.Add(LuhnAlgorithm.RetrunDouble(s))
                    Else
                        sums.Add(Integer.Parse(s))
                    End If
                    dblIt = Not dblIt
                End If
            Next
            Return sums
        End Function
    
        Private Shared Function RetrunDouble(n As String) As Integer
            Dim i As Integer = 2 * Integer.Parse(n) 'double the number
            If i.ToString.Length > 1 Then 'if length is > 1 then 
                'sum the individual digits
                Dim s As String = i.ToString
                i = 0
                For x As Integer = 0 To s.Length - 1
                    i += Integer.Parse(s(x))
                Next
            End If
            Return i
        End Function
    End Class
    Sample Usage:

    Code:
        Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
            Dim s As String = LuhnAlgorithm.AddChkDigiToNum("7992739871")
            LuhnAlgorithm.ValidateNum(s)
        End Sub
    Last edited by dbasnett; Nov 30th, 2014 at 08:44 AM.
    My First Computer -- Documentation Link (RT?M) -- Using the Debugger -- Prime Number Sieve
    Counting Bits -- Subnet Calculator -- UI Guidelines -- >> SerialPort Answer <<

    "Those who use Application.DoEvents have no idea what it does and those who know what it does never use it." John Wein

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width