mirror of
https://gitee.com/durcframework/SOP.git
synced 2025-08-11 21:57:56 +08:00
C#SDK提交
This commit is contained in:
@@ -1,6 +1,7 @@
|
||||
using System;
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using System.Text;
|
||||
|
||||
namespace SDKCSharp.Utility
|
||||
{
|
||||
@@ -11,16 +12,45 @@ namespace SDKCSharp.Utility
|
||||
{
|
||||
|
||||
/// <summary>
|
||||
/// 构建签名。
|
||||
/// 构建签名
|
||||
/// </summary>
|
||||
/// <returns>The sign.</returns>
|
||||
/// <param name="parameters">参数.</param>
|
||||
/// <param name="privateKeyPem">私钥.</param>
|
||||
/// <param name="privateKey">私钥.</param>
|
||||
/// <param name="charset">字符集.</param>
|
||||
/// <param name="signType">签名类型.</param>
|
||||
/// <returns>返回签名.</returns>
|
||||
public static string CreateSign(IDictionary<string, string> parameters, string privateKeyPem, string charset, bool isFromFile, string signType)
|
||||
public static string CreateSign(IDictionary<string, string> parameters, string privateKey, Encoding charset, SignType signType)
|
||||
{
|
||||
return AlipaySignature.RSASign(parameters, privateKeyPem, charset, isFromFile, signType);
|
||||
RSAHelper rsa = new RSAHelper(signType, charset, privateKey, null);
|
||||
string content = GetSignContent(parameters);
|
||||
return rsa.Sign(content);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 构建签名内容
|
||||
/// </summary>
|
||||
/// <returns>The sign content.</returns>
|
||||
/// <param name="parameters">Parameters.</param>
|
||||
public static string GetSignContent(IDictionary<string, string> parameters)
|
||||
{
|
||||
// 第一步:把字典按Key的字母顺序排序
|
||||
IDictionary<string, string> sortedParams = new SortedDictionary<string, string>(parameters);
|
||||
IEnumerator<KeyValuePair<string, string>> dem = sortedParams.GetEnumerator();
|
||||
|
||||
// 第二步:把所有参数名和参数值串在一起
|
||||
StringBuilder query = new StringBuilder("");
|
||||
while (dem.MoveNext())
|
||||
{
|
||||
string key = dem.Current.Key;
|
||||
string value = dem.Current.Value;
|
||||
if (!string.IsNullOrEmpty(key) && !string.IsNullOrEmpty(value))
|
||||
{
|
||||
query.Append(key).Append("=").Append(value).Append("&");
|
||||
}
|
||||
}
|
||||
string content = query.ToString().Substring(0, query.Length - 1);
|
||||
|
||||
return content;
|
||||
}
|
||||
|
||||
}
|
||||
|
Reference in New Issue
Block a user