Forum Discussion
gerald_wegener_
Nimbostratus
Jul 11, 2005Regsub always returns a 1 never 0
I've tried running the script that is posted which uses regsub to search for Social Security Numbers in the form xxx-xx-xxxx. I've tried several different permuations of this but can never get anythin...
unRuleY_95363
Jul 13, 2005Historic F5 Account
I found the problem with my version of the LUHN algorithm. I was trying to avoid the use of % as it is a relatively expensive operation compared to & (by a factor of about 10). Here is the corrected version:
Find ALL the possible credit card numbers in one pass
set card_nums [regexp -all -inline {(?:3[4-7]\d{13})|(?:4\d{15})|(?:5[1-5]\d{14})|(?:6011\d{12})} $payload]
Now iterate over each one and check, categorize and log it
foreach cardnum $card_nums {
set cclen [string length $cardnum]
set double [expr {$cclen & 1}]
set chksum 0
for { set i 0 } { $i < $cclen } { incr i } {
set c [string index $cardnum $i]
if {($i & 1) == $double} {
if {[incr c $c] >= 10} {incr c -9}
}
incr chksum $c
}
switch [string index $cardnum 0] {
3 { set type AmericanExpress }
4 { set type Visa }
5 { set type MasterCard }
6 { set type Discover }
default { set type Unknown }
}
if { ($chksum % 10) == 0 } {
set isCard valid
} else {
set isCard invalid
}
log local0. "Found $isCard $type CC $cardnum - Client SourceIP: $clientip Accessing URI: $clienturi via ServerIP: $serverip"
}
I found a buddy with an Amex card and now have tested this with both my Visa cards and his Amex number and it appears to be working for both now.
Also, there is no need to check the lengths and or more than the first digit as the regular expression is only going to find card numbers according to the RE.
Recent Discussions
Related Content
DevCentral Quicklinks
* Getting Started on DevCentral
* Community Guidelines
* Community Terms of Use / EULA
* Community Ranking Explained
* Community Resources
* Contact the DevCentral Team
* Update MFA on account.f5.com
Discover DevCentral Connects
