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

前言

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

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

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

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

获取pmlib
引入依赖bundle.js,有以下两种方式:
  1. 从github下载postman collection ,并导入进你的集合里 。

  2. 将所需js所需js所需js全部复制保存成一个全局变量如:pmlib_code

  3. 把自己的私钥设置成环境变量如:pri_key

使用Pre-request Script对请求进行加签(具体加签字段请看自己项目)

  1. // 使用eval执行js

  2. eval(pm.globals.get('pmlib_code'))

  3. // 生成rfctime

  4. let date = new Date()

  5. let y = date.getFullYear()

  6. let m = date.getMonth()+1<10?'0'+(date.getMonth()+1):(date.getMonth()+1)

  7. let d = date.getDate()<10?'0'+date.getDate():date.getDate()

  8. let hh = date.getHours()<10?'0'+date.getHours():date.getHours();

  9. let mm = date.getMinutes()<10?'0'+date.getMinutes():date.getMinutes()

  10. let ss = date.getSeconds()<10?'0'+date.getSeconds():date.getSeconds()

  11. this.rfc_time = y +'-' + m + '-' + d + ' ' + hh + ':' + mm + ':' + ss

  12. this.rfc_time = this.rfc_time.replace(/\s+/g, 'T')+'+08:00'

  13. pm.variables.set('rfctime',this.rfc_time)

  14. // console.log(pm.variables.get('rfctime'))

  15. const privkey = pm.environment.get('pri_key').replace(/\\n/g, "\n")

  16. // 随机字符串

  17. const uuid = pm.variables.replaceIn('{``{$randomUUID}}')

  18. pm.variables.set('nonce_str', uuid)

  19. const requestBodyRaw = pm.variables.replaceIn(pm.request.body == undefined ? '' : pm.request.body.raw)

  20. const now = pm.variables.replaceIn('{``{$timestamp}}')

  21. pm.variables.set('req_time', now)

  22. // 具体加密字段拼接请依据项目情况案例是:method+\n+url+\n+timestamp+\n+nonce_str+\n+body

  23. var dataToSign = pm.request.method + "\n" +

  24. pm.request.url.getPathWithQuery() + "\n" +

  25. now + "\n" +

  26. uuid + "\n" +

  27. requestBodyRaw

  28. console.log(dataToSign)

  29. const sha256withrsa = new pmlib.rs.KJUR.crypto.Signature({"alg": "SHA256withRSA"});

  30. sha256withrsa.init(privkey);

  31. sha256withrsa.updateString(dataToSign);

  32. const sign = pmlib.rs.hextob64(sha256withrsa.sign());

  33. // console.log(sign);

  34. pm.variables.set('sign', sign)

  35. // 添加请求头

  36. pm.request.headers.add({

  37. key:"Authorization",

  38. value:"SHA256-RSA nonce_str={``{nonce_str}},timestamp={``{req_time}},signature={``{sign}}"

  39. });

