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