From 8fb5effbba39c728d6aaaec01fceee1f48ba1fc2 Mon Sep 17 00:00:00 2001 From: what Date: Thu, 28 Nov 2024 10:47:28 +0800 Subject: [PATCH] =?UTF-8?q?[feat]=20=E6=9B=B4=E6=8D=A2=20base62=20?= =?UTF-8?q?=E4=B8=BA=20github.com/jxskiss/base62?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- base62json.go | 31 +++++++++++++++---------------- base62json_test.go | 1 - go.mod | 4 ++-- go.sum | 6 ++---- 4 files changed, 19 insertions(+), 23 deletions(-) diff --git a/base62json.go b/base62json.go index 008bf02..6d955e2 100644 --- a/base62json.go +++ b/base62json.go @@ -1,20 +1,22 @@ package base62json import ( - "errors" + "fmt" + "unsafe" - "git.fsdpf.net/go/base62" "git.fsdpf.net/go/jsonpack" + "github.com/jxskiss/base62" "github.com/samber/lo" ) const cursor = 59 +const encodeStd = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789" -var bs62 *base62.Base62 +var bs62 *base62.Encoding func init() { - bs62 = base62.GetInstance() + bs62 = base62.NewEncoding(encodeStd) } // 项目入口 @@ -22,7 +24,7 @@ func GenerateProjectSecret(key, secret string) ([]byte, error) { appSecret := lo.FindUniques([]byte(secret)) if len(appSecret) != 62 { - return nil, errors.New("Invalid secret") + return nil, fmt.Errorf("Invalid secret") } for i := 0; i < len(key); i++ { @@ -32,12 +34,11 @@ func GenerateProjectSecret(key, secret string) ([]byte, error) { return appSecret, nil } -func SetCharacters(secret []byte) error { - if res, err := bs62.SetCharacters(secret); err == nil { - bs62 = res - } else { - return err +func SetCharacters(secret string) error { + if len(secret) != 64 { + return fmt.Errorf("encoding alphabet is not 62-bytes long") } + bs62 = base62.NewEncoding(secret) return nil } @@ -48,19 +49,17 @@ func Encode[T []any | map[string]any](data T) (string, error) { return "", err } - res := bs62.Encode([]byte(str)) + stringData := unsafe.StringData(str) - return res, nil + return bs62.EncodeToString(unsafe.Slice(stringData, len(str))), nil } func Decode(str string) (string, error) { - json, err := bs62.Decode(str) + json, err := bs62.DecodeString(str) if err != nil { return "", err } - data := string(json) - - return jsonpack.Unpack(data) + return jsonpack.Unpack(*(*string)(unsafe.Pointer(&json))) } diff --git a/base62json_test.go b/base62json_test.go index fba0f2b..1d5ac6c 100644 --- a/base62json_test.go +++ b/base62json_test.go @@ -40,7 +40,6 @@ func TestGenerateProjectSecret(t *testing.T) { // PrV4uZRqFHWOjkYIo07J1wdU6cDyXzh5Asi2xb3QMCNSmEvLl8pgetBGfnT9aK 数据库 // PrV4uZRqFHWOjkYIo07J1wdU6cDyXzh5Asi2xb3QMCNSmEvLl8pgetBGfnT9aK 加密后 - // PrV4uZReFpWOjkYIoNCJ1wdU6cDyXzh5Asi2xb3QM70SmEvLl8HgqtBGfnT9aK res, _ := GenerateProjectSecret("demo", "PrV4uZReFpWOjkYIoNCJ1wdU6cDyXzh5Asi2xb3QM70SmEvLl8HgqtBGfnT9aK") t.Log(string(res)) diff --git a/go.mod b/go.mod index 939d43d..0e05abc 100644 --- a/go.mod +++ b/go.mod @@ -1,10 +1,10 @@ module git.fsdpf.net/go/base62json -go 1.18 +go 1.20 require ( - git.fsdpf.net/go/base62 v0.0.0-20241126072008-66ea462704b7 git.fsdpf.net/go/jsonpack v0.0.0-20230414160652-34bc26cb4839 + github.com/jxskiss/base62 v1.1.0 github.com/samber/lo v1.38.1 ) diff --git a/go.sum b/go.sum index e38374e..0e6a147 100644 --- a/go.sum +++ b/go.sum @@ -1,9 +1,7 @@ -git.fsdpf.net/go/base62 v0.0.0-20230414160450-79c590957c64 h1:wpAuCgw7/+EFEnodjtg4YLrLHmFwA+zX1V1UrwbfcK0= -git.fsdpf.net/go/base62 v0.0.0-20230414160450-79c590957c64/go.mod h1:DWMg8qh5X/0I07jjSWXHl5aPgnGjdV5SijniomRzmXA= -git.fsdpf.net/go/base62 v0.0.0-20241126072008-66ea462704b7 h1:JzR6K9xz8y4I7ZZuz8kwFS36QgC/cGXzxgPbaba2Dxo= -git.fsdpf.net/go/base62 v0.0.0-20241126072008-66ea462704b7/go.mod h1:DWMg8qh5X/0I07jjSWXHl5aPgnGjdV5SijniomRzmXA= git.fsdpf.net/go/jsonpack v0.0.0-20230414160652-34bc26cb4839 h1:V6AOjmXFikY6yzGm9JUTo9v9Y5oc3au1J62aMXfGSFM= git.fsdpf.net/go/jsonpack v0.0.0-20230414160652-34bc26cb4839/go.mod h1:vRbZ2FL+dDcUN9di0ONHm2gWu3NRl4mjoOhiu04ZOMs= +github.com/jxskiss/base62 v1.1.0 h1:A5zbF8v8WXx2xixnAKD2w+abC+sIzYJX+nxmhA6HWFw= +github.com/jxskiss/base62 v1.1.0/go.mod h1:HhWAlUXvxKThfOlZbcuFzsqwtF5TcqS9ru3y5GfjWAc= github.com/samber/lo v1.38.1 h1:j2XEAqXKb09Am4ebOg31SpvzUTTs6EN3VfgeLUhPdXM= github.com/samber/lo v1.38.1/go.mod h1:+m/ZKRl6ClXCE2Lgf3MsQlWfh4bn1bz6CXEOxnEXnEA= golang.org/x/exp v0.0.0-20220303212507-bbda1eaf7a17 h1:3MTrJm4PyNL9NBqvYDSj3DHl46qQakyfqfWo4jgfaEM=