irc-netcore-api/IRaCIS.Core.Application/Helper/Email/IRCEmailPasswordHelper.cs

78 lines
2.6 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

namespace IRaCIS.Core.Application.Helper;
public static class IRCEmailPasswordHelper
{
private static readonly Random Random = new Random();
//显示位数3分之2的位数向上取整
//取哪几个个值:最后一位和前面几位
//其他3个***。
//比如hlj23@126.com
//为hlj***3@126.com
//he@126.com
//为h*** e@126.com
public static string MaskEmail(string email)
{
// 找到 "@" 符号的位置
int atIndex = email.IndexOf('@');
string visiblePartBefore = email.Substring(0, atIndex);
string afterAt = email.Substring(atIndex + 1);
int visibleLength = (int)Math.Ceiling((double)visiblePartBefore.Length * 2 / 3);
// 替换中间两位字符为星号
string hiddenPartBeforeAt = visiblePartBefore.Substring(0, visibleLength - 1) + "***" + visiblePartBefore.Last();
// 组合隐藏和可见部分
string hiddenEmail = hiddenPartBeforeAt + "@" + afterAt;
return hiddenEmail;
}
/// <summary>
/// 密码必须包含18 32 个字符2至少1个数字3) 至少1个大写字母4至少1个小写字母5至少1个特殊字符 (~!-@#$%^&*_+?)
/// </summary>
/// <returns></returns>
public static string GenerateRandomPassword(int length)
{
// 必须包含的字符组
const string numbers = "0123456789";
const string upperCaseLetters = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
const string lowerCaseLetters = "abcdefghijklmnopqrstuvwxyz";
const string specialCharacters = "~!-@#$%^&*_+?";
// 随机选择至少一个字符
char[] requiredCharacters =
{
numbers[Random.Next(numbers.Length)],
upperCaseLetters[Random.Next(upperCaseLetters.Length)],
lowerCaseLetters[Random.Next(lowerCaseLetters.Length)],
specialCharacters[Random.Next(specialCharacters.Length)]
};
// 构建剩余的字符集,用于填充密码的其余部分
string allCharacters = numbers + upperCaseLetters + lowerCaseLetters + specialCharacters;
// 确保密码长度满足用户要求
char[] password = new char[length];
// 将必须包含的字符放入密码中
requiredCharacters.CopyTo(password, 0);
// 填充剩余的字符
for (int i = requiredCharacters.Length; i < length; i++)
{
password[i] = allCharacters[Random.Next(allCharacters.Length)];
}
// 随机打乱密码字符顺序
return new string(password.OrderBy(_ => Random.Next()).ToArray());
}
}