mirror of
https://gitee.com/durcframework/SOP.git
synced 2025-08-11 21:57:56 +08:00
添加C#sdk
This commit is contained in:
69
sop-sdk/sdk-csharp/SDKCSharp/Utility/AESUtil.cs
Normal file
69
sop-sdk/sdk-csharp/SDKCSharp/Utility/AESUtil.cs
Normal file
@@ -0,0 +1,69 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
using System.Security.Cryptography;
|
||||
|
||||
namespace SDKCSharp.Utility
|
||||
{
|
||||
/// <summary>
|
||||
/// AES加解密.
|
||||
/// 字符集:UTF-8
|
||||
/// 算法模式:ECB
|
||||
/// 补码方式:PKCS7Padding
|
||||
/// 加密结果编码方式:Base64
|
||||
/// </summary>
|
||||
public class AESUtil
|
||||
{
|
||||
private static Encoding ENCODING_UTF8 = Encoding.UTF8;
|
||||
|
||||
/// <summary>
|
||||
/// AES 加密
|
||||
/// </summary>
|
||||
/// <param name="data">明文(待加密)</param>
|
||||
/// <param name="password">密文</param>
|
||||
/// <returns>返回Base64字符串</returns>
|
||||
public static string EncryptToBase64String(string data, string password)
|
||||
{
|
||||
if (string.IsNullOrEmpty(data)) return null;
|
||||
Byte[] toEncryptArray = ENCODING_UTF8.GetBytes(data);
|
||||
|
||||
RijndaelManaged rm = new RijndaelManaged
|
||||
{
|
||||
Key = ENCODING_UTF8.GetBytes(password),
|
||||
Mode = CipherMode.ECB,
|
||||
Padding = PaddingMode.PKCS7
|
||||
};
|
||||
|
||||
ICryptoTransform cTransform = rm.CreateEncryptor();
|
||||
Byte[] resultArray = cTransform.TransformFinalBlock(toEncryptArray, 0, toEncryptArray.Length);
|
||||
|
||||
return Convert.ToBase64String(resultArray, 0, resultArray.Length);
|
||||
}
|
||||
/// <summary>
|
||||
/// AES 解密
|
||||
/// </summary>
|
||||
/// <param name="data">密文(待解密)</param>
|
||||
/// <param name="password">密码</param>
|
||||
/// <returns>返回明文</returns>
|
||||
public static string DecryptFromBase64String(string data, string password)
|
||||
{
|
||||
if (string.IsNullOrEmpty(data)) return null;
|
||||
Byte[] toEncryptArray = Convert.FromBase64String(data);
|
||||
|
||||
RijndaelManaged rm = new RijndaelManaged
|
||||
{
|
||||
Key = ENCODING_UTF8.GetBytes(password),
|
||||
Mode = CipherMode.ECB,
|
||||
Padding = PaddingMode.PKCS7
|
||||
};
|
||||
|
||||
ICryptoTransform cTransform = rm.CreateDecryptor();
|
||||
Byte[] resultArray = cTransform.TransformFinalBlock(toEncryptArray, 0, toEncryptArray.Length);
|
||||
|
||||
return ENCODING_UTF8.GetString(resultArray);
|
||||
}
|
||||
}
|
||||
}
|
75
sop-sdk/sdk-csharp/SDKCSharp/Utility/ClassUtil.cs
Normal file
75
sop-sdk/sdk-csharp/SDKCSharp/Utility/ClassUtil.cs
Normal file
@@ -0,0 +1,75 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Collections;
|
||||
using System.Reflection;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
using SDKCSharp.Request;
|
||||
|
||||
namespace SDKCSharp.Utility
|
||||
{
|
||||
public class ClassUtil
|
||||
{
|
||||
/// <summary>
|
||||
/// 将普通对象转换成Dictionary
|
||||
/// </summary>
|
||||
/// <param name="obj"></param>
|
||||
/// <returns></returns>
|
||||
public static Dictionary<string, object> ConvertObjectToDictionary(object obj)
|
||||
{
|
||||
Dictionary<string, object> dict = new Dictionary<string, object>();
|
||||
|
||||
if (obj == null)
|
||||
{
|
||||
return dict;
|
||||
}
|
||||
// 得到请求对象的所有属性
|
||||
PropertyInfo[] properties = obj.GetType().GetProperties();
|
||||
|
||||
if (properties.Length <= 0)
|
||||
{
|
||||
return dict;
|
||||
}
|
||||
|
||||
foreach (PropertyInfo propertyInfo in properties)
|
||||
{
|
||||
if (IsIgnoreSignProperty(propertyInfo))
|
||||
{
|
||||
continue;
|
||||
}
|
||||
string name = propertyInfo.Name.ToLower();
|
||||
object value = propertyInfo.GetValue(obj, null);
|
||||
// Console.WriteLine("{0}:{1}", name, value);
|
||||
dict.Add(name, value);
|
||||
}
|
||||
|
||||
return dict;
|
||||
}
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// 被[IgnoreSign]标记的字段名,如果被标记的话就不加入到签名算法中
|
||||
/// </summary>
|
||||
/// <param name="propertyInfo"></param>
|
||||
/// <returns></returns>
|
||||
private static bool IsIgnoreSignProperty(PropertyInfo propertyInfo)
|
||||
{
|
||||
Type ignoreSignType = typeof(IgnoreSign);
|
||||
// 获取这个字段的元数据
|
||||
object[] attrs = propertyInfo.GetCustomAttributes(false);
|
||||
|
||||
foreach (object attr in attrs)
|
||||
{
|
||||
if (attr.GetType().Equals(ignoreSignType))
|
||||
{
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
}
|
||||
}
|
49
sop-sdk/sdk-csharp/SDKCSharp/Utility/FileUtil.cs
Normal file
49
sop-sdk/sdk-csharp/SDKCSharp/Utility/FileUtil.cs
Normal file
@@ -0,0 +1,49 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
using System.IO;
|
||||
|
||||
namespace SDKCSharp.Utility
|
||||
{
|
||||
public class FileUtil
|
||||
{
|
||||
|
||||
/// <summary>
|
||||
/// 获取文件名,带后缀
|
||||
/// </summary>
|
||||
/// <param name="filePath">文件全路径</param>
|
||||
/// <returns>返回文件名</returns>
|
||||
public static string GetFileName(string filePath)
|
||||
{
|
||||
return Path.GetFileName(filePath);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 读取文件,将文件内容转成byte数组
|
||||
/// </summary>
|
||||
/// <param name="filePath">文件路径</param>
|
||||
/// <returns></returns>
|
||||
public static byte[] ReadFile(string filePath)
|
||||
{
|
||||
return File.ReadAllBytes(filePath);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 将FileStream内容转成byte数组
|
||||
/// </summary>
|
||||
/// <param name="fs">FileStream</param>
|
||||
/// <returns></returns>
|
||||
public static byte[] ReadFile(FileStream fs)
|
||||
{
|
||||
byte[] buffer = new byte[fs.Length];
|
||||
using (BinaryWriter bw = new BinaryWriter(fs))
|
||||
{
|
||||
bw.Write(buffer);
|
||||
}
|
||||
return buffer;
|
||||
}
|
||||
}
|
||||
}
|
52
sop-sdk/sdk-csharp/SDKCSharp/Utility/JsonUtil.cs
Normal file
52
sop-sdk/sdk-csharp/SDKCSharp/Utility/JsonUtil.cs
Normal file
@@ -0,0 +1,52 @@
|
||||
using System;
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using Newtonsoft.Json;
|
||||
|
||||
namespace SDKCSharp.Utility
|
||||
{
|
||||
/// <summary>
|
||||
/// JSON序列化/反序列化工具
|
||||
/// 使用Newtonsoft.Json组件,详见:https://www.newtonsoft.com/json
|
||||
/// </summary>
|
||||
public class JsonUtil
|
||||
{
|
||||
|
||||
public const string EMPTY_JSON = "{}";
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// JSON字符串转化成对象
|
||||
/// </summary>
|
||||
/// <typeparam name="T"></typeparam>
|
||||
/// <param name="json"></param>
|
||||
/// <returns></returns>
|
||||
public static T ParseObject<T>(string json)
|
||||
{
|
||||
return JsonConvert.DeserializeObject<T>(json);// //反序列化
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// json字符串转换成Dictionary
|
||||
/// </summary>
|
||||
/// <returns>The to dictionary.</returns>
|
||||
/// <param name="json">Json.</param>
|
||||
public static Dictionary<string, object> ParseToDictionary(string json)
|
||||
{
|
||||
return JsonConvert.DeserializeObject<Dictionary<string, object>>(json);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 对象转换成json字符串
|
||||
/// </summary>
|
||||
/// <param name="obj"></param>
|
||||
/// <returns></returns>
|
||||
public static string ToJSONString(object obj) {
|
||||
if (obj == null)
|
||||
{
|
||||
return EMPTY_JSON;
|
||||
}
|
||||
return JsonConvert.SerializeObject(obj);
|
||||
}
|
||||
}
|
||||
}
|
71
sop-sdk/sdk-csharp/SDKCSharp/Utility/MD5Util.cs
Normal file
71
sop-sdk/sdk-csharp/SDKCSharp/Utility/MD5Util.cs
Normal file
@@ -0,0 +1,71 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
using System.Security.Cryptography;
|
||||
|
||||
namespace SDKCSharp.Utility
|
||||
{
|
||||
public class MD5Util
|
||||
{
|
||||
/// <summary>
|
||||
/// MD5加密,全部大写
|
||||
/// </summary>
|
||||
/// <param name="input"></param>
|
||||
/// <returns></returns>
|
||||
public static string EncryptToUpper(string input)
|
||||
{
|
||||
return Encrypt(input).ToUpper();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 返回长度16串,小写
|
||||
/// </summary>
|
||||
/// <param name="input"></param>
|
||||
/// <returns></returns>
|
||||
public static String Encrypt16(String input)
|
||||
{
|
||||
return Encrypt(input).Substring(8, 16);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// MD5加密,全部小写
|
||||
/// </summary>
|
||||
/// <param name="input"></param>
|
||||
/// <returns></returns>
|
||||
public static string Encrypt(string input)
|
||||
{
|
||||
return Encrypt(Encoding.UTF8.GetBytes(input));
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// MD5加密
|
||||
/// </summary>
|
||||
/// <param name="inputData"></param>
|
||||
/// <returns>返回小写字母</returns>
|
||||
public static string Encrypt(byte[] inputData)
|
||||
{
|
||||
// Create a new instance of the MD5CryptoServiceProvider object.
|
||||
MD5CryptoServiceProvider md5Hasher = new MD5CryptoServiceProvider();
|
||||
|
||||
// Convert the input string to a byte array and compute the hash.
|
||||
byte[] data = md5Hasher.ComputeHash(inputData);
|
||||
|
||||
// Create a new Stringbuilder to collect the bytes
|
||||
// and create a string.
|
||||
StringBuilder sBuilder = new StringBuilder();
|
||||
|
||||
// Loop through each byte of the hashed data
|
||||
// and format each one as a hexadecimal string.
|
||||
for (int i = 0; i < data.Length; i++)
|
||||
{
|
||||
sBuilder.Append(data[i].ToString("x2"));
|
||||
}
|
||||
|
||||
// Return the hexadecimal string.
|
||||
return sBuilder.ToString();
|
||||
}
|
||||
}
|
||||
}
|
210
sop-sdk/sdk-csharp/SDKCSharp/Utility/RSA.cs
Normal file
210
sop-sdk/sdk-csharp/SDKCSharp/Utility/RSA.cs
Normal file
@@ -0,0 +1,210 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
using System.Security.Cryptography;
|
||||
|
||||
using Org.BouncyCastle.Asn1.Pkcs;
|
||||
using Org.BouncyCastle.Asn1.X509;
|
||||
using Org.BouncyCastle.Crypto.Generators;
|
||||
using Org.BouncyCastle.Crypto.Parameters;
|
||||
using Org.BouncyCastle.Math;
|
||||
using Org.BouncyCastle.Pkcs;
|
||||
using Org.BouncyCastle.Security;
|
||||
using Org.BouncyCastle.Crypto.Engines;
|
||||
using Org.BouncyCastle.X509;
|
||||
using Org.BouncyCastle.Crypto;
|
||||
using Org.BouncyCastle.Asn1;
|
||||
using Org.BouncyCastle.Crypto.Encodings;
|
||||
|
||||
|
||||
namespace SDKCSharp.Utility
|
||||
{
|
||||
public class RSA
|
||||
{
|
||||
private static Encoding Encoding_UTF8 = Encoding.UTF8;
|
||||
|
||||
/// <summary>
|
||||
/// KEY 结构体
|
||||
/// </summary>
|
||||
public struct RSAKEY
|
||||
{
|
||||
/// <summary>
|
||||
/// 公钥
|
||||
/// </summary>
|
||||
public string PublicKey
|
||||
{
|
||||
get;
|
||||
set;
|
||||
}
|
||||
/// <summary>
|
||||
/// 私钥
|
||||
/// </summary>
|
||||
public string PrivateKey
|
||||
{
|
||||
get;
|
||||
set;
|
||||
}
|
||||
}
|
||||
public RSAKEY GetKey()
|
||||
{
|
||||
//RSA密钥对的构造器
|
||||
RsaKeyPairGenerator keyGenerator = new RsaKeyPairGenerator();
|
||||
|
||||
//RSA密钥构造器的参数
|
||||
RsaKeyGenerationParameters param = new RsaKeyGenerationParameters(
|
||||
Org.BouncyCastle.Math.BigInteger.ValueOf(3),
|
||||
new Org.BouncyCastle.Security.SecureRandom(),
|
||||
1024, //密钥长度
|
||||
25);
|
||||
//用参数初始化密钥构造器
|
||||
keyGenerator.Init(param);
|
||||
//产生密钥对
|
||||
AsymmetricCipherKeyPair keyPair = keyGenerator.GenerateKeyPair();
|
||||
//获取公钥和密钥
|
||||
AsymmetricKeyParameter publicKey = keyPair.Public;
|
||||
AsymmetricKeyParameter privateKey = keyPair.Private;
|
||||
|
||||
SubjectPublicKeyInfo subjectPublicKeyInfo = SubjectPublicKeyInfoFactory.CreateSubjectPublicKeyInfo(publicKey);
|
||||
PrivateKeyInfo privateKeyInfo = PrivateKeyInfoFactory.CreatePrivateKeyInfo(privateKey);
|
||||
|
||||
|
||||
Asn1Object asn1ObjectPublic = subjectPublicKeyInfo.ToAsn1Object();
|
||||
|
||||
byte[] publicInfoByte = asn1ObjectPublic.GetEncoded("UTF-8");
|
||||
Asn1Object asn1ObjectPrivate = privateKeyInfo.ToAsn1Object();
|
||||
byte[] privateInfoByte = asn1ObjectPrivate.GetEncoded("UTF-8");
|
||||
|
||||
RSAKEY item = new RSAKEY()
|
||||
{
|
||||
PublicKey = Convert.ToBase64String(publicInfoByte),
|
||||
PrivateKey = Convert.ToBase64String(privateInfoByte)
|
||||
};
|
||||
return item;
|
||||
}
|
||||
private AsymmetricKeyParameter GetPublicKeyParameter(string keyBase64)
|
||||
{
|
||||
keyBase64 = keyBase64.Replace("\r", "").Replace("\n", "").Replace(" ", "");
|
||||
byte[] publicInfoByte = Convert.FromBase64String(keyBase64);
|
||||
Asn1Object pubKeyObj = Asn1Object.FromByteArray(publicInfoByte);//这里也可以从流中读取,从本地导入
|
||||
AsymmetricKeyParameter pubKey = PublicKeyFactory.CreateKey(publicInfoByte);
|
||||
return pubKey;
|
||||
}
|
||||
|
||||
private AsymmetricKeyParameter GetPrivateKeyParameter(string keyBase64)
|
||||
{
|
||||
keyBase64 = keyBase64.Replace("\r", "").Replace("\n", "").Replace(" ", "");
|
||||
byte[] privateInfoByte = Convert.FromBase64String(keyBase64);
|
||||
// Asn1Object priKeyObj = Asn1Object.FromByteArray(privateInfoByte);//这里也可以从流中读取,从本地导入
|
||||
// PrivateKeyInfo privateKeyInfo = PrivateKeyInfoFactory.CreatePrivateKeyInfo(privateKey);
|
||||
AsymmetricKeyParameter priKey = PrivateKeyFactory.CreateKey(privateInfoByte);
|
||||
return priKey;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 私钥加密
|
||||
/// </summary>
|
||||
/// <param name="data">加密内容</param>
|
||||
/// <param name="privateKey">私钥(Base64后的)</param>
|
||||
/// <returns>返回Base64内容</returns>
|
||||
public string EncryptByPrivateKey(string data, string privateKey)
|
||||
{
|
||||
//非对称加密算法,加解密用
|
||||
IAsymmetricBlockCipher engine = new Pkcs1Encoding(new RsaEngine());
|
||||
|
||||
//加密
|
||||
|
||||
try
|
||||
{
|
||||
engine.Init(true, GetPrivateKeyParameter(privateKey));
|
||||
byte[] byteData = Encoding_UTF8.GetBytes(data);
|
||||
var ResultData = engine.ProcessBlock(byteData, 0, byteData.Length);
|
||||
return Convert.ToBase64String(ResultData);
|
||||
//Console.WriteLine("密文(base64编码):" + Convert.ToBase64String(testData) + Environment.NewLine);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
throw ex;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 私钥解密
|
||||
/// </summary>
|
||||
/// <param name="data">待解密的内容</param>
|
||||
/// <param name="privateKey">私钥(Base64编码后的)</param>
|
||||
/// <returns>返回明文</returns>
|
||||
public string DecryptByPrivateKey(string data, string privateKey)
|
||||
{
|
||||
data = data.Replace("\r", "").Replace("\n", "").Replace(" ", "");
|
||||
//非对称加密算法,加解密用
|
||||
IAsymmetricBlockCipher engine = new Pkcs1Encoding(new RsaEngine());
|
||||
|
||||
//解密
|
||||
try
|
||||
{
|
||||
engine.Init(false, GetPrivateKeyParameter(privateKey));
|
||||
byte[] byteData = Convert.FromBase64String(data);
|
||||
var ResultData = engine.ProcessBlock(byteData, 0, byteData.Length);
|
||||
return Encoding_UTF8.GetString(ResultData);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
throw ex;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 公钥加密
|
||||
/// </summary>
|
||||
/// <param name="data">加密内容</param>
|
||||
/// <param name="publicKey">公钥(Base64编码后的)</param>
|
||||
/// <returns>返回Base64内容</returns>
|
||||
public string EncryptByPublicKey(string data, string publicKey)
|
||||
{
|
||||
//非对称加密算法,加解密用
|
||||
IAsymmetricBlockCipher engine = new Pkcs1Encoding(new RsaEngine());
|
||||
|
||||
//加密
|
||||
try
|
||||
{
|
||||
engine.Init(true, GetPublicKeyParameter(publicKey));
|
||||
byte[] byteData = Encoding_UTF8.GetBytes(data);
|
||||
var ResultData = engine.ProcessBlock(byteData, 0, byteData.Length);
|
||||
return Convert.ToBase64String(ResultData);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
throw ex;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 公钥解密
|
||||
/// </summary>
|
||||
/// <param name="data">待解密的内容</param>
|
||||
/// <param name="publicKey">公钥(Base64编码后的)</param>
|
||||
/// <returns>返回明文</returns>
|
||||
public string DecryptByPublicKey(string data, string publicKey)
|
||||
{
|
||||
data = data.Replace("\r", "").Replace("\n", "").Replace(" ", "");
|
||||
//非对称加密算法,加解密用
|
||||
IAsymmetricBlockCipher engine = new Pkcs1Encoding(new RsaEngine());
|
||||
|
||||
//解密
|
||||
try
|
||||
{
|
||||
engine.Init(false, GetPublicKeyParameter(publicKey));
|
||||
byte[] byteData = Convert.FromBase64String(data);
|
||||
var ResultData = engine.ProcessBlock(byteData, 0, byteData.Length);
|
||||
return Encoding_UTF8.GetString(ResultData);
|
||||
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
throw ex;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
47
sop-sdk/sdk-csharp/SDKCSharp/Utility/RSAUtil.cs
Normal file
47
sop-sdk/sdk-csharp/SDKCSharp/Utility/RSAUtil.cs
Normal file
@@ -0,0 +1,47 @@
|
||||
using System;
|
||||
using System.Xml;
|
||||
using System.Collections.Generic;
|
||||
using System.IO;
|
||||
using System.Linq;
|
||||
using System.Security.Cryptography;
|
||||
using System.Text;
|
||||
|
||||
using Org.BouncyCastle.Asn1.Pkcs;
|
||||
using Org.BouncyCastle.Asn1.X509;
|
||||
using Org.BouncyCastle.Crypto.Generators;
|
||||
using Org.BouncyCastle.Crypto.Parameters;
|
||||
using Org.BouncyCastle.Math;
|
||||
using Org.BouncyCastle.Pkcs;
|
||||
using Org.BouncyCastle.Security;
|
||||
using Org.BouncyCastle.Crypto.Engines;
|
||||
using Org.BouncyCastle.X509;
|
||||
using Org.BouncyCastle.Crypto;
|
||||
using Org.BouncyCastle.Asn1;
|
||||
using Org.BouncyCastle.Crypto.Encodings;
|
||||
|
||||
namespace SDKCSharp.Utility
|
||||
{
|
||||
public class RSAUtil
|
||||
{
|
||||
|
||||
static Encoding UTF8 = Encoding.UTF8;
|
||||
static RSA rsa = new RSA();
|
||||
|
||||
/// <summary>
|
||||
/// 私钥加密
|
||||
/// </summary>
|
||||
/// <returns>The by private key.</returns>
|
||||
/// <param name="data">内容.</param>
|
||||
/// <param name="privateKey">私钥.</param>
|
||||
public static string EncryptByPrivateKey(string data, string privateKey)
|
||||
{
|
||||
return rsa.EncryptByPrivateKey(data, privateKey);
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
}
|
43
sop-sdk/sdk-csharp/SDKCSharp/Utility/SignUtil.cs
Normal file
43
sop-sdk/sdk-csharp/SDKCSharp/Utility/SignUtil.cs
Normal file
@@ -0,0 +1,43 @@
|
||||
using System;
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace SDKCSharp.Utility
|
||||
{
|
||||
/// <summary>
|
||||
/// 签名工具类
|
||||
/// </summary>
|
||||
public class SignUtil
|
||||
{
|
||||
|
||||
/// <summary>
|
||||
/// 参数签名
|
||||
/// </summary>
|
||||
/// <param name="paramsMap">参数</param>
|
||||
/// <param name="secret">秘钥</param>
|
||||
/// <returns>返回sign</returns>
|
||||
public static String CreateSign(Dictionary<string, object> paramsMap, string secret)
|
||||
{
|
||||
StringBuilder sb = new StringBuilder();
|
||||
ArrayList paramNames = new ArrayList(paramsMap.Keys);
|
||||
|
||||
paramNames.Sort();
|
||||
|
||||
sb.Append(secret);
|
||||
foreach (string paramName in paramNames)
|
||||
{
|
||||
sb.Append(paramName).Append(paramsMap[paramName]);
|
||||
}
|
||||
sb.Append(secret);
|
||||
|
||||
string source = sb.ToString();
|
||||
|
||||
return MD5Util.EncryptToUpper(source);
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user