Skip to content

Genuine Credit Card Number?

November 24, 2011

As part of the Payment Card Industry Data Security Standards (PCI DSS) compliance, its not okay to leave credit card numbers lying around on your filesystems. If you are interested to tell if a long string of numbers is a credit card number, this subroutinne will run the number against the Luhn test, returning 1 if a match is positive.

sub luhn_test {
        my $number = $_[0];
        chomp $number;

        # Split the number into single digits and store in array
        @digits  = split (//, $number);

        my $total = 0;                          # Holds the running total
        my $i =  1;                             # Digit counter
        for $d (reverse (@digits)) {            # Go through in reverse
                if ( ! ($i++ % 2) ) {           # If its a digit in an even 
                                                # position,
                        $d *= 2;                # double it and
                        if ( $d > 9) {          # if the result is over 
                                                # 9...
                                $d -= 9;        # ...subtract 9
                                }
                        }
                $total += $d;                   # Running total
                }

        if ( $total > 0  && !($total % 10) ) {  # Mod 10 of this total
                # Luhn algorithm passed if the total is greater than 10,
                # and total mod 10 is zero (i.e. divisable by ten without 
                # a remainer)
                return 1;
                }
        return 0;
        }
Advertisements

From → Scripts

Leave a Comment

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

w

Connecting to %s

%d bloggers like this: