Beefy Boxes and Bandwidth Generously Provided by pair Networks
good chemistry is complicated,
and a little bit messy -LW
 
PerlMonks  

Find Credit Card Numbers and Mask only 6 digits in a logfile

by gtk (Acolyte)
on Apr 30, 2018 at 10:43 UTC ( #1213816=perlquestion: print w/replies, xml ) Need Help??
gtk has asked for the wisdom of the Perl Monks concerning the following question:

I wanted to find the card numbers in a logfiles and mask the card numbers. Can you please let me know what is the easiest way to do this?

Card Number format => 1234567890123456 and I want to mask the number like his one 123456******3456

The problem is to identify the number in the file because I cannot open the file because sometimes files are too large. The next problem is mask the card number from 7th digit to 12th digit.

The logfile how the crd number stored

Dec 10 14:00:00 AAA: ABC * 11111 * + CRD : 1234567890123456

Replies are listed 'Best First'.
Re: Find Credit Card Numbers and Mask only 6 digits in a logfile
by Marshall (Abbot) on Apr 30, 2018 at 11:07 UTC
    I don't understand this: "The problem is to identify the number in the file because I cannot open the file because sometimes files are too large.". Maybe you are trying to read the whole file into memory at once? Open the file and read it line by line. Use a regex to modify the number, perhaps something as simple as this:
    use strict; use warnings; my $n = 'asdfklj fuyy23 1234567890123456 78vc'; $n =~ s/(\d{6,6})(\d{6,6})(\d{4,4})/$1******$3/; print "$1 $2 $3\n"; print $n; __END__ 123456 789012 3456 asdfklj fuyy23 123456******3456 78vc
Re: Find Credit Card Numbers and Mask only 6 digits in a logfile
by marto (Archbishop) on Apr 30, 2018 at 10:55 UTC

    Why are you storing credit card details in plain text in a log file?

      Why are you storing credit card details in plain text in a log file?

      Not storing them; parsing the dump of a breach for posting on the dark web ... whoops, did I say that out loud?

      It is good question, due to some debugging purpose we have disabled the encryption and currently we want to mask only 6 digits.
Re: Find Credit Card Numbers and Mask only 6 digits in a logfile
by shmem (Chancellor) on Apr 30, 2018 at 10:59 UTC
    The problem is to identify the number in the file because I cannot open the file because sometimes files are too large.

    The problem is not opening the file. If opening fails, it is because you are not allowed to do that (permissions), file doesn't exist or isn't a file.

    Most likely, what fails is reading the whole file into memory.

    Show some code! so we can see where it probably fails.

    Matching 123456******3456 in perl is m/123456\d{6}3456/. See perlre.

    perl -le'print map{pack c,($-++?1:13)+ord}split//,ESEL'
Re: Find Credit Card Numbers and Mask only 6 digits in a logfile
by kcott (Chancellor) on May 01, 2018 at 09:58 UTC

    G'day gtk,

    I don't see how the size of the file prevents you from opening it. Perhaps you meant something else. If you show us the code you're using, we can probably point you in the right direction.

    As far as "masking" goes, substr would seem to be an appropriate tool:

    $ perl -E 'my $x = "1234567890123456"; say $x; substr $x, 6, 6, "*"x6; + say $x' 1234567890123456 123456******3456

    — Ken

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://1213816]
Approved by haukex
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others about the Monastery: (13)
As of 2019-03-21 14:02 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    How do you Carpe diem?





    Results (108 votes). Check out past polls.

    Notices?