请教:golang的rsa签名结果为何与openssl命令的结果不一样?

alexdel · · 300 次点击 · 开始浏览    置顶
最近在对接微信商户相关接口,遇到rsa签名(详情链接:https://wechatpay-api.gitbook.io/wechatpay-api-v3/qian-ming-zhi-nan-1/qian-ming-sheng-cheng )的困惑,希望有经验的大神能指点迷津 我使用的openssl(版本1.0.2u)命令: `echo -n -e "test\n" | openssl dgst -sha256 -sign ./apiclient_key.pem | openssl base64 -A` 得到的结果: ``` iYbARZQx0oLnehCfRNIc/fV0pVeAYFsenRWYMCayxzA95V/t91N5OXLXlzIs4T6rIOrpr3LSKptt3ga08eMORFWy8iPKdWFvt1zlufstjCSYM4oOK05SFwmFWIAV1Vj7FbdIQ5VDZmxPn8sT+5giktcV0i95ibErcEvWz9vbNDInoj0AsBPpztqzfCdVPKPOwlHBQ5FOPoO3loPwRGkBnZg24RO6P+ht+8C+u4C8iOnPqcV7SW4Z+Np/+fZUzDaKYlW3c52iMpd42W3KagRWO1nJbpKHzqwRerrXPSXzfNW5Qx36/GFCIDNnp//KQXGgh9ABSoUt+orcFCV6DrVJhQ==``` 我的签名相关golang代码: ``` // 获取私钥 bPrivateKey, err := ioutil.ReadFile("./apiclient_key.pem") if err != nil || bPrivateKey == nil { log.Println("读取秘钥文件失败:", err) return } block, _ := pem.Decode(bPrivateKey) if block == nil { log.Println("解码秘钥失败") return } pKeyInterface, err := x509.ParsePKCS8PrivateKey(block.Bytes) if err != nil { log.Println(err) return } pKey, ok := pKeyInterface.(*rsa.PrivateKey) if !ok { log.Println("获取私钥失败") return } // 签名 strForSign = "test\n" shaForSign := sha256.New() shaForSign.Write([]byte(strForSign)) hashedForSign := shaForSign.Sum(nil) rawSignature, err := rsa.SignPKCS1v15(rand.Reader, pKey, crypto.SHA256, hashedForSign) if err != nil { log.Println(err) return } signature := base64.StdEncoding.EncodeToString(rawSignature) log.Println(signature) ``` 得到的结果: ``` NcW5pBmUfHqVNus1PTDjGOilazWkcyxquGc/Ldu5IAjg/gAIQOKBGp7r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DtnEnEd8+QAuLfSsV6 wGBXV5cfnmmlkWKFxhDzW0solk9eBDKfIVw7Zw1NdanBlIrK187sOETArzeVEVFK J4r4CnPuguTMoik5AXNAOxnA5OrYP4Tn8xk3AV9Kuh16f+CAt6zzVQQo2FXzoteK NOlLM+oLe+XrY8kQFJkv4GXDmQKBgQDNHUVPmP2FeHbpU79mwun143HGDSPDslek nOEv+/1Cm9+L1sla1Yglb6OZrk9wxjnw0c4HnQV5+l1K6c72ujI840BobjUqMqCW mvb14lr/p1pVyAsSbMBlGR6EgR/QkMD/CL7KTKa/RwiM393oslvxvdGxCPuKEISN rCt/zxLBqQKBgEHXPNmkGW/eXN9i6LXK+Oc67jVkwAGQ96v55liIw5rLi9KRdzWB GP2c4KLGJ/PsJUlv43axYNvDsbMsyUmzC18QBm/g9WV+yc1yCW2S8t/wjuK9NybO xf3n82/RgrmfFaaVPudud2iaCbcpOHCjmMGt8PydqGhBjkVWb9xEYe6BAoGBAIjx /hDC6KFwaJQoIiaOBH7zOEcvBq3mKLXzODY9XD5Yq8xFv3IyFxBHlUdKJif5cT3k Flbm830ZN6iIqnH4pwoYACy5SC3AV4+2LyTD2FXQenPDeAD5MwtM1h9JxuANhY3B XQDcZIhY+NB1t0dhrhmczqAQi0EG/jlItlJDAxbhAoGAV5l05wHqqwEZroN0gXq2 86JTftIRzmstgs6O/yfjuInqHjMGp1fTOLyqbwA6+ZrHmzNyyoD8jiGW068LC1ue 7xucV/dYuUXUXOcX0HN/trIdMkl5jQWkrXSKP5o5ekw9gsMcicWFUBXdnJMxCcYb i8CRH78crAdaIqZ+2buIozo= -----END PRIVATE KEY----- ```

有疑问加站长微信联系

入群交流(和以上内容无关):加入Go大咖交流群,或添加微信:liuxiaoyan-s 备注:入群;或加QQ群:812540095

300 次点击  
加入收藏 微博
1 回复  |  直到 2020-09-24 16:32:44
暂无回复
添加一条新回复 (您需要 登录 后才能回复 没有账号 ?)
  • 请尽量让自己的回复能够对别人有帮助
  • 支持 Markdown 格式, **粗体**、~~删除线~~、`单行代码`
  • 支持 @ 本站用户;支持表情(输入 : 提示),见 Emoji cheat sheet
  • 图片支持拖拽、截图粘贴等方式上传