- fix dingding crypt bug

This commit is contained in:
gazebo
2019-03-09 13:59:04 +08:00
parent e0d459c280
commit 0e62d0a16c
4 changed files with 18 additions and 5 deletions

View File

@@ -1,8 +1,10 @@
package dingdingapi
import (
"bytes"
"crypto/sha1"
"encoding/base64"
"encoding/binary"
"errors"
"fmt"
"sort"
@@ -81,7 +83,12 @@ func (a *API) calculateCallbackSign(data map[string]interface{}) (sign string) {
func (a *API) Encrypt(msg string) (encryptedMsg string, err error) {
aesKey := a.GetCallbackAESKey()
binResult, err := utils.AESCBCEncpryt([]byte(msg), aesKey, aesKey[:16])
buf := bytes.NewBuffer(nil)
buf.WriteString(utils.GetUUID()[:16])
binary.Write(buf, binary.BigEndian, int32(len(msg)))
buf.WriteString(msg)
buf.WriteString(a.corpID)
binResult, err := utils.AESCBCEncpryt(buf.Bytes(), aesKey, aesKey[:16])
encryptedMsg = base64.StdEncoding.EncodeToString(binResult)
return encryptedMsg, err
}
@@ -92,7 +99,11 @@ func (a *API) Decrypt(msg string) (decryptedMsg string, err error) {
aesKey := a.GetCallbackAESKey()
binResult, err2 := utils.AESCBCDecpryt(binMsg, aesKey, aesKey[:16])
if err = err2; err == nil {
decryptedMsg = string(binResult)
var msgLen int32
if err = binary.Read(bytes.NewBuffer(binResult[16:]), binary.BigEndian, &msgLen); err == nil {
baseapi.SugarLogger.Debug(msgLen)
decryptedMsg = string(binResult[16+4 : 16+4+msgLen])
}
}
}
return decryptedMsg, err

View File

@@ -15,7 +15,7 @@ func TestCrypt(t *testing.T) {
func TestDecrypt(t *testing.T) {
api.RegisterCallback(nil, "j9JMGyaZs&vxqt&S", "VFFjTnZrZDJZZmZHJksxeTlxcnp5aG42WmRCbXl3REE", "")
encryptedMsg := "UsgVSPqyJg1aLBmVSbAP19AcIgK/O2UgOfo7lOuwRbSfPDP8XnDUuE+os+nnxsbaMTJ4DSu/twE5aNrZk7RDE9vZYiT/gXqXNvj7y45R32xfOLecVNTvy13wF4vDzuUB"
encryptedMsg := "L+CkLbztRNz104HWXbFJInfrUuleuB7Q/Il1bgkMA4Ovy6OgObmL9o7smUTxzdNMRFrnjPSJ5Hmfzrsn3a1QVAWgRGJhPrc9mQcbb2rtNu9M0x9o+4xGKAWkb18Roqp4"
decryptedMsg, err := api.Decrypt(encryptedMsg)
if err != nil {
t.Fatal(err)

View File

@@ -31,6 +31,7 @@ const (
)
type API struct {
corpID string
token string
appID string
secret string
@@ -42,12 +43,13 @@ type API struct {
callbackAESKey []byte
}
func New(appID, secret string, config ...*platformapi.APIConfig) *API {
func New(corpID, appID, secret string, config ...*platformapi.APIConfig) *API {
curConfig := platformapi.DefAPIConfig
if len(config) > 0 {
curConfig = *config[0]
}
return &API{
corpID: corpID,
appID: appID,
secret: secret,
client: &http.Client{Timeout: curConfig.ClientTimeout},

View File

@@ -18,7 +18,7 @@ func init() {
sugarLogger = logger.Sugar()
baseapi.Init(sugarLogger)
api = New("ding7iu9cptairtcls0c", "LWrZAFeqUfuVv7n_tc8vPpCAx6PT4CwManx2XCVhJOqGsx2L5XCDuX1sAN_JtvsI")
api = New("ding7ab5687f3784a8db", "ding7iu9cptairtcls0c", "LWrZAFeqUfuVv7n_tc8vPpCAx6PT4CwManx2XCVhJOqGsx2L5XCDuX1sAN_JtvsI")
api.RetrieveToken()
}