From f33123fcf6f7b16c99b8f39618b8b804e9a22e52 Mon Sep 17 00:00:00 2001 From: wdvxdr Date: Mon, 6 Feb 2023 20:28:03 +0800 Subject: [PATCH] all: bump go 1.20 --- .github/workflows/go.yml | 2 +- go.mod | 2 +- internal/crypto/crypto.go | 15 +++++++++++++++ internal/crypto/ecdh_119.go | 19 ------------------- internal/crypto/ecdh_120.go | 21 --------------------- utils/string.go | 12 ++---------- 6 files changed, 19 insertions(+), 52 deletions(-) delete mode 100644 internal/crypto/ecdh_119.go delete mode 100644 internal/crypto/ecdh_120.go diff --git a/.github/workflows/go.yml b/.github/workflows/go.yml index c5de0468..d75732b5 100644 --- a/.github/workflows/go.yml +++ b/.github/workflows/go.yml @@ -16,7 +16,7 @@ jobs: - name: Set up Go 1.x uses: actions/setup-go@v2 with: - go-version: 1.19 + go-version: 1.20 - name: Check out code into the Go module directory uses: actions/checkout@v2 diff --git a/go.mod b/go.mod index 593cf0f3..e27154bc 100644 --- a/go.mod +++ b/go.mod @@ -1,6 +1,6 @@ module github.com/Mrs4s/MiraiGo -go 1.19 +go 1.20 require ( github.com/RomiChan/protobuf v0.1.1-0.20230204044148-2ed269a2e54d diff --git a/internal/crypto/crypto.go b/internal/crypto/crypto.go index a72d8418..4ce94ed0 100644 --- a/internal/crypto/crypto.go +++ b/internal/crypto/crypto.go @@ -1,6 +1,9 @@ package crypto import ( + "crypto/ecdh" + "crypto/md5" + "crypto/rand" "encoding/hex" "encoding/json" "net/http" @@ -51,3 +54,15 @@ func (e *ECDH) FetchPubKey(uin int64) { key, _ := hex.DecodeString(pubKey.Meta.PubKey) e.init(key) // todo check key sign } + +func (e *ECDH) init(svrPubKey []byte) { + p256 := ecdh.P256() + local, _ := p256.GenerateKey(rand.Reader) + remote, _ := p256.NewPublicKey(svrPubKey) + share, _ := local.ECDH(remote) + + hash := md5.New() + hash.Write(share[:16]) + e.ShareKey = hash.Sum(nil) + e.PublicKey = local.PublicKey().Bytes() +} diff --git a/internal/crypto/ecdh_119.go b/internal/crypto/ecdh_119.go deleted file mode 100644 index fbbde39f..00000000 --- a/internal/crypto/ecdh_119.go +++ /dev/null @@ -1,19 +0,0 @@ -//go:build !go1.20 - -package crypto - -import ( - "crypto/elliptic" - "crypto/md5" - "crypto/rand" -) - -func (e *ECDH) init(svrPubKey []byte) { - p256 := elliptic.P256() - key, sx, sy, _ := elliptic.GenerateKey(p256, rand.Reader) - tx, ty := elliptic.Unmarshal(p256, svrPubKey) - x, _ := p256.ScalarMult(tx, ty, key) - hash := md5.Sum(x.Bytes()[:16]) - e.ShareKey = hash[:] - e.PublicKey = elliptic.Marshal(p256, sx, sy) -} diff --git a/internal/crypto/ecdh_120.go b/internal/crypto/ecdh_120.go deleted file mode 100644 index c563cbd3..00000000 --- a/internal/crypto/ecdh_120.go +++ /dev/null @@ -1,21 +0,0 @@ -//go:build go1.20 - -package crypto - -import ( - "crypto/ecdh" - "crypto/md5" - "crypto/rand" -) - -func (e *ECDH) init(svrPubKey []byte) { - p256 := ecdh.P256() - local, _ := p256.GenerateKey(rand.Reader) - remote, _ := p256.NewPublicKey(svrPubKey) - share, _ := local.ECDH(remote) - - hash := md5.New() - hash.Write(share[:16]) - e.ShareKey = hash.Sum(nil) - e.PublicKey = local.PublicKey().Bytes() -} diff --git a/utils/string.go b/utils/string.go index aa8b236d..8b091d8d 100644 --- a/utils/string.go +++ b/utils/string.go @@ -2,7 +2,6 @@ package utils import ( "math/rand" - "reflect" "strconv" "strings" "unicode/utf8" @@ -50,10 +49,8 @@ func ConvertSubVersionToInt(str string) int32 { } // B2S converts byte slice to a string without memory allocation. -// See https://groups.google.com/forum/#!msg/Golang-Nuts/ENgbUzYvCuU/90yGx7GUAgAJ . -// from github.com/savsgio/gotils/strconv func B2S(b []byte) string { - return *(*string)(unsafe.Pointer(&b)) + return unsafe.String(&b[0], len(b)) } // S2B converts string to a byte slice without memory allocation. @@ -61,12 +58,7 @@ func B2S(b []byte) string { // Note it may break if string and/or slice header will change // in the future go versions. func S2B(s string) (b []byte) { - sh := (*reflect.StringHeader)(unsafe.Pointer(&s)) - bh := (*reflect.SliceHeader)(unsafe.Pointer(&b)) - bh.Data = sh.Data - bh.Cap = sh.Len - bh.Len = sh.Len - return + return unsafe.Slice(unsafe.StringData(s), len(s)) } const (