You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
SOP/sop-sdk/sdk-csharp/SDKCSharp/Utility/SignUtil.cs

94 lines
3.5 KiB

6 years ago
using System;
using System.Collections;
using System.Collections.Generic;
6 years ago
using System.Text;
5 years ago
using System.Text.RegularExpressions;
6 years ago
namespace SDKCSharp.Utility
{
/// <summary>
/// 签名工具类
/// </summary>
5 years ago
public static class SignUtil
6 years ago
{
/// <summary>
6 years ago
/// 构建签名
6 years ago
/// </summary>
6 years ago
/// <returns>The sign.</returns>
/// <param name="parameters">参数.</param>
6 years ago
/// <param name="privateKey">私钥.</param>
/// <param name="charset">字符集.</param>
/// <param name="signType">签名类型.</param>
6 years ago
public static string CreateSign(IDictionary<string, string> parameters, string privateKey, Encoding charset, SignType signType)
6 years ago
{
6 years ago
RSAHelper rsa = new RSAHelper(signType, charset, privateKey, null);
string content = GetSignContent(parameters);
return rsa.Sign(content);
}
5 years ago
public static bool RsaCheck(string content, string sign, string publicKeyPlatform, Encoding charset,
SignType signType)
{
RSAHelper rsa = new RSAHelper(signType, charset, null, publicKeyPlatform);
return rsa.Verify(content, sign);
}
6 years ago
/// <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;
6 years ago
}
5 years ago
/// <summary>
/// 构建签名内容
/// </summary>
/// <returns>The sign content.</returns>
/// <param name="parameters">Parameters.</param>
public static string GetSignContentObject(IDictionary<string, object> parameters)
{
// 第一步:把字典按Key的字母顺序排序
IDictionary<string, object> sortedParams = new SortedDictionary<string, object>(parameters);
IEnumerator<KeyValuePair<string, object>> dem = sortedParams.GetEnumerator();
// 第二步:把所有参数名和参数值串在一起
StringBuilder query = new StringBuilder("");
while (dem.MoveNext())
{
string key = dem.Current.Key;
string value = Convert.ToString(dem.Current.Value);
value = Regex.Replace(value, @"\s", "");
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;
}
6 years ago
}
}