Skip to content

Commit

Permalink
perf: perfect crypto util
Browse files Browse the repository at this point in the history
  • Loading branch information
starudream committed Sep 9, 2024
1 parent 71201db commit 637a31b
Show file tree
Hide file tree
Showing 4 changed files with 7 additions and 30 deletions.
5 changes: 1 addition & 4 deletions pkg/douyu/ws/client.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
package ws

import (
"crypto/md5"
"crypto/tls"
"encoding/hex"
"fmt"
"net/http"
"strconv"
Expand Down Expand Up @@ -82,8 +80,7 @@ func Login(p LoginParams) error {
func (c *Client) login(p LoginParams) error {
devId := util.UUID()
rt := strconv.FormatInt(time.Now().Unix(), 10)
b := md5.Sum([]byte(rt + hash + devId))
vk := hex.EncodeToString(b[:])
vk := util.MD5Hex(rt + hash + devId)

c.write(
"type", "loginreq",
Expand Down
8 changes: 2 additions & 6 deletions pkg/miyoushe/api/ds.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
package api

import (
"crypto/md5"
"encoding/hex"
"fmt"
"math/rand/v2"
"time"
Expand Down Expand Up @@ -35,8 +33,7 @@ func DS1() string {

func ds1(t int64, r string) (string, string) {
s := fmt.Sprintf("salt=%s&t=%d&r=%s", AppSaltK2, t, r)
b := md5.Sum([]byte(s))
return s, fmt.Sprintf("%d,%s,%s", t, r, hex.EncodeToString(b[:]))
return s, fmt.Sprintf("%d,%s,%s", t, r, util.MD5Hex(s))
}

// DS2 https://github.com/UIGF-org/mihoyo-api-collect/blob/3a9116ea538941cfead749572df1f364cb9f9c8d/other/authentication.md#ds
Expand All @@ -50,6 +47,5 @@ func ds2(t int64, r int, body, query string) (string, string) {
r += 542367
}
s := fmt.Sprintf("salt=%s&t=%d&r=%d&b=%s&q=%s", AppSalt6X, t, r, body, query)
b := md5.Sum([]byte(s))
return s, fmt.Sprintf("%d,%d,%s", t, r, hex.EncodeToString(b[:]))
return s, fmt.Sprintf("%d,%d,%s", t, r, util.MD5Hex(s))
}
17 changes: 1 addition & 16 deletions pkg/skland/api/sign.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,6 @@
package api

import (
"crypto/hmac"
"crypto/md5"
"crypto/sha256"
"encoding/hex"
"net/url"
"strconv"
"time"
Expand Down Expand Up @@ -45,16 +41,5 @@ func sign(headers signHeaders, method, path, token string, query url.Values, bod

content := path + str + headers.Timestamp + json.MustMarshalString(headers)

b1 := hmac256(token, content)
s1 := hex.EncodeToString(b1)
b2 := md5.Sum([]byte(s1))
s2 := hex.EncodeToString(b2[:])

return content, s2
}

func hmac256(key, content string) []byte {
h := hmac.New(sha256.New, []byte(key))
h.Write([]byte(content))
return h.Sum(nil)
return content, util.MD5Hex(util.HMAC256Hex(token, content))
}
7 changes: 3 additions & 4 deletions pkg/tieba/api/sign.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@ package api

import (
"bytes"
"crypto/md5"
"encoding/hex"
"slices"
"strings"

"github.com/starudream/go-lib/resty/v2"

"github.com/starudream/sign-task/util"
)

func addSign(r *resty.Request) *resty.Request {
Expand All @@ -29,7 +29,6 @@ func addSign(r *resty.Request) *resty.Request {
}
buf.WriteString("tiebaclient!!!")

b := md5.Sum(buf.Bytes())
r.FormData.Set("sign", strings.ToUpper(hex.EncodeToString(b[:])))
r.FormData.Set("sign", strings.ToUpper(util.MD5Hex(buf.String())))
return r
}

0 comments on commit 637a31b

Please sign in to comment.