-
Notifications
You must be signed in to change notification settings - Fork 0
/
sms.go
56 lines (44 loc) · 1.18 KB
/
sms.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
package qiniu
import (
"net/url"
"github.com/duke-git/lancet/v2/cryptor"
"github.com/duke-git/lancet/v2/netutil"
)
// SMS sms
type SMS struct{}
// NewSMS new sms
func NewSMS() *SMS {
return &SMS{}
}
// Sign sms sign
func (s *SMS) Sign(args *netutil.HttpRequest) (string, error) {
u, err := url.Parse(args.RawURL)
if err != nil {
return "", err
}
// 1. 添加 Path
data := args.Method + " " + u.Path
// 2. 添加 Query,前提: Query 存在且不为空
if u.RawQuery != "" {
data += "?" + u.RawQuery
}
// 3. 添加 Host
data += "\nHost: " + u.Host
// 4. 添加 Content-Type,前提: Content-Type 存在且不为空
ctType := args.Headers.Get("Content-Type")
if ctType != "" {
data += "\nContent-Type: " + ctType
}
// 5. 添加回车
data += ("\n\n")
// 6. 添加 Body,前提: Content-Length 存在且 Body 不为空,
// 同时 Content-Type 存在且不为空或 "application/octet-stream"
ctLength := args.Headers.Get("Content-Length")
if ctLength != "" && len(args.Body) != 0 {
if ctType != "" && ctType != "application/octet-stream" {
data += string(args.Body)
}
}
str := cryptor.HmacSha1(data, SecretKey)
return cryptor.Base64StdEncode(str), nil
}