深圳集团网站建设公司好,图片类网站怎样做高并发,足球个人网站模板,电商网站运维怎么做前言
在接口测试中#xff0c;我们经常需要对请求进行签名#xff0c;以保证数据的安全性。而SHA256withRSA是一种较为常见的签名算法#xff0c;它可以使用私钥对数据进行签名#xff0c;使用公钥进行验签。
但是#xff0c;实现该算法签名可能会涉及到一些繁琐的操作我们经常需要对请求进行签名以保证数据的安全性。而SHA256withRSA是一种较为常见的签名算法它可以使用私钥对数据进行签名使用公钥进行验签。
但是实现该算法签名可能会涉及到一些繁琐的操作给我们的工作带来不小的困扰。
今天我要向大家介绍一个神器——Postman它可以轻松完成SHA256withRSA签名的实现让您的API请求得到更加完善的保护。
接下来我将简单介绍如何使用Postman实现SHA256withRSA签名并且分享一些注意事项和技巧希望能让大家轻松掌握这个技能。
获取pmlib
引入依赖bundle.js有以下两种方式
从github下载postman collection 并导入进你的集合里。 将所需js所需js所需js全部复制保存成一个全局变量如pmlib_code把自己的私钥设置成环境变量如pri_key
使用Pre-request Script对请求进行加签(具体加签字段请看自己项目)
// 使用eval执行js
eval(pm.globals.get(pmlib_code))// 生成rfctime
let date new Date()
let y date.getFullYear()
let m date.getMonth()110?0(date.getMonth()1):(date.getMonth()1)
let d date.getDate()10?0date.getDate():date.getDate()
let hh date.getHours()10?0date.getHours():date.getHours();
let mm date.getMinutes()10?0date.getMinutes():date.getMinutes()
let ss date.getSeconds()10?0date.getSeconds():date.getSeconds()
this.rfc_time y - m - d hh : mm : ss
this.rfc_time this.rfc_time.replace(/\s/g, T)08:00
pm.variables.set(rfctime,this.rfc_time)
// console.log(pm.variables.get(rfctime))const privkey pm.environment.get(pri_key).replace(/\\n/g, \n)// 随机字符串
const uuid pm.variables.replaceIn({{$randomUUID}})
pm.variables.set(nonce_str, uuid)const requestBodyRaw pm.variables.replaceIn(pm.request.body undefined ? : pm.request.body.raw)const now pm.variables.replaceIn({{$timestamp}})
pm.variables.set(req_time, now)
// 具体加密字段拼接请依据项目情况案例是method\nurl\ntimestamp\nnonce_str\nbody
var dataToSign pm.request.method \n pm.request.url.getPathWithQuery() \n now \n uuid \n requestBodyRawconsole.log(dataToSign)const sha256withrsa new pmlib.rs.KJUR.crypto.Signature({alg: SHA256withRSA});
sha256withrsa.init(privkey);sha256withrsa.updateString(dataToSign);const sign pmlib.rs.hextob64(sha256withrsa.sign());// console.log(sign);
pm.variables.set(sign, sign)
// 添加请求头
pm.request.headers.add({key:Authorization,value:SHA256-RSA nonce_str{{nonce_str}},timestamp{{req_time}},signature{{sign}}
}); 使用Pre-request Script对请求进行加签(具体加签字段请看自己项目)
// 使用eval执行js
eval(pm.globals.get(pmlib_code))// 生成rfctime
let date new Date()
let y date.getFullYear()
let m date.getMonth()110?0(date.getMonth()1):(date.getMonth()1)
let d date.getDate()10?0date.getDate():date.getDate()
let hh date.getHours()10?0date.getHours():date.getHours();
let mm date.getMinutes()10?0date.getMinutes():date.getMinutes()
let ss date.getSeconds()10?0date.getSeconds():date.getSeconds()
this.rfc_time y - m - d hh : mm : ss
this.rfc_time this.rfc_time.replace(/\s/g, T)08:00
pm.variables.set(rfctime,this.rfc_time)
// console.log(pm.variables.get(rfctime))const privkey pm.environment.get(pri_key).replace(/\\n/g, \n)// 随机字符串
const uuid pm.variables.replaceIn({{$randomUUID}})
pm.variables.set(nonce_str, uuid)const requestBodyRaw pm.variables.replaceIn(pm.request.body undefined ? : pm.request.body.raw)const now pm.variables.replaceIn({{$timestamp}})
pm.variables.set(req_time, now)
// 具体加密字段拼接请依据项目情况案例是method\nurl\ntimestamp\nnonce_str\nbody
var dataToSign pm.request.method \n pm.request.url.getPathWithQuery() \n now \n uuid \n requestBodyRawconsole.log(dataToSign)const sha256withrsa new pmlib.rs.KJUR.crypto.Signature({alg: SHA256withRSA});
sha256withrsa.init(privkey);sha256withrsa.updateString(dataToSign);const sign pmlib.rs.hextob64(sha256withrsa.sign());// console.log(sign);
pm.variables.set(sign, sign)
// 添加请求头
pm.request.headers.add({key:Authorization,value:SHA256-RSA nonce_str{{nonce_str}},timestamp{{req_time}},signature{{sign}}
});
至此SHA256withRSA签名已完成 感谢每一个认真阅读我文章的人礼尚往来总是要有的虽然不是什么很值钱的东西如果你用得到的话可以直接拿走 这些资料对于【软件测试】的朋友来说应该是最全面最完整的备战仓库这个仓库也陪伴上万个测试工程师们走过最艰难的路程希望也能帮助到你!有需要的小伙伴可以点击下方小卡片领取