feat: add HideMiddleDigits

This commit is contained in:
AnonymousDotNet 2024-11-25 14:02:55 +08:00
parent 0d95262bb3
commit dc078564ae
2 changed files with 36 additions and 3 deletions

View file

@ -23,7 +23,7 @@ public static class Utilities
var data = sha256.ComputeHash(Encoding.UTF8.GetBytes(text));
var sb = new StringBuilder();
foreach(var c in data)
foreach (var c in data)
{
sb.Append(c.ToString("x2"));
}
@ -47,4 +47,36 @@ public static class Utilities
memcache.Compact(100);
}
}
public static string HideMiddleDigits(string input, bool isEmail = false)
{
if (string.IsNullOrWhiteSpace(input))
{
return input;
}
if (isEmail)
{
int atIndex = input.IndexOf('@');
if (atIndex > 1)
{
string localPart = input.Substring(0, atIndex);
if (localPart.Length > 2)
{
string maskedLocalPart = $"{localPart[0]}{new string('*', localPart.Length - 2)}{localPart[^1]}";
return $"{maskedLocalPart}@{input.Substring(atIndex + 1)}";
}
}
}
else
{
if (input.Length > 6)
{
return $"{input.Substring(0, 3)}{new string('*', input.Length - 6)}{input.Substring(input.Length - 3)}";
}
}
return input;
}
}

View file

@ -1,4 +1,5 @@
using BotSharp.Abstraction.Users.Enums;
using BotSharp.Core.Infrastructures;
using System.Text.Json.Serialization;
namespace BotSharp.OpenAPI.ViewModels.Users;
@ -57,8 +58,8 @@ public class UserViewModel
UserName = user.UserName,
FirstName = user.FirstName,
LastName = user.LastName,
Email = user.Email,
Phone = !string.IsNullOrWhiteSpace(user.Phone) ? user.Phone.Replace("+86", String.Empty) : user.Phone,
Email = Utilities.HideMiddleDigits(user.Email, true),
Phone = Utilities.HideMiddleDigits((!string.IsNullOrWhiteSpace(user.Phone) ? user.Phone.Replace("+86", String.Empty) : user.Phone)),
Type = user.Type,
Role = user.Role,
Source = user.Source,