CreateHmac() public static method

生成请求hmac签名
public static CreateHmac ( String>.Dictionary data ) : string
data String>.Dictionary
return string
Esempio n. 1
0
        protected void Button1_Click(object sender, EventArgs e)
        {
            StringBuilder log = new StringBuilder();

            log.Append(DateTime.Now.ToString() + "\n");
            log.Append("测试功能:" + theme.InnerText + "\n");


            //***********************修改内容****************************

            string requestUrl = APIURLConfig.pay;

            string[] list = { "p0_Cmd", "p1_MerId", "p2_Order", "p3_Amt", "p4_Cur", "p5_Pid", "p6_Pcat", "p7_Pdesc", "p8_Url", "p9_SAF", "pa_MP", "pd_FrpId", "pm_Period", "pm_Period", "pr_NeedResponse", "pt_UserName", "pt_Address", "pt_TeleNo", "pt_Mobile", "pt_Email", "pt_LeaveMessage" };


            //***********************************************************


            log.Append("请求地址:" + requestUrl + "\n");
            log.Append("商户编号:" + p1_MerId.Value + "\n");
            log.Append("商户密钥:" + CustomerConfig.merchantKey + "\n");


            //存储前台数据
            string data = "";

            //循环生成
            foreach (string listname in list)
            {
                if (Request[listname] != "")
                {
                    data = data + Request[listname];
                }
            }

            //生成hmac签名
            string hmac = Digest.CreateHmac(data);

            log.Append("加密的字符串:" + data + "\n");
            log.Append("请求hmac:" + hmac + "\n");

            //循环生成请求数据
            data = "";
            foreach (string listname in list)
            {
                if (Request[listname] != "")
                {
                    data = data + listname + "=" + Request[listname] + "&";
                }
            }

            data = data + "hmac=" + hmac;

            log.Append("请求链接:" + requestUrl + "?" + data + "\n");


            SoftLog.LogStr(log.ToString(), theme.InnerText);

            Response.Redirect(requestUrl + "?" + data);
        }
Esempio n. 2
0
        protected void Button1_Click(object sender, EventArgs e)
        {
            StringBuilder log = new StringBuilder();

            log.Append(DateTime.Now.ToString() + "\n");
            log.Append("测试功能:" + theme.InnerText + "\n");


            //***********************修改内容****************************

            string requestUrl = APIURLConfig.QueryableOrder;

            string[] list          = { "p0_Cmd", "p1_MerId", "pb_TrxId", "pv_Ver" };
            string[] list_response = { "r0_Cmd",
                                       "r1_Code" };

            //***********************************************************


            log.Append("请求地址:" + requestUrl + "\n");
            log.Append("商户编号:" + p1_MerId.Value + "\n");
            log.Append("商户密钥:" + CustomerConfig.merchantKey + "\n");

            //存储前台数据
            string data_hmac = "";

            //循环生成
            foreach (string listname in list)
            {
                if (Request[listname] != "")
                {
                    data_hmac = data_hmac + Request[listname];
                }
            }

            //生成hmac签名
            string hmac = Digest.CreateHmac(data_hmac);

            log.Append("请求加密的字符串:" + data_hmac + "\n");
            log.Append("请求hmac:" + hmac + "\n");

            //循环生成要请求链接的数据
            string data_request = "";

            foreach (string listname in list)
            {
                data_request = data_request + listname + "=" + Request[listname] + "&";
            }
            //最终字符串
            data_request = data_request + "hmac=" + hmac;

            log.Append("请求链接:" + requestUrl + "?" + data_request + "\n");

            //发出请求
            string reqResult = YJPayUtil.payAPIRequestOnlince(requestUrl, data_request, true);

            log.Append("返回的原始信息:" + reqResult + "\n");

            //存储响应信息
            SortedDictionary <string, string> sd = new SortedDictionary <string, string>();

            //循环存储response
            foreach (string listname in list_response)
            {
                sd.Add(listname, FormatQueryString.GetQueryString(listname, reqResult, '\n'));
            }
            sd.Add("hmac", FormatQueryString.GetQueryString("hmac", reqResult, '\n'));


            string response_json = Newtonsoft.Json.JsonConvert.SerializeObject(sd);

            string type = "";

            if (sd["r1_Code"] == "1")
            {
                //回调验证签名
                string response_data = "";

                //循环生成
                foreach (string listname in list_response)
                {
                    response_data = response_data + sd[listname];
                }

                //回调信息生成HMAC
                string hmac_location = Digest.CreateHmac(response_data);


                //验证签名
                StringComparer comparer = StringComparer.OrdinalIgnoreCase;
                if (0 == comparer.Compare(hmac_location, sd["hmac"]))
                {
                    type = "验证签名成功";
                }
                else
                {
                    type = "验证签名失败";
                }
            }
            else
            {
                type = "请检查数据";
            }

            //返回数据data
            string data = response_json.ToString();



            SoftLog.LogStr(log.ToString(), theme.InnerText);

            //跳转页面
            Response.Redirect("http://localhost:58903/CallBack.aspx?data=" + data + "&type=" + type);
        }