main.go
3.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
package main
import (
"bufio"
"crypto/rand"
"crypto/rsa"
"crypto/x509"
"encoding/pem"
"flag"
socialwork_sdk "gitlab.workai.com.cn/chenang/socialwork-sdk"
"io/ioutil"
"os"
)
/*func init(){
var bits int
flag.IntVar(&bits, "b", 1024, "密钥长度,默认为1024位")
flag.Parse()
if err := GenRsaKey(bits); err != nil {
log.Fatal("密钥文件生成失败!")
}
log.Println("密钥文件生成成功!")
}
*/
func main() {
var key string
var iv string
var privateKeyPath string
var publicKeyPath string
var ip string
var bussinessCode string
var ptCode string
var reqBody string
var errFilePath string
var resultFilePath string
flag.StringVar(&key, "key", "", "key")
flag.StringVar(&iv, "iv", "", "iv")
flag.StringVar(&privateKeyPath, "privateKeyPath", "", "privateKeyPath")
flag.StringVar(&publicKeyPath, "publicKeyPath", "", "publicKeyPath")
flag.StringVar(&ip, "ip", "", "ip")
flag.StringVar(&bussinessCode, "bussiness_code", "", "bussiness_code")
flag.StringVar(&ptCode, "ptCode", "", "ptCode")
flag.StringVar(&reqBody, "reqBody", "", "reqBody")
flag.StringVar(&errFilePath, "errFilePath", "", "errFilePath")
flag.StringVar(&resultFilePath, "resultFilePath", "", "resultFilePath")
flag.Parse()
if key == "" || iv == "" || privateKeyPath == "" || publicKeyPath == "" || ip == "" || bussinessCode == "" || ptCode == "" || reqBody == "" || resultFilePath == "" || errFilePath == "" {
os.Exit(1)
}
fd, _ := os.OpenFile(errFilePath, os.O_RDWR|os.O_CREATE|os.O_APPEND, 0644)
defer fd.Close()
publicKey, err := ioutil.ReadFile(publicKeyPath)
if err != nil {
write := bufio.NewWriter(fd)
write.WriteString("命令行执行错误:" + err.Error() + "\r\n")
write.Flush()
return
}
privateKey, err := ioutil.ReadFile(privateKeyPath)
if err != nil {
write := bufio.NewWriter(fd)
write.WriteString("命令行执行错误:" + err.Error() + "\r\n")
write.Flush()
return
}
client := socialwork_sdk.NewOrderClient(key, iv, string(privateKey), string(publicKey), ip, ptCode)
req, err := client.SendRequest(bussinessCode, reqBody)
if err != nil {
write := bufio.NewWriter(fd)
write.WriteString("命令行执行错误:" + err.Error() + "\r\n")
write.Flush()
return
} else {
file, err := os.Create(resultFilePath)
defer file.Close()
if err != nil {
write := bufio.NewWriter(fd)
write.WriteString("命令行执行错误:" + err.Error() + "\r\n")
write.Flush()
return
}
file.WriteString(req)
return
}
}
func GenRsaKey(bits int) error {
// 生成私钥文件
privateKey, err := rsa.GenerateKey(rand.Reader, bits)
if err != nil {
return err
}
derStream := x509.MarshalPKCS1PrivateKey(privateKey)
block := &pem.Block{
Type: "私钥",
Bytes: derStream,
}
file, err := os.Create("private.pem")
if err != nil {
return err
}
err = pem.Encode(file, block)
if err != nil {
return err
}
// 生成公钥文件
publicKey := &privateKey.PublicKey
derPkix, err := x509.MarshalPKIXPublicKey(publicKey)
if err != nil {
return err
}
block = &pem.Block{
Type: "公钥",
Bytes: derPkix,
}
file, err = os.Create("public.pem")
if err != nil {
return err
}
err = pem.Encode(file, block)
if err != nil {
return err
}
return nil
}