Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a better way to mask a Credit Card number in python?

Tags:

python

string

So I have a csv file that contains full credit-card numbers.. We dont need the full number, and so I am writing a quick script to parse through the csv and replace the cc number with a masked representation. (all *'s except the last four). I am pretty new to python and hacked this up, and it works, but in order to learn I want to know if it could be done easier.

Assume that "str" will be a full creditcard number. But for the sake of my example I am just using the string "CREDITCARDNUMBER".

str = "CREDITCARDNUMBER";
strlength = len(str)
masked = strlength - 4
slimstr = str[masked:]
print "*" * masked + slimstr

The output is exactly what I want

************MBER

But I am sure there is a more elegant solution. :) Thanks!

like image 633
Bill Swearingen Avatar asked Mar 16 '12 01:03

Bill Swearingen


People also ask

How do I mask a credit card number?

Use 4 issuer digits. The first four digits of the credit card number are copied from the source to the output. The remaining part of the credit card number is appended with the masked account number and a check digit. A check digit is a digit added to a number that validates the authenticity of the number.

How do you hide a number in Python?

In Python, we use double underscore before the attributes name to make them inaccessible/private or to hide them.


1 Answers

For those who want to keep Issuer Identification Number (IIN) (previously called the "Bank Identification Number" (BIN)), which is usually the first 6 digits, that should do the job:

print card[:6] + 'X' * 6 + card[-4:]
'455694XXXXXX6539'
like image 90
kirpit Avatar answered Nov 15 '22 15:11

kirpit