update
This commit is contained in:
@@ -14,7 +14,7 @@ import (
|
||||
"time"
|
||||
|
||||
"github.com/tjfoc/gmsm/sm2"
|
||||
"github.com/tjfoc/gmsm/sm3"
|
||||
"github.com/tjfoc/gmsm/x509"
|
||||
)
|
||||
|
||||
// CompanyInfoService 公司信息查询服务
|
||||
@@ -122,7 +122,7 @@ func NewTaxHealthService(userID, privateKey string) *TaxHealthService {
|
||||
}
|
||||
client := &http.Client{
|
||||
Transport: tr,
|
||||
Timeout: 30 * time.Second,
|
||||
Timeout: 60 * time.Second,
|
||||
}
|
||||
|
||||
httpClient := &HTTPClient{Client: client}
|
||||
@@ -138,8 +138,8 @@ func NewTaxHealthService(userID, privateKey string) *TaxHealthService {
|
||||
func (s *CompanyInfoService) GetCompanyInfo(companyName string, apis []string) (*CompanyInfoResponse, error) {
|
||||
// 生成随机数和时间戳
|
||||
r, _ := rand.Int(rand.Reader, big.NewInt(10000))
|
||||
nonce := strconv.FormatInt(time.Now().UnixNano(), 36) + strconv.FormatInt(r.Int64(), 10)
|
||||
timestamp := strconv.FormatInt(time.Now().Unix(), 10)
|
||||
nonce := strconv.FormatInt(r.Int64(), 10)
|
||||
timestamp := strconv.FormatInt(time.Now().Unix()*1000, 10)
|
||||
|
||||
// 构建查询参数
|
||||
params := url.Values{}
|
||||
@@ -183,11 +183,12 @@ func (s *CompanyInfoService) GetCompanyInfo(companyName string, apis []string) (
|
||||
func (s *TaxHealthService) EvaluateTaxHealth(request TaxEvalRequest) (*TaxEvalResponse, error) {
|
||||
// 生成随机数和时间戳
|
||||
r, _ := rand.Int(rand.Reader, big.NewInt(10000))
|
||||
nonce := strconv.FormatInt(time.Now().UnixNano(), 36) + strconv.FormatInt(r.Int64(), 10)
|
||||
timestamp := strconv.FormatInt(time.Now().Unix(), 10)
|
||||
|
||||
nonce := strconv.FormatInt(r.Int64(), 10)
|
||||
timestamp := strconv.FormatInt(time.Now().Unix()*1000, 10)
|
||||
fmt.Println(nonce)
|
||||
fmt.Println(timestamp)
|
||||
apiURL := "https://api.cmdp.cn/v1/eval/tax-risk"
|
||||
|
||||
fmt.Println(apiURL)
|
||||
// 序列化请求体
|
||||
jsonData, err := json.Marshal(request)
|
||||
fmt.Println(string(jsonData))
|
||||
@@ -198,6 +199,7 @@ func (s *TaxHealthService) EvaluateTaxHealth(request TaxEvalRequest) (*TaxEvalRe
|
||||
// 生成签名
|
||||
signData := s.userID + timestamp + nonce + apiURL + string(jsonData)
|
||||
signature, err := s.generateSignature([]byte(signData))
|
||||
fmt.Println(signature)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("生成签名失败: %v", err)
|
||||
}
|
||||
@@ -268,55 +270,47 @@ func (s *TaxHealthService) QueryTaxEvalResult(orderNo string) (*TaxEvalQueryResp
|
||||
|
||||
// generateSignature 生成签名
|
||||
func (s *CompanyInfoService) generateSignature(plainText []byte) (string, error) {
|
||||
privateKey, err := parsePrivateKeyFromHex(s.privateKey)
|
||||
return sign(plainText, s.privateKey)
|
||||
}
|
||||
func sign(data []byte, privateKeyHex string) (string, error) {
|
||||
prvKeyBytes, err := hex.DecodeString(privateKeyHex)
|
||||
if err != nil {
|
||||
return "", fmt.Errorf("解析私钥失败: %v", err)
|
||||
return "", fmt.Errorf("failed to decode private key hex: %v", err)
|
||||
}
|
||||
|
||||
digest := sm3.Sm3Sum(plainText)
|
||||
signature, err := privateKey.Sign(rand.Reader, digest, nil)
|
||||
if err != nil {
|
||||
return "", fmt.Errorf("签名失败: %v", err)
|
||||
var prvKey *sm2.PrivateKey
|
||||
if pkcs8Key, err := x509.ParsePKCS8PrivateKey(prvKeyBytes, nil); err == nil {
|
||||
prvKey = pkcs8Key
|
||||
} else {
|
||||
prvKey = &sm2.PrivateKey{}
|
||||
prvKey.Curve = sm2.P256Sm2()
|
||||
prvKey.D = new(big.Int).SetBytes(prvKeyBytes)
|
||||
if prvKey.D.Sign() == 0 {
|
||||
return "", fmt.Errorf("failed to parse private key")
|
||||
}
|
||||
prvKey.PublicKey.Curve = sm2.P256Sm2()
|
||||
// 使用ScalarBaseMult计算公钥坐标,这是SM2曲线标准做法
|
||||
prvKey.PublicKey.X, prvKey.PublicKey.Y = sm2.P256Sm2().ScalarBaseMult(prvKey.D.Bytes())
|
||||
}
|
||||
|
||||
return base64.StdEncoding.EncodeToString(signature), nil
|
||||
r, s, err := sm2.Sm2Sign(prvKey, data, nil, rand.Reader)
|
||||
if err != nil {
|
||||
return "", fmt.Errorf("sign failed: %v", err)
|
||||
}
|
||||
|
||||
sig, err := sm2.SignDigitToSignData(r, s)
|
||||
if err != nil {
|
||||
return "", fmt.Errorf("marshal signature failed: %v", err)
|
||||
}
|
||||
|
||||
return base64.StdEncoding.EncodeToString(sig), nil
|
||||
}
|
||||
|
||||
// generateSignature 生成签名
|
||||
func (s *TaxHealthService) generateSignature(plainText []byte) (string, error) {
|
||||
privateKey, err := parsePrivateKeyFromHex(s.privateKey)
|
||||
if err != nil {
|
||||
return "", fmt.Errorf("解析私钥失败: %v", err)
|
||||
}
|
||||
|
||||
digest := sm3.Sm3Sum(plainText)
|
||||
signature, err := privateKey.Sign(rand.Reader, digest, nil)
|
||||
if err != nil {
|
||||
return "", fmt.Errorf("签名失败: %v", err)
|
||||
}
|
||||
return sign(plainText, s.privateKey)
|
||||
|
||||
return base64.StdEncoding.EncodeToString(signature), nil
|
||||
}
|
||||
|
||||
// parsePrivateKeyFromHex 从十六进制字符串解析私钥
|
||||
func parsePrivateKeyFromHex(privateKeyHex string) (*sm2.PrivateKey, error) {
|
||||
privateKeyBytes, err := hex.DecodeString(privateKeyHex)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("解码私钥失败: %v", err)
|
||||
}
|
||||
|
||||
d := new(big.Int).SetBytes(privateKeyBytes)
|
||||
|
||||
privateKey := &sm2.PrivateKey{
|
||||
PublicKey: sm2.PublicKey{
|
||||
Curve: sm2.P256Sm2(),
|
||||
},
|
||||
D: d,
|
||||
}
|
||||
|
||||
privateKey.PublicKey.X, privateKey.PublicKey.Y = privateKey.PublicKey.Curve.ScalarBaseMult(d.Bytes())
|
||||
|
||||
return privateKey, nil
|
||||
}
|
||||
|
||||
// joinAPIParams 连接API参数
|
||||
|
||||
Reference in New Issue
Block a user