Skip to content

Commit

Permalink
refactor: change hash function
Browse files Browse the repository at this point in the history
  • Loading branch information
xhofe committed Jul 15, 2023
1 parent 9d1f122 commit a4511c1
Show file tree
Hide file tree
Showing 11 changed files with 25 additions and 21 deletions.
4 changes: 2 additions & 2 deletions drivers/139/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand Down
2 changes: 1 addition & 1 deletion drivers/189/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
2 changes: 1 addition & 1 deletion drivers/189pc/driver.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
4 changes: 2 additions & 2 deletions drivers/189pc/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")))
}

// 提交上传
Expand Down Expand Up @@ -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")))
}

// 检测是否支持快传
Expand Down
6 changes: 3 additions & 3 deletions drivers/aliyundrive/driver.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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 {
Expand Down
2 changes: 1 addition & 1 deletion drivers/local/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
4 changes: 2 additions & 2 deletions drivers/thunder/driver.go
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down Expand Up @@ -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
}(),
Expand Down
2 changes: 1 addition & 1 deletion drivers/thunder/meta.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{
Expand Down
2 changes: 1 addition & 1 deletion drivers/thunder/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion drivers/uss/driver.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
16 changes: 10 additions & 6 deletions pkg/utils/hash.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{
"-": "+",
"_": "/",
Expand Down

0 comments on commit a4511c1

Please sign in to comment.