-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
♻ refactor: mask sensitive info (#729)
- Loading branch information
Showing
3 changed files
with
48 additions
and
2 deletions.
There are no files selected for viewing
46 changes: 46 additions & 0 deletions
46
src/Masa.Stack.Components/Extensions/TextFormattingExtensions.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
namespace Masa.Stack.Components.Extensions; | ||
|
||
public static class TextFormattingExtensions | ||
{ | ||
public static string MaskPhoneNumber(this string phoneNumber) | ||
{ | ||
if (phoneNumber.Length == 11) | ||
{ | ||
return phoneNumber.Substring(0, 3) + "****" + phoneNumber.Substring(7); | ||
} | ||
return phoneNumber; | ||
} | ||
|
||
public static string MaskIdCard(this string idCard) | ||
{ | ||
if (idCard.Length == 18) | ||
{ | ||
return idCard.Substring(0, 6) + "********" + idCard.Substring(14); | ||
} | ||
return idCard; | ||
} | ||
|
||
public static string MaskAccount(this string account) | ||
{ | ||
if (account.Length > 2) | ||
{ | ||
return account.Substring(0, 1) + new string('*', account.Length - 2) + account.Substring(account.Length - 1); | ||
} | ||
else if (account.Length == 2) | ||
{ | ||
return account.Substring(0, 1) + "*"; | ||
} | ||
return account; | ||
} | ||
|
||
public static string MaskSensitiveInfo(this string input, string type) | ||
{ | ||
return type switch | ||
{ | ||
"phone" => input.MaskPhoneNumber(), | ||
"idCard" => input.MaskIdCard(), | ||
"account" => input.MaskAccount(), | ||
_ => input | ||
}; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters