1. Introduction
1.导言
Privacy and data security are important elements of software development. Masking sensitive details such as the user’s email address and phone number is usually one procedure used to safeguard user information and prevent its disclosure.
隐私和数据安全是软件开发的重要因素。屏蔽用户的电子邮件地址和电话号码等敏感信息通常是用于保护用户信息并防止其泄露的一种程序。
In this tutorial, we’ll investigate how to mask email addresses and phone numbers in Java.
在本教程中,我们将研究如何在 Java 中屏蔽电子邮件地址和电话号码。
2. Masking Email Addresses
2.屏蔽电子邮件地址
2.1. Using String Manipulation
2.1.使用字符串操作
String manipulation is one of the ways to hide an email by editing the characters and replacing a few with asterisks. Here’s a simple Java code snippet demonstrating this approach:
字符串操作是通过编辑字符并用星号替换一些字符来隐藏电子邮件的方法之一。下面是演示这种方法的简单 Java 代码片段:
String email = "testemailaddress@example.com";
String expectedMaskedEmail = "te**************@example.com";
@Test
public void givenEmailAddress_whenUsingStringManipulation_thenMaskEmail() {
int atIndex = email.indexOf('@');
String repeatedString = IntStream.range(0, atIndex - 2).mapToObj(i -> "*").collect(Collectors.joining());
String maskedPart = email.substring(0, atIndex - repeatedString.length()) + repeatedString;
String maskedEmail = maskedPart + email.substring(atIndex);
assertEquals(expectedMaskedEmail, maskedEmail);
}
In the given example, we first find the index of the “@” character in the email address. Then, we generate a string of asterisks with a length equal to atIndex-2 using Java’s Stream API and string operations. Note that we subtract 2 digits from the atIndex to make the first two digits of the email.
在给出的示例中,我们首先找到电子邮件地址中”@”字符的索引。然后,我们使用 Java 的流 API 和字符串操作生成一个长度等于 atIndex-2 的星号字符串。注意,我们从 atIndex 中减去 2 个数字,得到 email 的前两位数字。
The maskedPart of the email is generated by combining the characters before the “@” symbol with the generated string of asterisks in the context of the provided Java code.
电子邮件的屏蔽部分是通过将”@”符号前的字符与所提供的 Java 代码中生成的星号字符串相结合而生成的。
The email address is then obtained by concatenating the maskedPart and the generated asterisks. Finally, we use the assertEquals() method to verify that the maskedEmail is the same as the expectedMaskedEmail.
然后,将 maskedPart 和生成的星号连接起来,即可获得电子邮件地址。最后,我们使用 assertEquals() 方法验证 maskedEmail 是否与 expectedMaskedEmail 相同。
2.2. Using Regular Expressions
2.2.使用正则表达式
Another method is to implement regular expressions to conceal the email address. Here’s an example:
另一种方法是使用正则表达式来隐藏电子邮件地址。下面是一个示例:
@Test
public void givenEmailAddress_whenUsingRegex_thenMaskEmail() {
int atIndex = email.indexOf('@');
String regex = "(.{2})(.*)(@.*)";
String repeatedAsterisks = "*".repeat(atIndex - 2);
String maskedEmail = email.replaceAll(regex, "$1" + repeatedAsterisks + "$3");
assertEquals(expectedMaskedEmail, maskedEmail);
}
In the above test method, we first determine the index of the “@” symbol in the email using the indexOf() method. Then, we use the regular expression regex “(.{2})(.*)(@.*)” to capture three groups: the first two characters, the characters between them and the “@” symbol, and the characters following the “@“.
在上述测试方法中,我们首先使用 indexOf() 方法确定 email 中”@“符号的索引。然后,我们使用正则表达式 regex“(.{2})(.*)(@.*)” 捕捉三组字符:前两个字符、它们与”@“符号之间的字符以及”@“之后的字符。
Subsequently, the variable repeatedAsterisks is assigned a string of asterisks with a length corresponding to atIndex -2. Finally, the replaceAll() method applies the regex pattern, replacing the middle part of the email with the generated asterisks.
随后,变量 repeatedAsterisks 将被分配一个长度与 atIndex -2 相对应的星号字符串。最后,replaceAll() 方法将应用 regex 模式,用生成的星号替换 email 的中间部分。
3. Masking Phone Numbers
3.屏蔽电话号码
3.1. Using String Manipulation
3.1.使用字符串操作
We can also mask phone numbers by performing some character manipulations. Here’s an example:
我们还可以通过一些字符操作来屏蔽电话号码。下面是一个例子:
String phoneNumber = "+1234567890";
String expectedMaskedPhoneNumber = "+******7890";
@Test
public void givenPhoneNumber_whenUsingStringManipulation_thenMaskPhone() {
String maskedPhoneNumber = phoneNumber.replaceAll("\\d(?=\\d{4})", "*");
assertEquals(expectedMaskedPhoneNumber, maskedPhoneNumber);
}
Here, we pass the regular expression “\\d(?=\\d{4})” to the replaceAll() method, aiming to identify and replace all numeric digits that are followed by four more digits with asterisks.
在这里,我们将正则表达式”\d(?=\d{4})“传递给 replaceAll() 方法,目的是识别并替换所有后面跟有星号的四位数字。
3.2. Using Regular Expressions
3.2.使用正则表达式
Similarly to the method used for masking email addresses, regular expressions can be implemented to hide the phone numbers properly. Here’s a Java code snippet demonstrating this method:
与屏蔽电子邮件地址的方法类似,也可以使用正则表达式来适当隐藏电话号码。下面是演示这种方法的 Java 代码片段:
@Test
public void givenPhoneNumber_whenUsingRegex_thenMaskPhone() {
int lastDigitsIndex = phoneNumber.length() - 5;
String regex = "(\\+)(\\d+)(\\d{4})";
String repeatedAsterisks = "*".repeat(Math.max(0, lastDigitsIndex));
String maskedPhoneNumber = phoneNumber.replaceAll(regex, "$1" + repeatedAsterisks + "$3");
assertEquals(expectedMaskedPhoneNumber, maskedPhoneNumber);
}
In the above code snippet, we define a regular expression regex “(\\+)(\\d+)(\\d{4})” that captures three groups: the plus sign, the leading digits, and the last four digits.
在上面的代码片段中,我们定义了一个正则表达式 regex “(\+)(\d+)(\d{4})“,它捕获了三组数字:加号、前导数字和最后四位数字。
Subsequently, we generate a string repeatedAsterisks of repeated asterisks based on the calculated lastDigitsIndex. Then, we use the replaceAll() method to apply the regex pattern, replacing the middle digits with asterisks.
随后,我们根据计算出的 lastDigitsIndex 生成一个由重复星号组成的字符串 repeatedAsterisks 。然后,我们使用 replaceAll() 方法应用 regex 模式,用星号替换中间的数字。
4. Conclusion
4.结论
In conclusion, the masking of sensitive information is critical for protecting user privacy and adhering to data security regulations. Hence, we see in this tutorial how to utilize mechanisms such as string manipulation and regular expressions to mask email addresses and phone numbers.
总之,屏蔽敏感信息对于保护用户隐私和遵守数据安全法规至关重要。因此,我们将在本教程中了解如何利用字符串操作和正则表达式等机制来屏蔽电子邮件地址和电话号码。
As always, the complete code samples for this article can be found over on GitHub.
与往常一样,本文的完整代码示例可在 GitHub 上找到。