Skip to content

Commit

Permalink
feat: Hotfix lyl md5 (#12)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
wgliyuli authored Nov 19, 2020
1 parent b5e6f72 commit 44db116
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 33 deletions.
22 changes: 0 additions & 22 deletions aes.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
12 changes: 6 additions & 6 deletions diamond.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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,
}
Expand Down
19 changes: 14 additions & 5 deletions get_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down Expand Up @@ -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
}
Expand All @@ -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
}
Expand All @@ -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
Expand Down

0 comments on commit 44db116

Please sign in to comment.