-
Notifications
You must be signed in to change notification settings - Fork 429
/
encode.go
65 lines (51 loc) · 1.28 KB
/
encode.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
package alipay
import (
"bytes"
"github.com/smartwalle/nsign"
"net/url"
"sort"
"strings"
)
type Encoder struct {
}
// EncodeValues
// 1、将参数名及其对应的值进行升序排序
// 2、将排序后的参数名及参数名使用等号进行连接,例如:a=10
// 3、将组合之后的参数使用&号进行连接,例如:a=10&b=20&c=30&c=31
func (encoder *Encoder) EncodeValues(buffer *bytes.Buffer, values url.Values, opts *nsign.SignOptions) ([]byte, error) {
if values == nil {
return nil, nil
}
if opts.Prefix != "" {
buffer.WriteString(opts.Prefix)
}
var pairs = make([]string, 0, len(values))
for key := range values {
if _, ok := opts.Ignores[key]; ok {
continue
}
var nValues = values[key]
for _, value := range nValues {
pairs = append(pairs, key+"="+value)
}
}
sort.Strings(pairs)
buffer.WriteString(strings.Join(pairs, "&"))
if opts.Suffix != "" {
buffer.WriteString(opts.Suffix)
}
return buffer.Bytes(), nil
}
func (encoder *Encoder) EncodeBytes(buffer *bytes.Buffer, data []byte, opts *nsign.SignOptions) ([]byte, error) {
if data == nil {
return nil, nil
}
if opts.Prefix != "" {
buffer.WriteString(opts.Prefix)
}
buffer.Write(data)
if opts.Suffix != "" {
buffer.WriteString(opts.Suffix)
}
return buffer.Bytes(), nil
}