refactor: change hash function

pull/4798/head
Andy Hsu 2023-07-15 16:28:55 +08:00
parent 9d1f122717
commit a4511c1963
11 changed files with 25 additions and 21 deletions

View File

@ -42,8 +42,8 @@ func calSign(body, ts, randStr string) string {
sort.Strings(strs) sort.Strings(strs)
body = strings.Join(strs, "") body = strings.Join(strs, "")
body = base64.StdEncoding.EncodeToString([]byte(body)) body = base64.StdEncoding.EncodeToString([]byte(body))
res := utils.GetMD5Encode(body) + utils.GetMD5Encode(ts+":"+randStr) res := utils.GetMD5EncodeStr(body) + utils.GetMD5EncodeStr(ts+":"+randStr)
res = strings.ToUpper(utils.GetMD5Encode(res)) res = strings.ToUpper(utils.GetMD5EncodeStr(res))
return res return res
} }

View File

@ -385,7 +385,7 @@ func (d *Cloud189) newUpload(ctx context.Context, dstDir model.Obj, file model.F
fileMd5 := hex.EncodeToString(md5Sum.Sum(nil)) fileMd5 := hex.EncodeToString(md5Sum.Sum(nil))
sliceMd5 := fileMd5 sliceMd5 := fileMd5
if file.GetSize() > DEFAULT { 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{ res, err = d.uploadRequest("/person/commitMultiUploadFile", map[string]string{
"uploadFileId": uploadFileId, "uploadFileId": uploadFileId,

View File

@ -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 { if !y.isLogin() || y.identity != identity {
y.identity = identity y.identity = identity
if err = y.login(); err != nil { if err = y.login(); err != nil {

View File

@ -519,7 +519,7 @@ func (y *Cloud189PC) CommonUpload(ctx context.Context, dstDir model.Obj, file mo
fileMd5Hex := strings.ToUpper(hex.EncodeToString(fileMd5.Sum(nil))) fileMd5Hex := strings.ToUpper(hex.EncodeToString(fileMd5.Sum(nil)))
sliceMd5Hex := fileMd5Hex sliceMd5Hex := fileMd5Hex
if file.GetSize() > DEFAULT { 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))) fileMd5Hex := strings.ToUpper(hex.EncodeToString(fileMd5.Sum(nil)))
sliceMd5Hex := fileMd5Hex sliceMd5Hex := fileMd5Hex
if file.GetSize() > DEFAULT { if file.GetSize() > DEFAULT {
sliceMd5Hex = strings.ToUpper(utils.GetMD5Encode(strings.Join(silceMd5Hexs, "\n"))) sliceMd5Hex = strings.ToUpper(utils.GetMD5EncodeStr(strings.Join(silceMd5Hexs, "\n")))
} }
// 检测是否支持快传 // 检测是否支持快传

View File

@ -67,7 +67,7 @@ func (d *AliDrive) Init(ctx context.Context) error {
return nil return nil
} }
// init deviceID // init deviceID
deviceID := utils.GetSHA256Encode(d.UserID) deviceID := utils.GetSHA256Encode([]byte(d.UserID))
// init privateKey // init privateKey
privateKey, _ := NewPrivateKeyFromHex(deviceID) privateKey, _ := NewPrivateKeyFromHex(deviceID)
state := State{ state := State{
@ -193,7 +193,7 @@ func (d *AliDrive) Put(ctx context.Context, dstDir model.Obj, stream model.FileS
if d.RapidUpload { if d.RapidUpload {
buf := bytes.NewBuffer(make([]byte, 0, 1024)) buf := bytes.NewBuffer(make([]byte, 0, 1024))
io.CopyN(buf, file, 1024) io.CopyN(buf, file, 1024)
reqBody["pre_hash"] = utils.GetSHA1Encode(buf.String()) reqBody["pre_hash"] = utils.GetSHA1Encode(buf.Bytes())
if localFile != nil { if localFile != nil {
if _, err := localFile.Seek(0, io.SeekStart); err != nil { if _, err := localFile.Seek(0, io.SeekStart); err != nil {
return err 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))) (t.file.slice(o.toNumber(), Math.min(o.plus(8).toNumber(), t.file.size)))
*/ */
buf := make([]byte, 8) 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()) i := new(big.Int).SetInt64(file.GetSize())
o := new(big.Int).SetInt64(0) o := new(big.Int).SetInt64(0)
if file.GetSize() > 0 { if file.GetSize() > 0 {

View File

@ -64,7 +64,7 @@ func readDir(dirname string) ([]fs.FileInfo, error) {
func (d *Local) getThumb(file model.Obj) (*bytes.Buffer, *string, error) { func (d *Local) getThumb(file model.Obj) (*bytes.Buffer, *string, error) {
fullPath := file.GetPath() fullPath := file.GetPath()
thumbPrefix := "alist_thumb_" thumbPrefix := "alist_thumb_"
thumbName := thumbPrefix + utils.GetMD5Encode(fullPath) + ".png" thumbName := thumbPrefix + utils.GetMD5EncodeStr(fullPath) + ".png"
if d.ThumbCacheFolder != "" { if d.ThumbCacheFolder != "" {
// skip if the file is a thumbnail // skip if the file is a thumbnail
if strings.HasPrefix(file.GetName(), thumbPrefix) { if strings.HasPrefix(file.GetName(), thumbPrefix) {

View File

@ -56,7 +56,7 @@ func (x *Thunder) Init(ctx context.Context) (err error) {
"j", "j",
"4scKJNdd7F27Hv7tbt", "4scKJNdd7F27Hv7tbt",
}, },
DeviceID: utils.GetMD5Encode(x.Username + x.Password), DeviceID: utils.GetMD5EncodeStr(x.Username + x.Password),
ClientID: "Xp6vsxz_7IYVw2BB", ClientID: "Xp6vsxz_7IYVw2BB",
ClientSecret: "Xp6vsy4tN9toTVdMSpomVdXpRmES", ClientSecret: "Xp6vsy4tN9toTVdMSpomVdXpRmES",
ClientVersion: "7.51.0.8196", ClientVersion: "7.51.0.8196",
@ -137,7 +137,7 @@ func (x *ThunderExpert) Init(ctx context.Context) (err error) {
DeviceID: func() string { DeviceID: func() string {
if len(x.DeviceID) != 32 { if len(x.DeviceID) != 32 {
return utils.GetMD5Encode(x.DeviceID) return utils.GetMD5EncodeStr(x.DeviceID)
} }
return x.DeviceID return x.DeviceID
}(), }(),

View File

@ -78,7 +78,7 @@ type Addition struct {
// 登录特征,用于判断是否重新登录 // 登录特征,用于判断是否重新登录
func (i *Addition) GetIdentity() string { func (i *Addition) GetIdentity() string {
return utils.GetMD5Encode(i.Username + i.Password) return utils.GetMD5EncodeStr(i.Username + i.Password)
} }
var config = driver.Config{ var config = driver.Config{

View File

@ -100,7 +100,7 @@ func (c *Common) GetCaptchaSign() (timestamp, sign string) {
timestamp = fmt.Sprint(time.Now().UnixMilli()) timestamp = fmt.Sprint(time.Now().UnixMilli())
str := fmt.Sprint(c.ClientID, c.ClientVersion, c.PackageName, c.DeviceID, timestamp) str := fmt.Sprint(c.ClientID, c.ClientVersion, c.PackageName, c.DeviceID, timestamp)
for _, algorithm := range c.Algorithms { for _, algorithm := range c.Algorithms {
str = utils.GetMD5Encode(str + algorithm) str = utils.GetMD5EncodeStr(str + algorithm)
} }
sign = "1." + str sign = "1." + str
return return

View File

@ -81,7 +81,7 @@ func (d *USS) Link(ctx context.Context, file model.Obj, args model.LinkArgs) (*m
expireAt := time.Now().Add(downExp).Unix() expireAt := time.Now().Add(downExp).Unix()
upd := url.QueryEscape(path.Base(file.GetPath())) upd := url.QueryEscape(path.Base(file.GetPath()))
signStr := strings.Join([]string{d.OperatorPassword, fmt.Sprint(expireAt), fmt.Sprintf("/%s", key)}, "&") 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) link := fmt.Sprintf("%s?_upd=%s&_upt=%s", u, upd, upt)
return &model.Link{URL: link}, nil return &model.Link{URL: link}, nil
} }

View File

@ -9,24 +9,28 @@ import (
"strings" "strings"
) )
func GetSHA1Encode(data string) string { func GetSHA1Encode(data []byte) string {
h := sha1.New() h := sha1.New()
h.Write([]byte(data)) h.Write(data)
return hex.EncodeToString(h.Sum(nil)) return hex.EncodeToString(h.Sum(nil))
} }
func GetSHA256Encode(data string) string { func GetSHA256Encode(data []byte) string {
h := sha256.New() h := sha256.New()
h.Write([]byte(data)) h.Write(data)
return hex.EncodeToString(h.Sum(nil)) return hex.EncodeToString(h.Sum(nil))
} }
func GetMD5Encode(data string) string { func GetMD5Encode(data []byte) string {
h := md5.New() h := md5.New()
h.Write([]byte(data)) h.Write(data)
return hex.EncodeToString(h.Sum(nil)) return hex.EncodeToString(h.Sum(nil))
} }
func GetMD5EncodeStr(data string) string {
return GetMD5Encode([]byte(data))
}
var DEC = map[string]string{ var DEC = map[string]string{
"-": "+", "-": "+",
"_": "/", "_": "/",