使用Pre-request Script对请求进行加签(具体加签字段请看自己项目)

  1. // 使用eval执行js

  2. eval(pm.globals.get('pmlib_code'))

  3. // 生成rfctime

  4. let date = new Date()

  5. let y = date.getFullYear()

  6. let m = date.getMonth()+1<10?'0'+(date.getMonth()+1):(date.getMonth()+1)

  7. let d = date.getDate()<10?'0'+date.getDate():date.getDate()

  8. let hh = date.getHours()<10?'0'+date.getHours():date.getHours();

  9. let mm = date.getMinutes()<10?'0'+date.getMinutes():date.getMinutes()

  10. let ss = date.getSeconds()<10?'0'+date.getSeconds():date.getSeconds()

  11. this.rfc_time = y +'-' + m + '-' + d + ' ' + hh + ':' + mm + ':' + ss

  12. this.rfc_time = this.rfc_time.replace(/\s+/g, 'T')+'+08:00'

  13. pm.variables.set('rfctime',this.rfc_time)

  14. // console.log(pm.variables.get('rfctime'))

  15. const privkey = pm.environment.get('pri_key').replace(/\\n/g, "\n")

  16. // 随机字符串

  17. const uuid = pm.variables.replaceIn('{``{$randomUUID}}')

  18. pm.variables.set('nonce_str', uuid)

  19. const requestBodyRaw = pm.variables.replaceIn(pm.request.body == undefined ? '' : pm.request.body.raw)

  20. const now = pm.variables.replaceIn('{``{$timestamp}}')

  21. pm.variables.set('req_time', now)

  22. // 具体加密字段拼接请依据项目情况案例是:method+\n+url+\n+timestamp+\n+nonce_str+\n+body

  23. var dataToSign = pm.request.method + "\n" +

  24. pm.request.url.getPathWithQuery() + "\n" +

  25. now + "\n" +

  26. uuid + "\n" +

  27. requestBodyRaw

  28. console.log(dataToSign)

  29. const sha256withrsa = new pmlib.rs.KJUR.crypto.Signature({"alg": "SHA256withRSA"});

  30. sha256withrsa.init(privkey);

  31. sha256withrsa.updateString(dataToSign);

  32. const sign = pmlib.rs.hextob64(sha256withrsa.sign());

  33. // console.log(sign);

  34. pm.variables.set('sign', sign)

  35. // 添加请求头

  36. pm.request.headers.add({

  37. key:"Authorization",

  38. value:"SHA256-RSA nonce_str={``{nonce_str}},timestamp={``{req_time}},signature={``{sign}}"

  39. });

至此SHA256withRSA签名已完成

总结:

感谢每一个认真阅读我文章的人!!!

作为一位过来人也是希望大家少走一些弯路,如果你不想再体验一次学习时找不到资料,没人解答问题,坚持几天便放弃的感受的话,在这里我给大家分享一些自动化测试的学习资源,希望能给你前进的路上带来帮助。

  1. 文档获取方式:

  2. 加入我的软件测试交流群:680748947免费获取~(同行大佬一起学术交流,每晚都有大佬直播分享技术知识点)

这份文档,对于想从事【软件测试】的朋友来说应该是最全面最完整的备战仓库,这个仓库也陪伴我走过了最艰难的路程,希望也能帮助到你!

以上均可以分享,只需要你搜索vx公众号:程序员雨果,即可免费领取

相关推荐
和粒科技2 天前
热重分析仪TG 309 Libra Classic
测试工具
努力努力再努力wz2 天前
【Lua进阶系列】Lua 调试机制详解:从虚拟机调用栈、错误传播到 debug 库
junit·单元测试·lua
光谱SpectrAI4 天前
打开 Postman 会改 Codex?Windows 上 4 处都没有
postman
炸薯条!4 天前
博客系统测试报告
开发语言·lua
yz_aiks4 天前
Postman 自动给 Claude Code、Codex、Cursor 装插件怎么查和卸载:本机实测 5 处痕迹(Postman 12.34)
安全·postman·ai编程·开发工具·claude code
BuHuaX6 天前
Lua 与 ToLua:运行机制及热更新原理
开发语言·unity·junit·c#·lua
努力努力再努力wz6 天前
【Lua 入门系列】模块与包(一):从多文件组织到require 运行时加载机制、模块缓存、预加载搜索与 Lua 文件搜索机制
开发语言·junit·lua
UWA6 天前
多人战斗中Lua持续高耗时,是瞬时峰值还是高频调用在作祟
性能优化·lua·游戏开发
努力努力再努力wz7 天前
【Lua 入门系列】从执行模型到事件驱动:一文理解 Lua 有栈协程、挂起恢复与协作式调度
开发语言·lua
努力努力再努力wz7 天前
【Lua 入门系列】Lua数据结构:从 Table 出发,一文串联数组、链表、栈、队列、集合、映射与树的实现原理
数据结构·链表·lua