forked from youjianglong/swiftpass
-
Notifications
You must be signed in to change notification settings - Fork 0
/
util.go
146 lines (127 loc) · 2.69 KB
/
util.go
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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
package swiftpass
import (
"crypto/md5"
"crypto/rsa"
"crypto/x509"
"encoding/hex"
"encoding/pem"
"encoding/xml"
"fmt"
"io"
"reflect"
"sort"
"strconv"
"strings"
)
func Md5String(str string) string {
h := md5.New()
h.Write([]byte(str))
return hex.EncodeToString(h.Sum(nil))
}
func Xml2Map(reader io.Reader) map[string]string {
val := XMLStringMap{}
_ = xml.NewDecoder(reader).Decode(&val)
return val
}
//对字典key进行排序
func SortKeys(m map[string]string) []string {
keys := make([]string, 0)
for key, _ := range m {
keys = append(keys, key)
}
sort.Strings(keys)
return keys
}
//生成url请求串
func BuildQueryString(params map[string]string) string {
signParams := map[string]string{}
for k, v := range params {
if v == "" {
continue
}
signParams[k] = v
}
keys := SortKeys(signParams)
queryStr := ""
for _, key := range keys {
queryStr += key + "=" + signParams[key] + "&"
}
return queryStr[:len(queryStr)-1]
}
//解码私钥
func DecodePrivateKey(content []byte) (*rsa.PrivateKey, error) {
block, _ := pem.Decode(content)
return x509.ParsePKCS1PrivateKey(block.Bytes)
}
//解码公钥
func DecodePublicKey(content []byte) (*rsa.PublicKey, error) {
block, _ := pem.Decode(content)
return x509.ParsePKCS1PublicKey(block.Bytes)
}
func XmlObject2Map(obj interface{}) map[string]string {
valueOf := reflect.ValueOf(obj)
kind := valueOf.Kind()
for kind == reflect.Ptr {
valueOf = valueOf.Elem()
kind = valueOf.Kind()
}
if kind != reflect.Struct {
return nil
}
typeOf := valueOf.Type()
m := make(map[string]string)
num := valueOf.NumField()
for i := 0; i < num; i++ {
fieldType := typeOf.Field(i)
if !fieldType.IsExported() || fieldType.Name == "XMLName" {
continue
}
tag := fieldType.Tag.Get("xml")
if tag == "-" {
continue
}
if fieldType.Anonymous {
subMap := XmlObject2Map(valueOf.Field(i).Interface())
if subMap != nil {
for k, v := range subMap {
m[k] = v
}
}
continue
}
tags := strings.Split(tag, ",")
key := strings.TrimSpace(tags[0])
if key == "" {
key = fieldType.Name
}
var omitempty bool
if len(tags) > 1 && strings.Contains(tag, "omitempty") {
omitempty = true
}
fieldValue := valueOf.Field(i)
var val string
switch fieldValue.Kind() {
case reflect.String:
val = fieldValue.String()
case reflect.Float64, reflect.Float32:
val = strconv.FormatFloat(fieldValue.Float(), 'f', -1, 64)
case reflect.Bool:
if fieldValue.Bool() {
val = "true"
} else {
if omitempty {
val = ""
} else {
val = "false"
}
}
default:
val = fmt.Sprintf("%v", fieldValue.Interface())
}
if val == "" && omitempty {
continue
}
m[key] = val
}
return m
}