Signature: Implement parameter dictionary sort, then stitch as url Parameter form

Source: Internet
Author: User
Tags md5 encryption

In many places request parameters need to be handled such as:

Steps

1. Parameter dictionary ordering.

2. Stitching characters.

        /// <summary>        ///Generate Signature/// </summary>        /// <param name= "Paramlst" >parameter list</param>        /// <param name= "Istoupper" >whether to turn uppercase</param>        /// <param name= "Isdirect" >whether to add the signature key directly</param>        /// <returns></returns>         Public Static stringGeneratesign (list<keyvaluepair<string,string>> LST,stringPrivatekey) {Comparison<KeyValuePair<string,string>> comparer =Newcomparison<keyvaluepair<string,string>>(Comparekeyvaluepair); Lst.            Sort (comparer); varParamlst = lst. Where (p =!)string. IsNullOrEmpty (P.value)).            ToList (); stringSignstr =string. Join ("&", Paramlst. Select (p = p.key +"="+ p.value)) +Privatekey; returnSign (SIGNSTR,"UTF-8"); }
        /// <summary>        /// Compare sort positions        by key /// </summary>        Private Static int Comparekeyvaluepair (keyvaluepair<stringstring> P1, keyvaluepair<string string> p2)        {            returnstring. Compare (P1. Key, p2. Key);        }        

3.MD5 encryption

/// <summary>        ///Signature String/// </summary>        /// <param name= "Prestr" >a string that needs to be signed</param>        /// <param name= "key" >secret key</param>        /// <param name= "Inputcharset" >encoding Format</param>        /// <returns>Signature Results</returns>         Public Static stringSign (stringPRESTR,stringInputcharset) {StringBuilder SB=NewStringBuilder (); MD5 MD5=NewMD5CryptoServiceProvider (); byte[] t =Md5.computehash (encoding.getencoding (Inputcharset).            GetBytes (PRESTR)); foreach(byteT1incht) {sb. Append (t1. ToString ("X2")); }            returnsb.        ToString (); }

Signature: Implements the parameter dictionary sort, then stitching as url Parameter form

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.