From 44db1161111f3d4adfca6816bef5bd37603736cc Mon Sep 17 00:00:00 2001 From: Lincoln_Lee <138951227@qq.com> Date: Thu, 19 Nov 2020 21:02:12 +0800 Subject: [PATCH] feat: Hotfix lyl md5 (#12) * feat: add kms * feat: optimize code * feat: upgrade README.md * feat: optimize kms option, replace str2byte * feat: run code * feat: update comment/parameter * fix: long pull error --- aes.go | 22 ---------------------- diamond.go | 12 ++++++------ get_config.go | 19 ++++++++++++++----- 3 files changed, 20 insertions(+), 33 deletions(-) diff --git a/aes.go b/aes.go index 5b03eec..c0e0bb5 100644 --- a/aes.go +++ b/aes.go @@ -17,28 +17,6 @@ func newECB(b cipher.Block) *ecb { } } -type ecbEncrypter ecb - -// newECBEncrypter returns a BlockMode which encrypts in electronic code book -// mode, using the given Block. -func newECBEncrypter(b cipher.Block) cipher.BlockMode { - return (*ecbEncrypter)(newECB(b)) -} -func (x *ecbEncrypter) BlockSize() int { return x.blockSize } -func (x *ecbEncrypter) CryptBlocks(dst, src []byte) { - if len(src)%x.blockSize != 0 { - panic("crypto/cipher: input not full blocks") - } - if len(dst) < len(src) { - panic("crypto/cipher: output smaller than input") - } - for len(src) > 0 { - x.b.Encrypt(dst, src[:x.blockSize]) - src = src[x.blockSize:] - dst = dst[x.blockSize:] - } -} - type ecbDecrypter ecb // newECBDecrypter returns a BlockMode which decrypts in electronic code book diff --git a/diamond.go b/diamond.go index ce33481..d68714c 100644 --- a/diamond.go +++ b/diamond.go @@ -169,14 +169,14 @@ func (d *Diamond) Register(oo ...*observer.Observer) { Group: i.Group, DataID: i.DataID, } - b, err := d.GetConfig(req) + c, err := d.GetConfig(req) if err != nil { d.checkErr(fmt.Errorf("DataID: %s, Group: %s, err: %+v", i.DataID, i.Group, err)) continue } conf = &config.Config{ - Content: b, - ContentMD5: Md5(string(b)), + Content: c.DecryptContent, + ContentMD5: Md5(string(c.Content)), Pulled: true, } d.all.Store(i, conf) @@ -283,19 +283,19 @@ func (d *Diamond) update(ctx context.Context, arg interface{}) error { DataID: i.DataID, Group: i.Group, } - b, err := d.GetConfig(req) + c, err := d.GetConfig(req) if err != nil { d.checkErr(fmt.Errorf("DataID: %s, Group: %s, err: %+v", i.DataID, i.Group, err)) return nil } - newContentMD5 := Md5(string(b)) + newContentMD5 := Md5(string(c.Content)) if preConf.ContentMD5 == newContentMD5 { return nil } conf := &config.Config{ - Content: b, + Content: c.DecryptContent, ContentMD5: newContentMD5, Pulled: true, } diff --git a/get_config.go b/get_config.go index d46a7f3..0b6bcf4 100644 --- a/get_config.go +++ b/get_config.go @@ -18,8 +18,13 @@ type GetConfigRequest struct { Group string `url:"group"` } +type GetConfigResponse struct { + Content []byte + DecryptContent []byte +} + // GetConfig 获取配置 -func (d *Diamond) GetConfig(args *GetConfigRequest) ([]byte, error) { +func (d *Diamond) GetConfig(args *GetConfigRequest) (*GetConfigResponse, error) { if len(args.Group) == 0 { args.Group = DefaultGroup } @@ -57,8 +62,11 @@ func (d *Diamond) GetConfig(args *GetConfigRequest) ([]byte, error) { } // getConfig 适配配置kms加密 -func (d *Diamond) getConfig(response *cast.Response, dataID string) ([]byte, error) { - config := response.Body() +func (d *Diamond) getConfig(response *cast.Response, dataID string) (*GetConfigResponse, error) { + config := &GetConfigResponse{ + Content: response.Body(), + DecryptContent: response.Body(), + } if d.kmsClient == nil { return config, nil } @@ -80,7 +88,7 @@ func (d *Diamond) getConfig(response *cast.Response, dataID string) ([]byte, err return nil, err } - config, err = aesDecrypt(bodyByte, dataKeyByte) + config.DecryptContent, err = aesDecrypt(bodyByte, dataKeyByte) if err != nil { return nil, err } @@ -90,7 +98,8 @@ func (d *Diamond) getConfig(response *cast.Response, dataID string) ([]byte, err if err != nil { return nil, err } - config = convert.StringToByte(configStr) + config.DecryptContent = convert.StringToByte(configStr) + } return config, nil