Postman轻松签名,让SHA256withRSA保驾护航!

前言

在接口测试中,我们经常需要对请求进行签名,以保证数据的安全性。而SHA256withRSA是一种较为常见的签名算法,它可以使用私钥对数据进行签名,使用公钥进行验签。

但是,实现该算法签名可能会涉及到一些繁琐的操作,给我们的工作带来不小的困扰。

今天,我要向大家介绍一个神器——Postman,它可以轻松完成SHA256withRSA签名的实现,让您的API请求得到更加完善的保护。

接下来,我将简单介绍如何使用Postman实现SHA256withRSA签名,并且分享一些注意事项和技巧,希望能让大家轻松掌握这个技能。

获取pmlib

引入依赖bundle.js,有以下两种方式:
  1. 从github下载postman collection ,并导入进你的集合里
    官方使用介绍

 

  1. 所需js所需js所需js全部复制保存成一个全局变量如:pmlib_code
    在这里插入图片描述
  2. 把自己的私钥设置成环境变量如: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()+1<10?'0'+(date.getMonth()+1):(date.getMonth()+1)
let d = date.getDate()<10?'0'+date.getDate():date.getDate()
let hh = date.getHours()<10?'0'+date.getHours():date.getHours();            
let mm = date.getMinutes()<10?'0'+date.getMinutes():date.getMinutes()
let ss = date.getSeconds()<10?'0'+date.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+\n+url+\n+timestamp+\n+nonce_str+\n+body
var dataToSign = pm.request.method + "\n" +
    pm.request.url.getPathWithQuery() + "\n" +
    now + "\n" +
    uuid + "\n" +
    requestBodyRaw
 
console.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()+1<10?'0'+(date.getMonth()+1):(date.getMonth()+1)
let d = date.getDate()<10?'0'+date.getDate():date.getDate()
let hh = date.getHours()<10?'0'+date.getHours():date.getHours();            
let mm = date.getMinutes()<10?'0'+date.getMinutes():date.getMinutes()
let ss = date.getSeconds()<10?'0'+date.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+\n+url+\n+timestamp+\n+nonce_str+\n+body
var dataToSign = pm.request.method + "\n" +
    pm.request.url.getPathWithQuery() + "\n" +
    now + "\n" +
    uuid + "\n" +
    requestBodyRaw
 
console.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签名已完成
在这里插入图片描述

 感谢每一个认真阅读我文章的人,礼尚往来总是要有的,虽然不是什么很值钱的东西,如果你用得到的话可以直接拿走:

 

这些资料,对于【软件测试】的朋友来说应该是最全面最完整的备战仓库,这个仓库也陪伴上万个测试工程师们走过最艰难的路程,希望也能帮助到你!有需要的小伙伴可以点击下方小卡片领取 

 

  • 7
    点赞
  • 11
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值