Skip to content

Commit 541eda4

Browse files
committed
添加代码
1 parent b1caec7 commit 541eda4

File tree

4 files changed

+93
-0
lines changed

4 files changed

+93
-0
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,3 +10,5 @@
1010

1111
# Output of the go coverage tool, specifically when used with LiteIDE
1212
*.out
13+
14+
.idea

TLSSigAPITest.go

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package main
2+
3+
import (
4+
"./tencentyun"
5+
"fmt"
6+
)
7+
8+
const (
9+
sdkappid = 1400000000
10+
key = "5bd2850fff3ecb11d7c805251c51ee463a25727bddc2385f3fa8bfee1bb93b5e"
11+
)
12+
13+
func main() {
14+
sig, err := tencentyun.GenSig(sdkappid, key, "xiaojun", 86400*180)
15+
if err != nil {
16+
fmt.Println(err.Error())
17+
} else {
18+
fmt.Println(sig)
19+
}
20+
}

tencentyun/TLSSigAPI.go

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
package tencentyun
2+
3+
import (
4+
"bytes"
5+
"compress/zlib"
6+
"crypto/hmac"
7+
"crypto/sha256"
8+
"encoding/base64"
9+
"encoding/json"
10+
"fmt"
11+
"strconv"
12+
"time"
13+
)
14+
15+
func hmacsha256(sdkappid int, key string, identifier string, currTime int64, expire int) string {
16+
var contentToBeSigned string
17+
contentToBeSigned = "TLS.identifier:" + identifier + "\n"
18+
contentToBeSigned += "TLS.sdkappid:" + strconv.Itoa(sdkappid) + "\n"
19+
contentToBeSigned += "TLS.time:" + strconv.FormatInt(currTime, 10) + "\n"
20+
contentToBeSigned += "TLS.expire:" + strconv.Itoa(expire) + "\n"
21+
22+
fmt.Println(contentToBeSigned)
23+
24+
h := hmac.New(sha256.New, []byte(key))
25+
h.Write([]byte(contentToBeSigned))
26+
return base64.StdEncoding.EncodeToString(h.Sum(nil))
27+
}
28+
29+
func GenSig(sdkappid int, key string, identifier string, expire int) (string, error) {
30+
currTime := time.Now().Unix()
31+
var sigDoc map[string]interface{}
32+
sigDoc = make(map[string]interface{})
33+
sigDoc["TLS.ver"] = "2.0"
34+
sigDoc["TLS.identifier"] = identifier
35+
sigDoc["TLS.sdkappid"] = sdkappid
36+
sigDoc["TLS.expire"] = expire
37+
sigDoc["TLS.time"] = currTime
38+
sigDoc["TLS.sig"] = hmacsha256(sdkappid, key, identifier, currTime, expire)
39+
40+
data, err := json.Marshal(sigDoc)
41+
if err != nil {
42+
return "", err
43+
}
44+
45+
var b bytes.Buffer
46+
w := zlib.NewWriter(&b)
47+
w.Write(data)
48+
w.Close()
49+
return base64urlEncode(b.Bytes()), nil
50+
}

tencentyun/base64url.go

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package tencentyun
2+
3+
import (
4+
"encoding/base64"
5+
"strings"
6+
)
7+
8+
func base64urlEncode(data []byte) string {
9+
str := base64.StdEncoding.EncodeToString(data)
10+
str = strings.Replace(str, "+", "*", -1)
11+
str = strings.Replace(str, "/", "-", -1)
12+
str = strings.Replace(str, "=", "_", -1)
13+
return str
14+
}
15+
16+
func base64urlDecode(str string) ([]byte, error) {
17+
str = strings.Replace(str, "_", "=", -1)
18+
str = strings.Replace(str, "-", "/", -1)
19+
str = strings.Replace(str, "*", "+", -1)
20+
return base64.StdEncoding.DecodeString(str)
21+
}

0 commit comments

Comments
 (0)