YouChuKoffee/app/utils/postbank/cmd/deCrypt.go

51 lines
1.3 KiB
Go
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

package cmd
import (
"bytes"
"encoding/base64"
"encoding/json"
"fmt"
"log"
"os"
"os/exec"
"path/filepath"
"qteam/app/utils/postbank/smtypes"
)
func DeCrypt(merchantId string, priKey string, sopPublicKey string, input *smtypes.RequestData) ([]byte, error) {
// 解密逻辑
requestJson, err := json.Marshal(input)
if err != nil {
return nil, fmt.Errorf("请求体json编码失败%s", err)
}
path, err := smv2()
if err != nil {
return nil, fmt.Errorf("解码获取当前目录失败:%s", err)
}
cmd := fmt.Sprintf("%s postbank decrypt --merchantId=\"%s\" --priKey=\"%s\" --sopPublicKey=\"%s\" --input=\"%s\" %s 2>&1",
path, merchantId, priKey, sopPublicKey, base64.StdEncoding.EncodeToString(requestJson), "--isRequest")
// 返回解密后的数据
return toExec(cmd), nil
}
func smv2() (string, error) {
path, err := os.Getwd()
if err != nil {
return "", err
}
return fmt.Sprintf("%s/%s/smv2", filepath.Dir(filepath.Dir(path)), "untils/sm"), nil
}
func toExec(cmdString string) []byte {
var stdoutBuf, stderrBuf bytes.Buffer
cmd := exec.Command(cmdString)
cmd.Stdout = &stdoutBuf
cmd.Stderr = &stderrBuf
err := cmd.Run()
if err != nil {
log.Fatalf("cmd.Run() failed with %s\n", err)
}
fmt.Printf("stdout: %s\nstderr: %s\n", stdoutBuf.String(), stderrBuf.String())
return stdoutBuf.Bytes()
}