51 lines
1.4 KiB
C#
51 lines
1.4 KiB
C#
using System.Security.Cryptography;
|
|
using System.Text;
|
|
using System.Text.RegularExpressions;
|
|
|
|
namespace InterfaceForward.Application.Helpers;
|
|
|
|
public class StringHelper
|
|
{
|
|
/// <summary>
|
|
/// 只能输入英文字符和键盘上常用的符号
|
|
/// </summary>
|
|
/// <param name="input"></param>
|
|
/// <returns></returns>
|
|
public static bool IsValidEnglishString(string input)
|
|
{
|
|
if (string.IsNullOrEmpty(input))
|
|
{
|
|
return false;
|
|
}
|
|
|
|
// Regular expression to match only English letters and specified symbols
|
|
var regex = new Regex(@"^[a-zA-Z\(\)@!#$%^&*_\-+=;:""'/><,.\s]+$");
|
|
return regex.IsMatch(input);
|
|
}
|
|
|
|
|
|
/// <summary>获取字符串的hash值</summary>
|
|
/// <param name="str"></param>
|
|
/// <returns></returns>
|
|
public static string GetHash(string str)
|
|
{
|
|
if (string.IsNullOrEmpty(str)) return string.Empty;
|
|
|
|
// 将字符串转换为字节
|
|
byte[] data = Encoding.UTF8.GetBytes(str);
|
|
|
|
// 创建SHA256实例
|
|
using SHA256 sha256 = SHA256.Create();
|
|
// 计算哈希值
|
|
byte[] hash = sha256.ComputeHash(data);
|
|
|
|
// 将哈希值转换为16进制字符串
|
|
StringBuilder sb = new StringBuilder();
|
|
foreach (byte b in hash)
|
|
{
|
|
sb.Append(b.ToString("x2"));
|
|
}
|
|
|
|
return sb.ToString();
|
|
}
|
|
} |