Code – Mask text to remove PII

Sometimes you need to remove personal identifiable information (PII) from the data you present in an application or on a web page. In the last couple of weeks this issue popped up twice, including one application which needs to be be HIPAA compliant. One solution is to mask any personal identifiable data so that the recipient can still verify the information, without sending it all in clear. I am sure you all seen this on for example credit card statements, with only the last 4 digits of your credit card number displayed.

I wrote a simple  Lotusscript function to do this, and I thought I would share it so others can use it as well. You pass a string to mask, the number of characters to leave un-masked and where the unmasked characters should be displayed (“B” for beginning or “E” for end).

MsgBox masktext("TexasSwede",3,"B")

This line would display Tex*******

MsgBox maskText("1234567890",4,"E")

This line would display ******7890

Enjoy!

 

%REM
    Function maskText
    Description: Masks a text with asterisks, leaving the num first or
    last characters visible. Direction is "B" (beginning) or "E" (end).
    Created by Karl-Henry Martinsson - texasswede@gmail.com 
%END REM
Function maskText(value As String, num As Integer, direction As string) As String
    Dim tmp As String
    Dim i As Integer
    If Len(value)>num Then
        If Left$(UCase(direction),1)="B" Then    ' Start at the beginning
            tmp = Left$(value,num)
            For i = num+1 To Len(value)
                tmp = tmp + "*"
            Next
        Else                                     ' Start at the end
            tmp = Right$(value,num)
            For i = Len(value) To num+1 Step -1
                tmp = "*" + tmp
            Next
        End If
    Else
        tmp = value
    End If
    maskText = tmp
End Function

This Post Has 4 Comments

  1. Palmi

    Thanks Karl for shearing

  2. Stevan Bajić

    This should be IMHO faster than using a For/Next loop:

    Function maskText(value As String, num As Integer, direction As String) As String
    Dim i As Integer
    i = Len(value)
    If i>num Then
    If Left$(UCase$(direction),1)={B} Then ‘ Start at the beginning
    maskText = Left$(value,num)+String$(i-num,{*})
    Else ‘ Start at the end
    maskText = String$(i-num,{*})+Right$(value,num)
    End If
    Else
    maskText = value
    End If
    End Function

    1. Karl-Henry Martinsson

      Very true, and it is very elegant to use String$() that way.
      One improvement to my code that I was considering was to not mask any spaces that may occur in the string, and then your solution won’t work, but I don’t see a huge use for that change, and then your solution is better/faster.
      I think I might profile the two solutions, would be interesting to see how much time we save with your code. 🙂

      1. Stevan Bajić

        This one should skip spaces:
        Function maskText(value As String, num As Integer, direction As String) As String
        Dim i,j As Integer
        Dim v As Variant
        Dim part As String
        i=Len(value)
        If i>num Then
        If Left$(UCase(direction),1)={B} Then
        part=Right$(value,i-num)
        Else
        part=Left$(value,i-num)
        End If
        v=Split(part,{ },-1,0)
        For j=LBound(v) To UBound(v)
        v(j)=String$(Len(v(j)),{*})
        Next
        If Left$(UCase(direction),1)={B} Then
        maskText=Left$(value,num)+Join(v,{ })
        Else
        maskText=Join(v,{ })+Right$(value,num)
        End If
        Else
        maskText=value
        End if
        End Function

Leave a Reply