增加飞书webhook监听处理
This commit is contained in:
44
utils/feishu/decrypt.go
Normal file
44
utils/feishu/decrypt.go
Normal file
@@ -0,0 +1,44 @@
|
||||
package feishu
|
||||
|
||||
import (
|
||||
"crypto/aes"
|
||||
"crypto/cipher"
|
||||
"crypto/sha256"
|
||||
"encoding/base64"
|
||||
"errors"
|
||||
"fmt"
|
||||
"strings"
|
||||
)
|
||||
|
||||
// 解密事件消息
|
||||
func DecryptFeiShuWebhookMsg(encryptData string, encryptKey string) ([]byte, error) {
|
||||
buf, err := base64.StdEncoding.DecodeString(encryptData)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("base64StdEncode Error[%v]", err)
|
||||
}
|
||||
if len(buf) < aes.BlockSize {
|
||||
return nil, errors.New("cipher too short")
|
||||
}
|
||||
keyBs := sha256.Sum256([]byte(encryptKey))
|
||||
block, err := aes.NewCipher(keyBs[:sha256.Size])
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("AESNewCipher Error[%v]", err)
|
||||
}
|
||||
iv := buf[:aes.BlockSize]
|
||||
buf = buf[aes.BlockSize:]
|
||||
// CBC mode always works in whole blocks.
|
||||
if len(buf)%aes.BlockSize != 0 {
|
||||
return nil, errors.New("ciphertext is not a multiple of the block size")
|
||||
}
|
||||
mode := cipher.NewCBCDecrypter(block, iv)
|
||||
mode.CryptBlocks(buf, buf)
|
||||
n := strings.Index(string(buf), "{")
|
||||
if n == -1 {
|
||||
n = 0
|
||||
}
|
||||
m := strings.LastIndex(string(buf), "}")
|
||||
if m == -1 {
|
||||
m = len(buf) - 1
|
||||
}
|
||||
return buf[n : m+1], nil
|
||||
}
|
||||
22
utils/feishu/verify.go
Normal file
22
utils/feishu/verify.go
Normal file
@@ -0,0 +1,22 @@
|
||||
package feishu
|
||||
|
||||
import (
|
||||
"crypto/sha256"
|
||||
"fmt"
|
||||
"strings"
|
||||
)
|
||||
|
||||
// 计算签名
|
||||
func CalculateFeiShuWebhookSignature(timestamp, nonce, encryptKey string, body []byte) string {
|
||||
var b strings.Builder
|
||||
b.WriteString(timestamp)
|
||||
b.WriteString(nonce)
|
||||
b.WriteString(encryptKey)
|
||||
b.Write(body) //bodystring 指整个请求体,不要在反序列化后再计算
|
||||
bs := []byte(b.String())
|
||||
h := sha256.New()
|
||||
h.Write(bs)
|
||||
bs = h.Sum(nil)
|
||||
sig := fmt.Sprintf("%x", bs)
|
||||
return sig
|
||||
}
|
||||
Reference in New Issue
Block a user