using System; using System.Collections.Generic; using System.IO; using System.Linq; using System.Net; using System.Security.Cryptography; using System.Text; using System.Threading.Tasks; using WebService.SDK; namespace KeRuYunClient { public class KeruyunHelper { #region 方法 -> SHA256加密 /// /// SHA256加密 /// /// /// public static string SHA256EncryptString(string data) { byte[] bytes = Encoding.UTF8.GetBytes(data); byte[] hash = SHA256Managed.Create().ComputeHash(bytes); StringBuilder builder = new StringBuilder(); for (int i = 0; i < hash.Length; i++) { builder.Append(hash[i].ToString("x2")); } return builder.ToString(); } /// /// SHA256加密 /// /// 待加密字符串 /// 加密数组 public static Byte[] SHA256EncryptByte(string StrIn) { var sha256 = new SHA256Managed(); var Asc = new ASCIIEncoding(); var tmpByte = Asc.GetBytes(StrIn); var EncryptBytes = sha256.ComputeHash(tmpByte); sha256.Clear(); return EncryptBytes; } #endregion #region 方法 -> get请求数据 public static string HttpUrlGet(string url) { string reString = ""; //根据url创建HttpWebRequest对象 HttpWebRequest objRequest = (HttpWebRequest)WebRequest.Create(url); objRequest.Method = "get"; //读取服务器返回信息 HttpWebResponse objResponse = (HttpWebResponse)objRequest.GetResponse(); //using作为语句,用于定义一个范围,在此范围的末尾将释放对象 using (StreamReader sr = new StreamReader(objResponse.GetResponseStream())) { //ReadToEnd适用于小文件的读取,一次性的返回整个文件 reString = sr.ReadToEnd(); sr.Close(); } return reString; } #endregion #region 方法 -> 获取时间戳 /// /// 获取时间戳,为真时获取10位(秒)时间戳(Unix),为假时获取13位(毫秒)时间戳 /// /// true:秒,false:毫秒 /// public static long GetTimeStamp(DateTime dt, bool bflag) { System.DateTime startTime = TimeZone.CurrentTimeZone.ToLocalTime(new System.DateTime(1970, 1, 1)); // 当地时区 TimeSpan ts = dt - startTime; long ret = 0; if (bflag) ret = Convert.ToInt64(ts.TotalSeconds); else ret = Convert.ToInt64(ts.TotalMilliseconds); return ret; } #endregion #region 方法 -> 将时间戳转换为DateTime时间 /// /// 将时间戳转换为DateTime时间,bSecond为true:秒,bSecond为false:毫秒 /// /// /// true:秒,bSecond为false:毫秒 /// public static DateTime TimeStampToDateTime(long timestamp, bool bSecond) { System.DateTime startTime = TimeZone.CurrentTimeZone.ToLocalTime(new System.DateTime(1970, 1, 1)); // 当地时区 if (bSecond) { return startTime.AddSeconds(timestamp); } else { return startTime.AddMilliseconds(timestamp); } } #endregion } }