コード例 #1
0
        /// <summary>
        /// 签名算法;支持hmac-sha256;md5
        /// </summary>
        /// <param name="message"></param>
        /// <param name="secret"></param>
        /// <param name="signMethod">哈希算法,默认HMACSHA256</param>
        /// <returns>签名16进制</returns>
        public virtual string GetSignhHash(string message, string secret, EncryptEnum signMethod = EncryptEnum.Default)
        {
            if (signMethod.HasFlag(EncryptEnum.Default) || signMethod.HasFlag(EncryptEnum.SignHMACSHA256))
            {
                return(SignCommon.GetHMACSHA256Sign(message, secret));
            }

            else if (signMethod.HasFlag(EncryptEnum.SignSHA256))
            {
                return(SignCommon.GetSHA256Sign(message, secret));
            }
            else if (signMethod.HasFlag(EncryptEnum.SignMD5))
            {
                return(SignCommon.CreateMD5(message, secret));
            }
            else
            {
                return(SignCommon.GetHMACSHA256Sign(message, secret));
            }
        }
コード例 #2
0
 /// <summary>
 /// 定义摘要算法
 /// </summary>
 /// <param name="message"></param>
 /// <param name="secret"></param>
 /// <param name="signMethod"></param>
 /// <returns></returns>
 public string GetSignhHash(string message, string secret, EncryptEnum signMethod = EncryptEnum.Default)
 {
     return("5555555");
 }
コード例 #3
0
ファイル: SignCommon.cs プロジェクト: yangchongyuan/NetPro
        /// <summary>
        /// 通用生成签名
        /// </summary>
        /// <param name="secret"></param>
        /// <param name="query">url参数,参数名统一小写;参数中不能包含时间戳,时间戳已内部处理,参数名为:timestamp</param>
        /// <param name="body">body参数</param>
        /// <param name="signMethod">算法名称:hmac-sha256;md5</param>
        /// <remarks><![CDATA[ 将url参数与body参数以'&'分割]]>
        /// 拼装新字符串utf-8编码后
        /// HMACSHA256摘要后转16进制小写
        /// </remarks>
        /// <returns></returns>
        public static string CreateSign(string secret, NameValueCollection query, object body = null, EncryptEnum signMethod = EncryptEnum.Default, bool prinftLog = false)
        {
            IDictionary <string, string> queryDic = new Dictionary <string, string>();

            foreach (var k in query.AllKeys)
            {
                queryDic.Add(k, query[k]);
            }

            if (queryDic == null || !queryDic.Any())
            {
                Console.WriteLine("签名公共参数必须以url方式提供");
                return(string.Empty);
            }

            if (body != null)
            {
                var jsonString = JsonSerializer.Serialize(body, new JsonSerializerOptions {
                    Encoder = JavaScriptEncoder.UnsafeRelaxedJsonEscaping
                });
                queryDic.Add("body", Regex.Replace(jsonString, @"\s(?=([^""]*""[^""]*"")*[^""]*$)", string.Empty));
            }

            var dicOrder = queryDic.OrderBy(s => s.Key, StringComparer.Ordinal).ToList();

            StringBuilder requestStr = new StringBuilder();

            for (int i = 0; i < dicOrder.Count(); i++)
            {
                if (i == dicOrder.Count() - 1)
                {
                    requestStr.Append($"{dicOrder[i].Key}={dicOrder[i].Value}");
                }
                else
                {
                    requestStr.Append($"{dicOrder[i].Key}={dicOrder[i].Value}&");
                }
            }

            var utf8Request = GetUtf8(requestStr.ToString());

            string result;

            if (signMethod.HasFlag(EncryptEnum.Default) || signMethod.HasFlag(EncryptEnum.SignHMACSHA256))
            {
                result = GetHMACSHA256Sign(utf8Request, secret);
            }
            else if (signMethod.HasFlag(EncryptEnum.SignSHA256))
            {
                result = GetSHA256Sign(utf8Request, secret);
            }
            else if (signMethod.HasFlag(EncryptEnum.SignMD5))
            {
                result = CreateMD5(utf8Request, secret);
            }
            else
            {
                result = GetHMACSHA256Sign(utf8Request, secret);
            }

            if (prinftLog)
            {
                Console.WriteLine($"拼装排序后的值==>{utf8Request};摘要计算后的值==>{result}");
            }

            return(result);
        }