In this post under DataMask, I will show with example how to do middle masking of a simple delimited text excluding masking of delimiter.
So if the text is “1111-2222-3333-4444”, the output should be “111*-****-**33-4444”
As you can see from the output the numbers are masked in the middle excluding delimiters.
Below is the complete main class for your reference
Example8
package core;import io.github.freewarelabs.datamask.core.DataFormatType;import io.github.freewarelabs.datamask.core.DataMaskManager;import io.github.freewarelabs.datamask.core.MaskInformationDTO;import io.github.freewarelabs.datamask.core.MaskType;import io.github.freewarelabs.datamask.core.exception.DataMaskException;public class Example8 { public static void main(String[] args) throws DataMaskException { MaskInformationDTO maskInformationDTO = MaskInformationDTO.builder() .maskType(MaskType.MIDDLE_MASKING) .delimiter("-") .leftCharacterCount(3) .rightCharacterCount(6) .dataFormatType(DataFormatType.TEXT).build(); DataMaskManager dataMaskManager = new DataMaskManager(); String result = dataMaskManager.maskText(maskInformationDTO, "1111-2222-3333-4444"); System.out.println(result); }}
In the above code, at line 11, I create and configure an instance of “MaskInformationDTO” class.
While configuring “MaskInformationDTO” instance I am setting the mask type to “MIDDLE_MASKING” and data format type as “TEXT”.
Please note I am also configuring delimiter by calling “delimiter” method.
This will tell “DataMaskManager” instance that the input data format is a simple text with delimiter and it has to mask in the middle excluding the delimiter “-“.
I also mentioned how much characters from the left should be excluded using “leftCharacterCount” method and how much characters from the right should be excluded using “rightCharacterCount” method.
At line 18, I create an instance of “DataMaskManager” class.
At line 19, I call non-static “maskText” method of “DataMaskManager” class and pass instance of “MaskInformationDTO” created at line 11 and actual text to be masked as an argument.
This method returns a masked text which is printed to the console.
Below is the output
Output
111*-****-**33-4444
In this way we can do middle masking of a simple delimited text excluding masking of delimiter.