Skip to content

Commit c8fca88

Browse files
committed
添加 userbuf 版本的接口
1 parent 1d9b977 commit c8fca88

File tree

3 files changed

+52
-3
lines changed

3 files changed

+52
-3
lines changed

README.md

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
## 说明
2+
此项目为 tls-sig-api-v2 (对称密钥版本)golang 实现版本,请注意,之前版本的非对称密钥无法使用此版 api,如需使用请查看[这里](https://github.com/tencentyun/tls-sig-api-golang)
3+
4+
## 使用
5+
``` go
6+
import (
7+
"./tencentyun"
8+
"fmt"
9+
)
10+
11+
const (
12+
sdkappid = 1400000000
13+
key = "5bd2850fff3ecb11d7c805251c51ee463a25727bddc2385f3fa8bfee1bb93b5e"
14+
)
15+
16+
func main() {
17+
sig, err := tencentyun.GenSig(sdkappid, key, "xiaojun", 86400*180)
18+
if err != nil {
19+
fmt.Println(err.Error())
20+
} else {
21+
fmt.Println(sig)
22+
}
23+
}
24+
```

TLSSigAPITest.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,4 +17,10 @@ func main() {
1717
} else {
1818
fmt.Println(sig)
1919
}
20+
sig, err = tencentyun.GenSigWithUserBuf(sdkappid, key, "xiaojun", 86400*180, []byte("abc"))
21+
if err != nil {
22+
fmt.Println(err.Error())
23+
} else {
24+
fmt.Println(sig)
25+
}
2026
}

tencentyun/TLSSigAPI.go

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,19 +11,22 @@ import (
1111
"time"
1212
)
1313

14-
func hmacsha256(sdkappid int, key string, identifier string, currTime int64, expire int) string {
14+
func hmacsha256(sdkappid int, key string, identifier string, currTime int64, expire int, base64UserBuf *string) string {
1515
var contentToBeSigned string
1616
contentToBeSigned = "TLS.identifier:" + identifier + "\n"
1717
contentToBeSigned += "TLS.sdkappid:" + strconv.Itoa(sdkappid) + "\n"
1818
contentToBeSigned += "TLS.time:" + strconv.FormatInt(currTime, 10) + "\n"
1919
contentToBeSigned += "TLS.expire:" + strconv.Itoa(expire) + "\n"
20+
if nil != base64UserBuf {
21+
contentToBeSigned += "TLS.userbuf:" + *base64UserBuf + "\n"
22+
}
2023

2124
h := hmac.New(sha256.New, []byte(key))
2225
h.Write([]byte(contentToBeSigned))
2326
return base64.StdEncoding.EncodeToString(h.Sum(nil))
2427
}
2528

26-
func GenSig(sdkappid int, key string, identifier string, expire int) (string, error) {
29+
func genSig(sdkappid int, key string, identifier string, expire int, userbuf []byte) (string, error) {
2730
currTime := time.Now().Unix()
2831
var sigDoc map[string]interface{}
2932
sigDoc = make(map[string]interface{})
@@ -32,7 +35,14 @@ func GenSig(sdkappid int, key string, identifier string, expire int) (string, er
3235
sigDoc["TLS.sdkappid"] = sdkappid
3336
sigDoc["TLS.expire"] = expire
3437
sigDoc["TLS.time"] = currTime
35-
sigDoc["TLS.sig"] = hmacsha256(sdkappid, key, identifier, currTime, expire)
38+
var base64UserBuf string
39+
if nil != userbuf {
40+
base64UserBuf = base64.StdEncoding.EncodeToString(userbuf)
41+
sigDoc["TLS.userbuf"] = base64UserBuf
42+
sigDoc["TLS.sig"] = hmacsha256(sdkappid, key, identifier, currTime, expire, &base64UserBuf)
43+
} else {
44+
sigDoc["TLS.sig"] = hmacsha256(sdkappid, key, identifier, currTime, expire, nil)
45+
}
3646

3747
data, err := json.Marshal(sigDoc)
3848
if err != nil {
@@ -45,3 +55,12 @@ func GenSig(sdkappid int, key string, identifier string, expire int) (string, er
4555
w.Close()
4656
return base64urlEncode(b.Bytes()), nil
4757
}
58+
59+
func GenSig(sdkappid int, key string, identifier string, expire int) (string, error) {
60+
return genSig(sdkappid, key, identifier, expire, nil)
61+
}
62+
63+
func GenSigWithUserBuf(sdkappid int, key string, identifier string, expire int, userbuf []byte) (string, error) {
64+
return genSig(sdkappid, key, identifier, expire, userbuf)
65+
}
66+

0 commit comments

Comments
 (0)