JS generate interface Request parameter signature encryption

Source: Internet
Author: User
Tags md5 encryption

JS generate interface Request parameter signature encryption

Define the rule: All parameter fields are sorted by initials, stitched into Key1 = value1 & key2 = value2 format, then at the end of the splicing key = Appsecret, then do MD5 encryption to generate sign, the method is as follows:

function getsign(params,Kappkey,Kappsecret){    if(typeofParams== "string"){        return Paramsstrsort(params);    } Else if(typeofParams== "Object"){        varArr=[];         for(varIinchParams{            arr.Push((i+ "=" +Params[i]));        }        return Paramsstrsort(arr.Join(("&")));    }}function Paramsstrsort(PARAMSSTR){    varUrl=Paramsstr+ "&appkey=" +Kappkey;    varUrlstr= URL.Split("&").Sort().Join("&");    varNewurl=Urlstr+ ' &key= ' +Kappsecret;    return MD5(Newurl);}

Call:

var="xid="++"&type="++"&pageSize="++"&pageNo="+ pageNo;var=getSign(params, kAppKey, kAppSecret);

Or:

var={xid: xid,pageSize: pageSize,type: type,pageNo:};var=getSign(paramsObj, kAppKey, kAppSecret);

Kappkey,kappsecret is a constant, usually defined by the backend! Signature encryption method relies on md5!

GitHub Address

JS generate interface Request parameter signature encryption

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.