http.go 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. package asign
  2. import (
  3. "crypto"
  4. "crypto/md5"
  5. "crypto/rand"
  6. "crypto/rsa"
  7. "crypto/sha1"
  8. "crypto/x509"
  9. "encoding/base64"
  10. "encoding/hex"
  11. "encoding/pem"
  12. "fmt"
  13. "sort"
  14. )
  15. func HttpPost(url string, bizData map[string]interface{}) (rspBody []byte, err error) {
  16. // 签名
  17. // timestamp := int64(time.Now().Unix())
  18. return
  19. }
  20. // 签名规范:
  21. // 1、表单提交方式:form-data
  22. // 示例:1B2M2Y8AsgTpgAmY7PhCfg==
  23. // 2、请求头部参数
  24. // 参数1:sign(签名值,具体算法参考一下的前面算法)
  25. // 参数2:timestamp(时间戳,13位)
  26. // 3、请求体参数:
  27. // 参数1:appId(appId值,每个接入者唯一一个)
  28. // 参数2:timestamp(时间戳,13位,与上述一致)
  29. // 参数3:bizData(json字符串,举个例子,比方说要传合同编号如:{"contractNo":"0001"})
  30. // 4、签名算法:
  31. // 4.1、将上述3所属的bizData(json字符串),按照阿拉伯字母排序(如:{"ba":1,"ac":2}--->{"ac":2,"ba":1}),
  32. // 4.2、将4.1排序后的字符串,将【bizData+md5(bizData)+ appId + timestatmp】拼接后利用RSA非对称加密算法(SHA1withRSA),计算出最后的签名sign,对其base64编码,放入head的key(sign)中。
  33. //
  34. // String sign ;
  35. // String rsaSuffix = jsonStr + DigestUtils.md5Hex ( jsonStr ) + appId + timestatmp ;
  36. // byte [] bytes = RSAUtils.generateSHA1withRSASignature ( rsaSuffix, privateKey ) ;
  37. // try {
  38. // sign = Base64Utils.encode ( bytes ) ;
  39. // sign = sign.replaceAll ( " \r\n" , "" ) ;
  40. // } catch ( Exception e ) {
  41. // log.error( "sdk异常", e ) ;
  42. // return ApiRespBody.create ( ApiResponseInfo. _ERROR ) ;
  43. // }
  44. func getSignature(bizData map[string]interface{}, appId, timestamp, privateKey string) (signature string, err error) {
  45. pem10, _ := pem.Decode([]byte(privateKey))
  46. privateKey10I, _ := x509.ParsePKCS8PrivateKey(pem10.Bytes)
  47. privateKey10 := privateKey10I.(*rsa.PrivateKey)
  48. // sort by key
  49. sortedData := sortMapByKey(bizData)
  50. // md5(bizData)
  51. m := md5.New()
  52. m.Write([]byte(sortedData))
  53. bdMd5Hx := hex.EncodeToString(m.Sum(nil))
  54. // 待签内容
  55. message := sortedData + bdMd5Hx + appId + timestamp
  56. h := sha1.New()
  57. h.Write([]byte(message))
  58. sum := h.Sum(nil)
  59. // 使用私钥进行签名
  60. sign, err := rsa.SignPKCS1v15(rand.Reader, privateKey10, crypto.SHA1, sum)
  61. if err != nil {
  62. fmt.Println("Error signing:", err)
  63. return
  64. }
  65. // fmt.Println(signature)
  66. signature = base64.StdEncoding.EncodeToString(sign)
  67. return
  68. }
  69. // sortMapByKey 按key排序map返回string
  70. func sortMapByKey(data map[string]interface{}) (sortedData string) {
  71. keys := make([]string, 0, len(data))
  72. for k := range data {
  73. keys = append(keys, k)
  74. }
  75. sort.Strings(keys)
  76. for i, k := range keys {
  77. if i > 0 {
  78. sortedData += ","
  79. }
  80. switch data[k].(type) {
  81. case string:
  82. sortedData += fmt.Sprintf(`"%s":"%s"`, k, data[k].(string))
  83. case map[string]interface{}:
  84. sortedData += fmt.Sprintf(`"%s":%s`, k, sortMapByKey(data[k].(map[string]interface{})))
  85. case []interface{}:
  86. list := data[k].([]interface{})
  87. sortedData += fmt.Sprintf(`"%s":[`, k)
  88. for j, item := range list {
  89. if j > 0 {
  90. sortedData += ","
  91. }
  92. sortedData += sortMapByKey(item.(map[string]interface{}))
  93. }
  94. sortedData += "]"
  95. default:
  96. sortedData += fmt.Sprintf(`"%s":%v`, k, data[k])
  97. }
  98. }
  99. return fmt.Sprintf("{%s}", sortedData)
  100. }
  101. // func main() {
  102. // datas := make(map[string]interface{})
  103. // json.Unmarshal([]byte(`{"account":"12345","name":"张三","map":{"ccc":"adfasd","aaa":34355},"list":[{"ccc":"adfasd","aaa":34355},{"dddd":"8ghg","kkkk":12.55}]}`), &datas)
  104. // getSignature(datas, "290912417", "1701866011940",
  105. // `-----BEGIN PRIVATE KEY-----
  106. // MIIBUwIBADANBgkqhkiG9w0BAQEFAASCAT0wggE5AgEAAkEAkMD+72J6iAF0ZNV+3t628lsRHfJ80nKZWK5/C7Pg+AZmOIzJlwHsKhRzCvxoxqYHQprhiFzW9l73v9vD9l1JYwIDAQABAkBVijccr01JYdKuY5t9iI8D2NzcnZc1pZMI3NUmzT18Uyg7b9CUvGHlLeg/gdT4QtVd7wIzHYCY4letEcEMh54BAiEAwzNWusj5XiLmty7PI0Hbakx4HtcND1+P0UHLEWqWOuECIQC91zQuL7nStgGzT3HvaeBB5Ouapa39fHRm2nCjHaxwwwIgRR2XdvmUOj23XWMomr5F14SN/7V7fVcD0D8wjNElsmECIDYavV5kb7tj7/wgqkInlKhzC8rZaUsTS0F9BBkY/eptAiAQJ8Saz8YlMIESdHMxANGSog01fECbcZqLFMuNf8SorA==
  107. // -----END PRIVATE KEY-----`)
  108. // }