In this post under DataMask, I will show with example how to side mask a simple text.
Side masking of a text means masking the sides of text leaving the middle portion of text intact.
So if the text is “insomnia” and I want to mask 2 characters from left and 3 characters from right, the result should be “**som***”.
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 Example3 { public static void main(String[] args) throws DataMaskException { MaskInformationDTO maskInformationDTO = MaskInformationDTO.builder() .maskType(MaskType.SIDE_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 “SIDE_MASKING” and data format type as “TEXT”.
We are also telling library to mask 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 side portion of the text.
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
**som***
In this way we can do side masking of simple text.