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公众号:程序员雨果,即可免费领取

相关推荐
自由鬼1 小时前
免费开源抓包工具Wireshark介绍
运维·服务器·网络·测试工具·网络安全·wireshark
海姐软件测试2 小时前
Deepseek如何写测试用例
测试工具·面试
alenliu06213 小时前
跟着 Lua 5.1 官方参考文档学习 Lua (12)
lua
不脱发的猴子8 小时前
Wireshark使用教程
网络·测试工具·wireshark
Aimyone13 小时前
postman导出 二进制文件流处理成文件
postman
勘察加熊人13 小时前
fastapi房产销售系统
数据库·lua·fastapi
waicsdn_haha13 小时前
Postman v11 安装与API测试入门教程(Windows平台)
人工智能·windows·测试工具·mysql·postman·dbeaver·rest
ITlinuxP13 小时前
2025最新Postman、Apipost和Apifox API 协议与工具选择方案解析
后端·测试工具·postman·开发工具·apipost·apifox·api协议
天才测试猿13 小时前
功能测试详解
自动化测试·软件测试·python·功能测试·测试工具·职场和发展·测试用例
海姐软件测试14 小时前
面试时,如何回答好“你是怎么测试接口的?”
测试工具·面试·职场和发展·postman