Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

微信支付现已支持HMAC-SHA256签名方式 #1351

Merged
merged 1 commit into from
Aug 25, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,14 @@ public TenPayV3MicroPayRequestData(string appId, string mchId, string key, strin
PackageRequestHandler.SetParameter("spbill_create_ip", this.SpbillCreateIp); //终端IP
PackageRequestHandler.SetParameter("goods_tag", this.GoodsTag); //商品标记
PackageRequestHandler.SetParameter("auth_code", this.AuthCode); //授权码
Sign = PackageRequestHandler.CreateMd5Sign("key", this.Key);

// TODO:★554393109 修改
//***************************************************************************************
Sign = "MD5".Equals(signType, System.StringComparison.OrdinalIgnoreCase)
? PackageRequestHandler.CreateMd5Sign("key", this.Key)
: PackageRequestHandler.CreateSha256Sign("key", this.Key);
//***************************************************************************************

PackageRequestHandler.SetParameter("sign", Sign); //签名

#endregion
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ public RequestHandler(HttpContext httpContext)
{
Parameters = new Hashtable();
#if NET35 || NET40 || NET45 || NET461
this.HttpContext = httpContext ?? HttpContext.Current;
this.HttpContext = httpContext ?? HttpContext.Current;
#else
this.HttpContext = httpContext ?? new DefaultHttpContext();
#endif
Expand Down Expand Up @@ -214,6 +214,56 @@ public virtual string CreateMd5Sign(string key, string value)
return sign;
}





// TODO:★554393109 修改
//***************************************************************************************
/// <summary>
/// 创建sha256摘要,规则是:按参数名称a-z排序,遇到空值的参数不参加签名
/// </summary>
/// <param name="key">参数名</param>
/// <param name="value">参数值</param>
/// key和value通常用于填充最后一组参数
/// <returns></returns>
public virtual string CreateSha256Sign(string key, string value)
{
var sb = new StringBuilder();

var akeys = new ArrayList(Parameters.Keys);
akeys.Sort(ASCIISort.Create());

foreach (string k in akeys)
{
string v = (string)Parameters[k];
if (null != v && "".CompareTo(v) != 0
&& "sign".CompareTo(k) != 0
//&& "sign_type".CompareTo(k) != 0
&& "key".CompareTo(k) != 0)
{
sb.Append(k + "=" + v + "&");
}
}

sb.Append(key + "=" + value);

//string sign = EncryptHelper.GetMD5(sb.ToString(), GetCharset()).ToUpper();

//编码强制使用UTF8:https://pay.weixin.qq.com/wiki/doc/api/jsapi.php?chapter=4_1

// 现支持HMAC-SHA256签名方式(EncryptHelper.GetHmacSha256方法留待实现)
// https://pay.weixin.qq.com/wiki/doc/api/micropay.php?chapter=4_3
string sign = EncryptHelper.GetHmacSha256(sb.ToString(), "UTF-8").ToUpper();
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

当前方法直接从上述CreateMd5Sign中复制修改名称而来


return sign;
}
//***************************************************************************************





/// <summary>
/// 输出XML
/// </summary>
Expand Down