Middle masking of simple text

In this post under DataMask, I will show with example how to middle mask a simple text.

Middle masking of a text means masking the middle portion of text leaving the side portion of text intact.

So if the text is “insomnia” and I want to leave first 2 characters and last 3 characters intact without masking, then the result will be “in***nia”

Below is the complete main class for your reference

Example3

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 Example4 {
public static void main(String[] args) throws DataMaskException {
MaskInformationDTO maskInformationDTO = MaskInformationDTO.builder()
.maskType(MaskType.MIDDLE_MASKING)
.leftCharacterCount(2)
.rightCharacterCount(3)
.dataFormatType(DataFormatType.TEXT).build();
DataMaskManager dataMaskManager = new DataMaskManager();
String result = dataMaskManager.maskText(maskInformationDTO, "insomnia");
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”.

We are also telling library to leave 2 characters on left by using “leftCharacterCount” method and 3 characters on right by using “rightCharacterCount” method.

This will tell “DataMaskManager” instance that the input data format is a simple text and it has to mask just the middle portion of the text, leaving the left 2 characters and right 3 characters intact.

At line 17, I create an instance of “DataMaskManager” class.

At line 18, 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 email which is printed to the console.

Below is the output

Output

in***nia

In this way we can do middle masking of simple text.

Leave a comment