/* * @Author: zkj * @Date: 2021-04-25 13:38:31 * @LastEditTime: 2021-04-25 15:50:07 * @LastEditors: Please set LastEditors * @Description: 实名认证测试 * @FilePath: /order-server/nppa/interface_test.go */ package nppa import ( "crypto/aes" "crypto/cipher" "crypto/rand" "crypto/sha256" "encoding/base64" "encoding/hex" "encoding/json" "io" "sort" "testing" "box-3rdServer/utils" ) func TestMakeRequestBody(t *testing.T) { mapBody := make(map[string]string) mapBody["ai"] = "test-accountId" mapBody["name"] = "用户姓名" mapBody["idNum"] = "371321199012310912" pack, _ := json.Marshal(mapBody) secretKey := "2836e95fcd10e04b0069bb1ee659955b" key, _ := hex.DecodeString(secretKey) block, err := aes.NewCipher(key) if err != nil { t.Fatal(err) } aesGcm, err := cipher.NewGCM(block) if err != nil { t.Fatal(err) } nonce := make([]byte, aesGcm.NonceSize()) if _, err := io.ReadFull(rand.Reader, nonce); err != nil { t.Fatal(err) } cipherText := aesGcm.Seal(nonce, nonce, pack, nil) // encode as base64 string encoded := base64.StdEncoding.EncodeToString(cipherText) ret := make(map[string]string) ret["data"] = encoded pack, _ = json.Marshal(ret) t.Log(string(pack)) } func TestMakeRequestSign(t *testing.T) { secretKey := "2836e95fcd10e04b0069bb1ee659955b" body := `{"data":"CqT/33f3jyoiYqT8MtxEFk3x2rlfhmgzhxpHqWosSj4d3hq2EbrtVyx2aLj565ZQNTcPrcDipnvpq/D/vQDaLKW70O83Q42zvR0//OfnYLcIjTPMnqa+SOhsjQrSdu66ySSORCAo"}` var kvs utils.Kvs kvs = append(kvs, &utils.Kv{Key: "appId", Value: "test-appId"}) kvs = append(kvs, &utils.Kv{Key: "bizId", Value: "test-bizId"}) kvs = append(kvs, &utils.Kv{Key: "timestamps", Value: "1584949895758"}) kvs = append(kvs, &utils.Kv{Key: "id", Value: "test-id"}) kvs = append(kvs, &utils.Kv{Key: "name", Value: "test-name"}) sort.Sort(kvs) key := "" for i := 0; i < kvs.Len(); i++ { key += kvs[i].Key + kvs[i].Value } strEncode := secretKey + key + body t.Log(strEncode, strEncode == `2836e95fcd10e04b0069bb1ee659955bappIdtest-appIdbizIdtest-bizIdidtest-idnametest-nametimestamps1584949895758{"data":"CqT/33f3jyoiYqT8MtxEFk3x2rlfhmgzhxpHqWosSj4d3hq2EbrtVyx2aLj565ZQNTcPrcDipnvpq/D/vQDaLKW70O83Q42zvR0//OfnYLcIjTPMnqa+SOhsjQrSdu66ySSORCAo"}`) h2 := sha256.New() h2.Write([]byte(strEncode)) hashed := h2.Sum(nil) sign := hex.EncodeToString(hashed) t.Log(string(sign), string(sign) == "386c03b776a28c06b8032a958fbd89337424ef45c62d0422706cca633d8ad5fd") }