using System; using System.Security.Cryptography; using System.Text; using System.Text.Encodings.Web; using System.Text.Json; using System.Text.RegularExpressions; using System.Text.Unicode; namespace SimApi.Helpers { public static class SimApiUtil { /// /// 检测手机号是否正确 /// /// 手机号码 /// public static bool CheckCell(string cell) { var regex = new Regex("^1[3456789]\\d{9}$"); return regex.IsMatch(cell); } /// /// MD5加密字符串 /// /// 源字符串 /// 加密结果"x2"结果为32位,"x3"结果为48位,"x4"结果为64位 /// public static string Md5(string source, string mode = "x2") { byte[] sor = Encoding.UTF8.GetBytes(source); MD5 md5 = MD5.Create(); byte[] result = md5.ComputeHash(sor); StringBuilder strbul = new StringBuilder(40); for (int i = 0; i < result.Length; i++) { strbul.Append(result[i].ToString(mode)); } return strbul.ToString(); } /// /// 将对象序列化成JSON (控制台输出中文不会被编码) /// /// /// public static string Json(object obj) { return JsonSerializer.Serialize(obj, new JsonSerializerOptions { Encoder = JavaScriptEncoder.Create(UnicodeRanges.All) }); } /// /// 获取 CST 当前时间 /// /// public static DateTime GetCstNow() { return DateTime.UtcNow.AddHours(8); } } }