using System;
using System.Collections;
using System.Collections.Generic;
using System.Text;
namespace SDKCSharp.Utility
{
///
/// 签名工具类
///
public class SignUtil
{
///
/// 构建签名
///
/// The sign.
/// 参数.
/// 私钥.
/// 字符集.
/// 签名类型.
public static string CreateSign(IDictionary parameters, string privateKey, Encoding charset, SignType signType)
{
RSAHelper rsa = new RSAHelper(signType, charset, privateKey, null);
string content = GetSignContent(parameters);
return rsa.Sign(content);
}
///
/// 构建签名内容
///
/// The sign content.
/// Parameters.
public static string GetSignContent(IDictionary parameters)
{
// 第一步:把字典按Key的字母顺序排序
IDictionary sortedParams = new SortedDictionary(parameters);
IEnumerator> 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;
}
}
}