開發(fā)中經常需要做一些接口的簽名生成和校驗工作,最開始的時候都是每個接口去按照約定單獨實現(xiàn),久而久之就變的非常難維護,因此就琢磨怎么能夠寫了一個比較通用的簽名生成工具。
專注于為中小企業(yè)提供成都做網站、成都網站設計服務,電腦端+手機端+微信端的三站合一,更高效的管理,為中小企業(yè)鳳慶免費做網站提供優(yōu)質的服務。我們立足成都,凝聚了一批互聯(lián)網行業(yè)人才,有力地推動了千余家企業(yè)的穩(wěn)健成長,幫助中小企業(yè)通過網站建設實現(xiàn)規(guī)模擴充和轉變。2、思路采用鏈式調用的方式,使得簽名的步驟可以動態(tài)拼湊組合。
3、直接看效果//設置數(shù)據源
var signSource = new Dictionary()
{{"param1", "1" },
{"param3", "3+" },
{"param2", "2" }
};
var signer = new HttpSigner();
signer.SetSignData(signSource);
//設置數(shù)據源并配置規(guī)則
signer.SetSignData(signSource, setting =>{//按參數(shù)名排序
//result -->param1 param2 param3
setting.IsOrderByWithKey = false;
//是否對簽名數(shù)據的參數(shù)值進行UrlEncode
setting.IsDoUrlEncodeForSourceValue = false;
//簽名主體是否包含參數(shù)名
setting.IsSignTextContainKey = true;
//簽名主體中參數(shù)和參數(shù)值的連接符(需要啟用IsSignTextContainKey)
setting.SignTextKeyValueSeparator = "=";
//簽名主體中不同參數(shù)項的連接符
setting.SignTextItemSeparator = "&";
//以上都開啟后 -->param1=1¶m2=2¶m3=3
//編碼
setting.DefaultEncoding = Encoding.UTF8;
});
//簽名主體設置前綴
signer.SetSignData(signSource).SetSignTextPrefix("TestPrefix");
//簽名主體設置后綴
signer.SetSignData(signSource).SetSignTextSuffix("TestSuffix");
//簽名主體進行Base64
signer.SetSignData(signSource).SetSignTextBase64();
//簽名主體進行MD5,(方法參數(shù)為簽名結果是否轉小寫)
signer.SetSignData(signSource).SetSignTextMD5(bool isToLower = true);
//簽名主體進行SHA1,(方法參數(shù)為簽名結果是否轉小寫)
signer.SetSignData(signSource).SetSignTextSHA1(bool isToLower = true);
//獲取簽名結果
string signString = signer.SetSignData(signSource).GetSignResult();
//組合調用
string signString = signer.SetSignData(signSource).SetSignTextBase64().SetSignTextMD5().SetSignTextSHA1();
4、代碼實現(xiàn)HttpSignItem類
用于保存簽名的參數(shù)集合。
namespace JiuLing.CommonLibs.Security.HttpSign
{internal class HttpSignItem
{public string Key {get; set; }
public string Value {get; set; }
public HttpSignItem(string key, string value)
{Key = key;
Value = value;
}
}
}
HttpSignSetting類
用于簽名的基本配置。
using System.Text;
namespace JiuLing.CommonLibs.Security.HttpSign
{////// 簽名配置
/// public class HttpSignSetting
{////// 是否按參數(shù)名進行排序
/// public bool IsOrderByWithKey {get; set; } = false;
////// 是否對簽名數(shù)據的參數(shù)值進行UrlEncode
/// public bool IsDoUrlEncodeForSourceValue {get; set; } = false;
////// 簽名主體是否包含參數(shù)名
/// public bool IsSignTextContainKey {get; set; } = true;
////// 簽名主體中參數(shù)和參數(shù)值的連接符(需要啟用IsSignTextContainKey)
/// public string SignTextKeyValueSeparator {get; set; } = "=";
////// 簽名主體中不同參數(shù)項的連接符
/// public string SignTextItemSeparator {get; set; } = "&";
////// 編碼
/// public Encoding DefaultEncoding {get; set; } = Encoding.UTF8;
}
}
HttpSigner類
簽名組件的具體實現(xiàn)。
using System;
using System.Collections.Generic;
using System.Linq;
namespace JiuLing.CommonLibs.Security.HttpSign
{////// 網絡請求簽名工具
/// public class HttpSigner
{////// 簽名配置
/// private readonly HttpSignSetting _setting = new HttpSignSetting();
////// 最終的簽名串
/// private string _signString;
////// 設置簽名數(shù)據
/// ///待簽名的鍵值對///配置簽名規(guī)則/// /// public HttpSigner SetSignData(DictionarysignSource, Actionsetting = null)
{setting?.Invoke(_setting);
if (_setting == null)
{throw new ArgumentNullException("無效的簽名配置", "setting");
}
if (signSource == null || signSource.Count == 0)
{throw new ArgumentException("待簽名數(shù)據異常", nameof(signSource));
}
var signSourceList = new List(signSource.Count);
foreach (var item in signSource)
{var itemValue = item.Value;
if (_setting.IsDoUrlEncodeForSourceValue)
{itemValue = System.Web.HttpUtility.UrlEncode(itemValue, _setting.DefaultEncoding);
}
signSourceList.Add(new HttpSignItem(item.Key, itemValue));
}
if (_setting.IsOrderByWithKey)
{signSourceList = signSourceList.OrderBy(x =>x.Key).ToList();
}
if (_setting.IsSignTextContainKey)
{_signString = string.Join(_setting.SignTextItemSeparator, signSourceList.Select(x =>$"{x.Key}{_setting.SignTextKeyValueSeparator}{x.Value}"));
}
else
{_signString = string.Join(_setting.SignTextItemSeparator, signSourceList.Select(x =>x.Value));
}
return this;
}
////// 簽名主體設置前綴
/// ///前綴值/// public HttpSigner SetSignTextPrefix(string input)
{_signString = $"{input}{_signString}";
return this;
}
////// 簽名主體設置后綴
/// ///后綴值/// public HttpSigner SetSignTextSuffix(string input)
{_signString = $"{_signString}{input}";
return this;
}
////// 簽名主體設置后綴
/// /// public HttpSigner SetUrlEncode()
{_signString = System.Web.HttpUtility.UrlEncode(_signString, _setting.DefaultEncoding);
return this;
}
////// 簽名主體進行Base64
/// /// public HttpSigner SetSignTextBase64()
{_signString = Base64Utils.GetStringValue(_signString);
return this;
}
////// 簽名主體進行MD5
/// ///簽名結果是否轉小寫/// public HttpSigner SetSignTextMD5(bool isToLower = true)
{if (isToLower)
{_signString = MD5Utils.GetStringValueToLower(_signString);
}
else
{_signString = MD5Utils.GetStringValueToUpper(_signString);
}
return this;
}
////// 簽名主體進行SHA1
/// ///簽名結果是否轉小寫/// public HttpSigner SetSignTextSHA1(bool isToLower = true)
{if (isToLower)
{_signString = SHA1Utils.GetStringValueToLower(_signString);
}
else
{_signString = SHA1Utils.GetStringValueToUpper(_signString);
}
return this;
}
////// 獲取簽名結果
/// /// public string GetSignResult()
{return _signString;
}
}
}
5、附上倉庫地址以上代碼包含在我的通用類庫中,GitHub地址,也可以直接Nuget搜索JiuLing.CommonLibs安裝。
文章代碼片段地址
你是否還在尋找穩(wěn)定的海外服務器提供商?創(chuàng)新互聯(lián)www.cdcxhl.cn海外機房具備T級流量清洗系統(tǒng)配攻擊溯源,準確流量調度確保服務器高可用性,企業(yè)級服務器適合批量采購,新人活動首月15元起,快前往官網查看詳情吧