这个接⼝中请求体body使⽤了AES-128/GCM + BASE64算法加密,签名使⽤了sha256。直接上完整的代码import ( "bytes"
"crypto/aes"
"crypto/cipher"
"crypto/rand"
"crypto/sha256"
"encoding/base64"
"encoding/hex"
"encoding/json"
"fmt"
"io"
"io/ioutil"
"net/http"
"sort"
"strconv"
"time"
)
type nppaInfo struct {
SecretKey string // 应⽤密钥
BizId string // 业务权限标识
}
var nppa nppaInfo
// 输⼊国家新闻出版署配置信息
func NppaInit(appId, secretKey, bizId string) {
nppa = nppaInfo{
紫花针茅AppId: appId,
SecretKey: secretKey,
霓虹灯变压器
BizId: bizId,
}
}
/
/ 实名验证
// ai - 游戏内部成员标识,固定32位字符,⼀般使⽤ md5(⽤户ID)
// name - 实名信息中的姓名
弱碱性水机// id - 实名信息中⾝份证号码
// 返回
// - result 是否验证成功
// - errcode 状态码
// - errmsg 状态描述
func NppaCheck(ai, name, id string) (result bool, errcode int, errmsg string ) {
client := &http.Client{}
// body 参数
param := map[string]string{
"ai": ai,
"name": name,
"idNum": id,
}
// AES-128/GCM + BASE64算法加密
jsonParam, _ := json.Marshal(param)
cipher, _ := gcmEncrypt(string(jsonParam))
body, _ := json.Marshal(map[string]string{
"data": cipher,
})
/
/ post请求地址
req, _ := http.NewRequest("POST", "api.v/idcard/authentication/check", bytes.NewReader(body))
// header头验证信息
headers := map[string]string{木工艺品制作
"appId": nppa.AppId,
"appId": nppa.AppId,
"bizId": nppa.BizId,
"timestamps": strconv.FormatInt(time.Now().UnixNano()/1e6, 10), }
// 签名
headers["sign"] = sign(headers, string(body))
headers["Content-Type"] = "application/json;charset=utf-8"
for k, v := range headers {
req.Header.Set(k, v)
}
// 请求
resp, err := client.Do(req)
if err != nil {
return false, -1, "Http请求错误"
}
defer resp.Body.Close()
if resp.StatusCode != 200 {
return false, resp.StatusCode, "Http请求返回码错误"
}
str, err := ioutil.ReadAll(resp.Body)
if err != nil {
return false, -2, "Http请求返回数据为空"
}
var msg map[string]interface{}
err = json.Unmarshal(str, &msg)
if err != nil {
return false, -3, "Http请求返回数据格式错误"
}
ec, ok := msg["errcode"]
if !ok {
return false, -3, "Http请求返回数据格式错误"
}
errcode = int(ec.(float64))
if errcode != 0 {
errmsg, _:= msg["errmsg"]
return false, errcode, errmsg.(string)
}
data, ok := msg["data"]
if !ok {
return false, -3, "Http请求返回数据格式错误"
}
r, ok := data.(map[string]interface{})["result"]
if !ok {
return false, -3, "Http请求返回数据格式错误"
}
status, ok := r.(map[string]interface{})["status"]
if !ok {
return false, -3, "Http请求返回数据格式错误"
}
errcode = int(status.(float64))
焊接卡盘
if errcode == 0{
return true, 0, "认证成功"
} else if errcode == 1 {
return false, errcode, "认证中"
}else {
return false, errcode, "认证失败"
}
内作}
func gcmEncrypt(originalText string) (string, error) {
// 需要解码
key, _ := hex.DecodeString(nppa.SecretKey)
block, err := aes.NewCipher(key)
if err != nil {