• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

mask cc number in log files from a string in the log file

 
lina ajay
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi

issue :--

i want to mask the creditcard number which gets displayed in my log files .
i have a class for creditcard processing which gets the information abt creditcard and writes it to a log file - i have to find the position of the cc in the long string" transaction string" which has details abt user id etc too.
this is what i have done so far

int logtranString = transactionString.indexOf("cc");

can anyone please help me in accomplishing the rest of it.

thanks a lot
 
Steve Luke
Bartender
Posts: 4181
21
IntelliJ IDE Java Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This may be a good place to use RegEx with the String.replaceAll(regex, replacement) method.

Look into the Pattern API and see if you can't find a Pattern that would match:
"4 digits" dash "4 digits" dash "4 digits" dash "4 digits" coming after "cc"

If you aren't familiar with RegEx then your strategy may be:
  • Use a StringBuilder:
  • Copy all the contents of the String before the indexOf("cc") [perhaps + 2 so "cc" appears in the output] into the StringBuilder
  • Append the mask for the credit card number onto the StringBiolder
  • Append all the contents of the String after indexOf("cc") + 2 + length of the credit card number into the StringBuilder
  • Turn the StringBuilder into a String.


  • The API for String and StringBuilder should help.
     
    Rob Spoor
    Sheriff
    Pie
    Posts: 20512
    54
    Chrome Eclipse IDE Java Windows
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    That works for most CC numbers, but 16 is not a hard limit. In fact, actual lengths range from 12 to 19.

    But if you limit input to 16 characters, than the following regex should do:

    In other words:
    - 4 characters
    - a space, dash or nothing followed by 4 characters, and this entire part times three
     
    Campbell Ritchie
    Sheriff
    Posts: 48652
    56
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    By four characters, Rob, you presumably mean four digits?
     
    lina ajay
    Ranch Hand
    Posts: 31
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    when i use StringBuilder SB= new String Builder(); I am getting an error message that -- string builder not defined. i have imported java.util.*; is there any other class to be imported to use this.
    jvm on my machine is 1.5 version.

    thanks
    Steve Luke wrote:This may be a good place to use RegEx with the String.replaceAll(regex, replacement) method.

    Look into the Pattern API and see if you can't find a Pattern that would match:
    "4 digits" dash "4 digits" dash "4 digits" dash "4 digits" coming after "cc"

    If you aren't familiar with RegEx then your strategy may be:
  • Use a StringBuilder:
  • Copy all the contents of the String before the indexOf("cc") [perhaps + 2 so "cc" appears in the output] into the StringBuilder
  • Append the mask for the credit card number onto the StringBiolder
  • Append all the contents of the String after indexOf("cc") + 2 + length of the credit card number into the StringBuilder
  • Turn the StringBuilder into a String.


  • The API for String and StringBuilder should help.
     
    fred rosenberger
    lowercase baba
    Bartender
    Pie
    Posts: 12100
    30
    Chrome Java Linux
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    you want "StringBuilder", not "String Builder".

    no space.
     
    lina ajay
    Ranch Hand
    Posts: 31
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    i tried either way. it still shows an error message??

    fred rosenberger wrote:you want "StringBuilder", not "String Builder".

    no space.
     
    Steve Luke
    Bartender
    Posts: 4181
    21
    IntelliJ IDE Java Python
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    lina ajay wrote:i tried either way. it still shows an error message??

    fred rosenberger wrote:you want "StringBuilder", not "String Builder".

    no space.


    Then verify that you have Java 1.5+ installed, and that you aren't compiling the code to be 1.4 compatible.

    StringBuilder is available in Java 1.5 and above. If you want to be backwards compatible before J1.5, use StringBuffer instead.
     
    Rob Spoor
    Sheriff
    Pie
    Posts: 20512
    54
    Chrome Eclipse IDE Java Windows
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Campbell Ritchie wrote:By four characters, Rob, you presumably mean four digits?

    I surely do.
     
    • Post Reply
    • Bookmark Topic Watch Topic
    • New Topic