dqst96444 2019-09-06 08:46
浏览 120

python中的JSON编码dict和golang中的映射不匹配

I am generating a HMAC, sha256 hash of a json encoded python dict using json. Lets call it hash1. This is my signature which am sending with JWT. Then I would like to verify this signature at another service in Go. I am using the data that i have in a map(same as python dict), json encoding and hashing it(hash2) However, hash1 and hash2 are different. I learned that this is due to python json adding space between elements in dict. Golang json library doesn't add any space. Is there a way I can work around this?

some_data = {'a':1, 'b':2}
json_str1 = json.dumps(some_data, sort_keys=True)
some_data := map[string]int{"a":1, "b":2}
json_str2 = json.Marshal(some_data)

EDIT: As suggested in one of the answers, using separators in json.dumps would solve the problem. Unfortunately, I do not own the python side code, so can't do the changes there.

  • 写回答

2条回答 默认 最新

  • du5910 2019-09-06 08:50
    关注

    Can't say anything about Go, but when i was generating hash in javascript i had the same issue. You need to play around a bit with separators, maybe something like json.dumps(data, separators=(',', ':')).encode('utf-8') will work.

    评论

报告相同问题?

悬赏问题

  • ¥60 求一个简单的网页(标签-安全|关键词-上传)
  • ¥35 lstm时间序列共享单车预测,loss值优化,参数优化算法
  • ¥15 基于卷积神经网络的声纹识别
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图