go中国家新闻出版署实名认证

go中国家新闻出版署实名认证
这个接⼝中请求体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 {
AppId    string // 应⽤标识
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 {

本文发布于:2024-09-23 10:19:52,感谢您对本站的认可!

本文链接:https://www.17tex.com/tex/3/259570.html

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。

标签:信息   请求   标识   实名
留言与评论(共有 0 条评论)
   
验证码:
Copyright ©2019-2024 Comsenz Inc.Powered by © 易纺专利技术学习网 豫ICP备2022007602号 豫公网安备41160202000603 站长QQ:729038198 关于我们 投诉建议