interface_test.go 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. /*
  2. * @Author: zkj
  3. * @Date: 2021-04-25 13:38:31
  4. * @LastEditTime: 2021-04-25 15:50:07
  5. * @LastEditors: Please set LastEditors
  6. * @Description: 实名认证测试
  7. * @FilePath: /order-server/nppa/interface_test.go
  8. */
  9. package nppa
  10. import (
  11. "crypto/aes"
  12. "crypto/cipher"
  13. "crypto/rand"
  14. "crypto/sha256"
  15. "encoding/base64"
  16. "encoding/hex"
  17. "encoding/json"
  18. "io"
  19. "sort"
  20. "testing"
  21. "box-3rdServer/utils"
  22. )
  23. func TestMakeRequestBody(t *testing.T) {
  24. mapBody := make(map[string]string)
  25. mapBody["ai"] = "test-accountId"
  26. mapBody["name"] = "用户姓名"
  27. mapBody["idNum"] = "371321199012310912"
  28. pack, _ := json.Marshal(mapBody)
  29. secretKey := "2836e95fcd10e04b0069bb1ee659955b"
  30. key, _ := hex.DecodeString(secretKey)
  31. block, err := aes.NewCipher(key)
  32. if err != nil {
  33. t.Fatal(err)
  34. }
  35. aesGcm, err := cipher.NewGCM(block)
  36. if err != nil {
  37. t.Fatal(err)
  38. }
  39. nonce := make([]byte, aesGcm.NonceSize())
  40. if _, err := io.ReadFull(rand.Reader, nonce); err != nil {
  41. t.Fatal(err)
  42. }
  43. cipherText := aesGcm.Seal(nonce, nonce, pack, nil)
  44. // encode as base64 string
  45. encoded := base64.StdEncoding.EncodeToString(cipherText)
  46. ret := make(map[string]string)
  47. ret["data"] = encoded
  48. pack, _ = json.Marshal(ret)
  49. t.Log(string(pack))
  50. }
  51. func TestMakeRequestSign(t *testing.T) {
  52. secretKey := "2836e95fcd10e04b0069bb1ee659955b"
  53. body := `{"data":"CqT/33f3jyoiYqT8MtxEFk3x2rlfhmgzhxpHqWosSj4d3hq2EbrtVyx2aLj565ZQNTcPrcDipnvpq/D/vQDaLKW70O83Q42zvR0//OfnYLcIjTPMnqa+SOhsjQrSdu66ySSORCAo"}`
  54. var kvs utils.Kvs
  55. kvs = append(kvs, &utils.Kv{Key: "appId", Value: "test-appId"})
  56. kvs = append(kvs, &utils.Kv{Key: "bizId", Value: "test-bizId"})
  57. kvs = append(kvs, &utils.Kv{Key: "timestamps", Value: "1584949895758"})
  58. kvs = append(kvs, &utils.Kv{Key: "id", Value: "test-id"})
  59. kvs = append(kvs, &utils.Kv{Key: "name", Value: "test-name"})
  60. sort.Sort(kvs)
  61. key := ""
  62. for i := 0; i < kvs.Len(); i++ {
  63. key += kvs[i].Key + kvs[i].Value
  64. }
  65. strEncode := secretKey + key + body
  66. t.Log(strEncode, strEncode == `2836e95fcd10e04b0069bb1ee659955bappIdtest-appIdbizIdtest-bizIdidtest-idnametest-nametimestamps1584949895758{"data":"CqT/33f3jyoiYqT8MtxEFk3x2rlfhmgzhxpHqWosSj4d3hq2EbrtVyx2aLj565ZQNTcPrcDipnvpq/D/vQDaLKW70O83Q42zvR0//OfnYLcIjTPMnqa+SOhsjQrSdu66ySSORCAo"}`)
  67. h2 := sha256.New()
  68. h2.Write([]byte(strEncode))
  69. hashed := h2.Sum(nil)
  70. sign := hex.EncodeToString(hashed)
  71. t.Log(string(sign), string(sign) == "386c03b776a28c06b8032a958fbd89337424ef45c62d0422706cca633d8ad5fd")
  72. }