最近接了一个项目,需要调用对方接口生成token,但是只提供了node版本。源码如下//https://blog.zhengxianjun.com/2015/05/javascript-crypto-js/npminstallcrypto-jsvarCryptoJS=require('crypto-js')calSignature=function(toSign,key){varhash,hex,signed;hash=CryptoJS.HmacSHA1(toSign,密钥);hex=hash.toString(CryptoJS.enc.Hex);signed=base64.encodeBase64(hex);回签;};varhmacSHA1=CryptoJS.HmacSHA1("Message","SecretPassphrase").toString(CryptoJS.enc.Hex);console.log(hmacSHA1);//e90f713295ea4cc06c92c9248606ffafc5d也可以在线测试https://blog.zhengxianjun.com/online-tool/hash/varstr='123456';CryptoJS.MD5(str);varstr='123456';varpassword='password';//HmacCryptoJS.HmacMD5(str,password);CryptoJS.HmacRIPEMD160(str,password);CryptoJS.HmacSHA1(str,password)areaddedbeforeHmacrelatedcalls;ButourprojectisbasedonPHP,sowehave$sign=base64_encode(hash_hmac('sha1',$sign,$key));Sureenough,PHPisthebestlanguage,anditcanbedoneinonesentence.Inaddition,whenitcomestorequestingtheotherparty'sinterface,theotherpartyrequirestopassajsonparameter,suchas$url='xxxxx.com';$data=['limit'=>['start'=>0,'end'=>5]];$res=curlGet($url.'/api/?'.json_encode($data);TheresultpromptsfailMalformedHTTPrequestlinebutpythongetisfineres=requests.get(url,params=data,headers=headers)于是对参数再进行一次base64$res=curlGet($url.'/api/?'.base64_encode(json_encode($data));但对方有时候接受的参数base64解析出错>>>s='eyJsaW1pdCI6eyJzdGFydCI6MCwiZW5kIjo1fSwidGFibGVfbmFtZSI6Im9ubGluZV91dl9hY2NvdW50X2lkXzFob3VyX2FuYWx5c2lzX2RhdGEiLCJmaWx0ZXJfYXJncyI6eyJ2ZmlkIjoiMTQiLCJ0aW1lc3RhbXBfMWhvdXIiOiIyMDE4LTAxLTA1IDIwOjA0OjI2In0sImJpel9kZXMwMSI6MCwib3JkZXJfYnkiOiJ0aW1lc3RhbXBfMWhvdXIifQ'>>>base64.b64decode(s)Traceback(mostrecentcalllast):文件“
