1 Star 0 Fork 0

kzangv/gsf-fof

Create your Gitee Account
Explore and code with more than 12 million developers,Free private repositories !:)
Sign up
Clone or Download
feisu_secret.go 1.27 KB
Copy Edit Raw Blame History
kzangv authored 2023-02-23 11:08 . fixed
package feishu
import (
"crypto/hmac"
"crypto/sha256"
"encoding/base64"
"encoding/json"
"fmt"
"time"
)
type SecretMethod struct {
WhileIpMethod
secret string
}
func (m *SecretMethod) Init(hook, secret string) {
m.hook, m.secret = hook, secret
}
func (m *SecretMethod) sign(timestamp int64) (string, error) {
//timestamp + key 做sha256, 再进行base64 encode
stringToSign := fmt.Sprintf("%v", timestamp) + "\n" + m.secret
var data []byte
h := hmac.New(sha256.New, []byte(stringToSign))
_, err := h.Write(data)
if err != nil {
return "", err
}
signature := base64.StdEncoding.EncodeToString(h.Sum(nil))
return signature, nil
}
func (m *SecretMethod) getBody(val map[string]interface{}) ([]byte, error) {
tm := time.Now().Unix()
sign, err := m.sign(tm)
if err != nil {
return nil, err
}
val["timestamp"] = fmt.Sprintf("%d", tm)
val["sign"] = sign
return json.Marshal(val)
}
func (m *SecretMethod) SendSimpleMsg(content string) (bool, error) {
body, err := m.getBody(getSimpleMsg(content))
if err != nil {
return false, err
}
return post(m.hook, body)
}
func (m *SecretMethod) SendRichMsg(title string, content RichContent) (bool, error) {
body, err := m.getBody(getRichMsg(title, content))
if err != nil {
return false, err
}
return post(m.hook, body)
}
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
Go
1
https://gitee.com/kzangv/gsf-fof.git
git@gitee.com:kzangv/gsf-fof.git
kzangv
gsf-fof
gsf-fof
v0.4.2

Search