[feat] 更换 base62 为 github.com/jxskiss/base62

This commit is contained in:
what 2024-11-28 10:47:28 +08:00
parent 9a695e31d4
commit 8fb5effbba
4 changed files with 19 additions and 23 deletions

View File

@ -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)))
}

View File

@ -40,7 +40,6 @@ func TestGenerateProjectSecret(t *testing.T) {
// PrV4uZRqFHWOjkYIo07J1wdU6cDyXzh5Asi2xb3QMCNSmEvLl8pgetBGfnT9aK 数据库
// PrV4uZRqFHWOjkYIo07J1wdU6cDyXzh5Asi2xb3QMCNSmEvLl8pgetBGfnT9aK 加密后
// PrV4uZReFpWOjkYIoNCJ1wdU6cDyXzh5Asi2xb3QM70SmEvLl8HgqtBGfnT9aK
res, _ := GenerateProjectSecret("demo", "PrV4uZReFpWOjkYIoNCJ1wdU6cDyXzh5Asi2xb3QM70SmEvLl8HgqtBGfnT9aK")
t.Log(string(res))

4
go.mod
View File

@ -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
)

6
go.sum
View File

@ -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=