微信卡券JSAPI簽名校驗演算法
阿新 • • 發佈:2019-02-08
微信卡卷的簽名規定:
1.將 api_ticket、timestamp、card_id、code、openid、nonce_str的value值進行字串的字典序排序。
2.將所有引數字串拼接成一個字串進行sha1加密,得到signature。
使用C#後臺進行字串的字典序排序,然後SHA1加密於微信卡券JSAPI簽名校驗工具進行比較:
結果一樣:
其中進行字串的字典序排序,和SHA1簽名如下:
string api_ticket = this.txt_api_ticket.Text; string timestamp = this.txt_timestamp.Text; string nonce_str = this.txt_nonce_str.Text; string card_id = this.txt_card_id.Text; List<string> ss = new List<string>() { api_ticket, timestamp, nonce_str, card_id }; var list=ss.OrderBy(x=>x,StringComparer.Ordinal).ToArray(); var orderstring=string.Join("",list); string _signature = SHA1Helper.HmacSha1(orderstring); this.txt_qm.Text = _signature; this.txt_px.Text = orderstring;
public class SHA1Helper { /// <summary> /// 生成hmacsha1的雜湊 /// </summary> /// <param name="word"></param> /// <returns></returns> public static string HmacSha1(string word) { return BitConverter.ToString(SHA1.Create().ComputeHash(Encoding.UTF8.GetBytes(word))).Replace("-", string.Empty); } }