2025-03-28 09:49:56 +08:00

115 lines
3.8 KiB
C#
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

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加密
/// <summary>
/// SHA256加密
/// </summary>
/// <param name="data"></param>
/// <returns></returns>
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();
}
/// <summary>
/// SHA256加密
/// </summary>
/// <param name="StrIn">待加密字符串</param>
/// <returns>加密数组</returns>
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 ->
/// <summary>
/// 获取时间戳,为真时获取10位(秒)时间戳(Unix),为假时获取13位(毫秒)时间戳
/// </summary>
/// <param name="bflag">truefalse毫秒</param>
/// <returns></returns>
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时间
/// <summary>
/// 将时间戳转换为DateTime时间bSecond为truebSecond为false毫秒
/// </summary>
/// <param name="timestamp"></param>
/// <param name="bSecond">truebSecond为false毫秒</param>
/// <returns></returns>
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
}
}