From a4511c1963bac2bd4728eeaf5cd6f097d32d9825 Mon Sep 17 00:00:00 2001 From: Andy Hsu Date: Sat, 15 Jul 2023 16:28:55 +0800 Subject: [PATCH] refactor: change hash function --- drivers/139/util.go | 4 ++-- drivers/189/util.go | 2 +- drivers/189pc/driver.go | 2 +- drivers/189pc/utils.go | 4 ++-- drivers/aliyundrive/driver.go | 6 +++--- drivers/local/util.go | 2 +- drivers/thunder/driver.go | 4 ++-- drivers/thunder/meta.go | 2 +- drivers/thunder/util.go | 2 +- drivers/uss/driver.go | 2 +- pkg/utils/hash.go | 16 ++++++++++------ 11 files changed, 25 insertions(+), 21 deletions(-) diff --git a/drivers/139/util.go b/drivers/139/util.go index 1ff53d6d261..1c9574db481 100644 --- a/drivers/139/util.go +++ b/drivers/139/util.go @@ -42,8 +42,8 @@ func calSign(body, ts, randStr string) string { sort.Strings(strs) body = strings.Join(strs, "") body = base64.StdEncoding.EncodeToString([]byte(body)) - res := utils.GetMD5Encode(body) + utils.GetMD5Encode(ts+":"+randStr) - res = strings.ToUpper(utils.GetMD5Encode(res)) + res := utils.GetMD5EncodeStr(body) + utils.GetMD5EncodeStr(ts+":"+randStr) + res = strings.ToUpper(utils.GetMD5EncodeStr(res)) return res } diff --git a/drivers/189/util.go b/drivers/189/util.go index 4ae1e9dfaa0..680ce252133 100644 --- a/drivers/189/util.go +++ b/drivers/189/util.go @@ -385,7 +385,7 @@ func (d *Cloud189) newUpload(ctx context.Context, dstDir model.Obj, file model.F fileMd5 := hex.EncodeToString(md5Sum.Sum(nil)) sliceMd5 := fileMd5 if file.GetSize() > DEFAULT { - sliceMd5 = utils.GetMD5Encode(strings.Join(md5s, "\n")) + sliceMd5 = utils.GetMD5EncodeStr(strings.Join(md5s, "\n")) } res, err = d.uploadRequest("/person/commitMultiUploadFile", map[string]string{ "uploadFileId": uploadFileId, diff --git a/drivers/189pc/driver.go b/drivers/189pc/driver.go index fe4cc3e1147..a11d3c1f8e9 100644 --- a/drivers/189pc/driver.go +++ b/drivers/189pc/driver.go @@ -51,7 +51,7 @@ func (y *Cloud189PC) Init(ctx context.Context) (err error) { } // 避免重复登陆 - identity := utils.GetMD5Encode(y.Username + y.Password) + identity := utils.GetMD5EncodeStr(y.Username + y.Password) if !y.isLogin() || y.identity != identity { y.identity = identity if err = y.login(); err != nil { diff --git a/drivers/189pc/utils.go b/drivers/189pc/utils.go index af1f1505ae8..0e01a298a80 100644 --- a/drivers/189pc/utils.go +++ b/drivers/189pc/utils.go @@ -519,7 +519,7 @@ func (y *Cloud189PC) CommonUpload(ctx context.Context, dstDir model.Obj, file mo fileMd5Hex := strings.ToUpper(hex.EncodeToString(fileMd5.Sum(nil))) sliceMd5Hex := fileMd5Hex if file.GetSize() > DEFAULT { - sliceMd5Hex = strings.ToUpper(utils.GetMD5Encode(strings.Join(silceMd5Hexs, "\n"))) + sliceMd5Hex = strings.ToUpper(utils.GetMD5EncodeStr(strings.Join(silceMd5Hexs, "\n"))) } // 提交上传 @@ -577,7 +577,7 @@ func (y *Cloud189PC) FastUpload(ctx context.Context, dstDir model.Obj, file mode fileMd5Hex := strings.ToUpper(hex.EncodeToString(fileMd5.Sum(nil))) sliceMd5Hex := fileMd5Hex if file.GetSize() > DEFAULT { - sliceMd5Hex = strings.ToUpper(utils.GetMD5Encode(strings.Join(silceMd5Hexs, "\n"))) + sliceMd5Hex = strings.ToUpper(utils.GetMD5EncodeStr(strings.Join(silceMd5Hexs, "\n"))) } // 检测是否支持快传 diff --git a/drivers/aliyundrive/driver.go b/drivers/aliyundrive/driver.go index b33f7e80335..ce8487aec5c 100644 --- a/drivers/aliyundrive/driver.go +++ b/drivers/aliyundrive/driver.go @@ -67,7 +67,7 @@ func (d *AliDrive) Init(ctx context.Context) error { return nil } // init deviceID - deviceID := utils.GetSHA256Encode(d.UserID) + deviceID := utils.GetSHA256Encode([]byte(d.UserID)) // init privateKey privateKey, _ := NewPrivateKeyFromHex(deviceID) state := State{ @@ -193,7 +193,7 @@ func (d *AliDrive) Put(ctx context.Context, dstDir model.Obj, stream model.FileS if d.RapidUpload { buf := bytes.NewBuffer(make([]byte, 0, 1024)) io.CopyN(buf, file, 1024) - reqBody["pre_hash"] = utils.GetSHA1Encode(buf.String()) + reqBody["pre_hash"] = utils.GetSHA1Encode(buf.Bytes()) if localFile != nil { if _, err := localFile.Seek(0, io.SeekStart); err != nil { return err @@ -259,7 +259,7 @@ func (d *AliDrive) Put(ctx context.Context, dstDir model.Obj, stream model.FileS (t.file.slice(o.toNumber(), Math.min(o.plus(8).toNumber(), t.file.size))) */ buf := make([]byte, 8) - r, _ := new(big.Int).SetString(utils.GetMD5Encode(d.AccessToken)[:16], 16) + r, _ := new(big.Int).SetString(utils.GetMD5EncodeStr(d.AccessToken)[:16], 16) i := new(big.Int).SetInt64(file.GetSize()) o := new(big.Int).SetInt64(0) if file.GetSize() > 0 { diff --git a/drivers/local/util.go b/drivers/local/util.go index ef3a2e4222a..84f1822bf24 100644 --- a/drivers/local/util.go +++ b/drivers/local/util.go @@ -64,7 +64,7 @@ func readDir(dirname string) ([]fs.FileInfo, error) { func (d *Local) getThumb(file model.Obj) (*bytes.Buffer, *string, error) { fullPath := file.GetPath() thumbPrefix := "alist_thumb_" - thumbName := thumbPrefix + utils.GetMD5Encode(fullPath) + ".png" + thumbName := thumbPrefix + utils.GetMD5EncodeStr(fullPath) + ".png" if d.ThumbCacheFolder != "" { // skip if the file is a thumbnail if strings.HasPrefix(file.GetName(), thumbPrefix) { diff --git a/drivers/thunder/driver.go b/drivers/thunder/driver.go index 30025b591b1..d933753527a 100644 --- a/drivers/thunder/driver.go +++ b/drivers/thunder/driver.go @@ -56,7 +56,7 @@ func (x *Thunder) Init(ctx context.Context) (err error) { "j", "4scKJNdd7F27Hv7tbt", }, - DeviceID: utils.GetMD5Encode(x.Username + x.Password), + DeviceID: utils.GetMD5EncodeStr(x.Username + x.Password), ClientID: "Xp6vsxz_7IYVw2BB", ClientSecret: "Xp6vsy4tN9toTVdMSpomVdXpRmES", ClientVersion: "7.51.0.8196", @@ -137,7 +137,7 @@ func (x *ThunderExpert) Init(ctx context.Context) (err error) { DeviceID: func() string { if len(x.DeviceID) != 32 { - return utils.GetMD5Encode(x.DeviceID) + return utils.GetMD5EncodeStr(x.DeviceID) } return x.DeviceID }(), diff --git a/drivers/thunder/meta.go b/drivers/thunder/meta.go index 3696637b957..12b01cbaa16 100644 --- a/drivers/thunder/meta.go +++ b/drivers/thunder/meta.go @@ -78,7 +78,7 @@ type Addition struct { // 登录特征,用于判断是否重新登录 func (i *Addition) GetIdentity() string { - return utils.GetMD5Encode(i.Username + i.Password) + return utils.GetMD5EncodeStr(i.Username + i.Password) } var config = driver.Config{ diff --git a/drivers/thunder/util.go b/drivers/thunder/util.go index fe4c3c979cf..f6dec3260cf 100644 --- a/drivers/thunder/util.go +++ b/drivers/thunder/util.go @@ -100,7 +100,7 @@ func (c *Common) GetCaptchaSign() (timestamp, sign string) { timestamp = fmt.Sprint(time.Now().UnixMilli()) str := fmt.Sprint(c.ClientID, c.ClientVersion, c.PackageName, c.DeviceID, timestamp) for _, algorithm := range c.Algorithms { - str = utils.GetMD5Encode(str + algorithm) + str = utils.GetMD5EncodeStr(str + algorithm) } sign = "1." + str return diff --git a/drivers/uss/driver.go b/drivers/uss/driver.go index 95ed3956cef..2ca19c0a7cb 100644 --- a/drivers/uss/driver.go +++ b/drivers/uss/driver.go @@ -81,7 +81,7 @@ func (d *USS) Link(ctx context.Context, file model.Obj, args model.LinkArgs) (*m expireAt := time.Now().Add(downExp).Unix() upd := url.QueryEscape(path.Base(file.GetPath())) signStr := strings.Join([]string{d.OperatorPassword, fmt.Sprint(expireAt), fmt.Sprintf("/%s", key)}, "&") - upt := utils.GetMD5Encode(signStr)[12:20] + fmt.Sprint(expireAt) + upt := utils.GetMD5EncodeStr(signStr)[12:20] + fmt.Sprint(expireAt) link := fmt.Sprintf("%s?_upd=%s&_upt=%s", u, upd, upt) return &model.Link{URL: link}, nil } diff --git a/pkg/utils/hash.go b/pkg/utils/hash.go index 54e4de4dae2..bf161a4ceb7 100644 --- a/pkg/utils/hash.go +++ b/pkg/utils/hash.go @@ -9,24 +9,28 @@ import ( "strings" ) -func GetSHA1Encode(data string) string { +func GetSHA1Encode(data []byte) string { h := sha1.New() - h.Write([]byte(data)) + h.Write(data) return hex.EncodeToString(h.Sum(nil)) } -func GetSHA256Encode(data string) string { +func GetSHA256Encode(data []byte) string { h := sha256.New() - h.Write([]byte(data)) + h.Write(data) return hex.EncodeToString(h.Sum(nil)) } -func GetMD5Encode(data string) string { +func GetMD5Encode(data []byte) string { h := md5.New() - h.Write([]byte(data)) + h.Write(data) return hex.EncodeToString(h.Sum(nil)) } +func GetMD5EncodeStr(data string) string { + return GetMD5Encode([]byte(data)) +} + var DEC = map[string]string{ "-": "+", "_": "/",