first commit
This commit is contained in:
commit
8b05fc4c9a
15
.gitignore
vendored
Normal file
15
.gitignore
vendored
Normal file
@ -0,0 +1,15 @@
|
||||
# Binaries for programs and plugins
|
||||
*.exe
|
||||
*.exe~
|
||||
*.dll
|
||||
*.so
|
||||
*.dylib
|
||||
|
||||
# Test binary, built with `go test -c`
|
||||
*.test
|
||||
|
||||
# Output of the go coverage tool, specifically when used with LiteIDE
|
||||
*.out
|
||||
|
||||
# Dependency directories (remove the comment below to include it)
|
||||
# vendor/
|
46
README.md
Normal file
46
README.md
Normal file
@ -0,0 +1,46 @@
|
||||
#base62json
|
||||
|
||||
```
|
||||
brew install mingw-w64
|
||||
|
||||
编译 32位 dll
|
||||
CGO_ENABLED=1 CC=i686-w64-mingw32-gcc CXX=i686-w64-mingw32-g++ GOOS=windows GOARCH=386 go build --buildmode=c-shared -ldflags="-s -w" -o main_x32.dll
|
||||
|
||||
编译 64位 dll
|
||||
CGO_ENABLED=1 CC=x86_64-w64-mingw32-gcc CXX=x86_64-w64-mingw32-g++ GOOS=windows GOARCH=amd64 go build --buildmode=c-shared -ldflags="-s -w" -o main_x64.dll
|
||||
```
|
||||
|
||||
```c#
|
||||
class Program
|
||||
{
|
||||
[DllImport("main", EntryPoint = "SetCharacters", CallingConvention = CallingConvention.Cdecl)]
|
||||
extern static bool SetCharacters(byte[] a);
|
||||
|
||||
[DllImport("main", EntryPoint = "Encode", CharSet = CharSet.Ansi, CallingConvention = CallingConvention.Cdecl)]
|
||||
extern static IntPtr Encode(byte[] a);
|
||||
|
||||
[DllImport("main", EntryPoint = "Decode", CallingConvention = CallingConvention.Cdecl)]
|
||||
extern static IntPtr Decode(byte[] test);
|
||||
|
||||
static void Main(string[] args)
|
||||
{
|
||||
byte[] k = Encoding.ASCII.GetBytes("vY1lKxmABkMeRhV5q2XIEfT4ojuJdzaPcNUCs7Ft8GZnOW3Hip0LQ9gywrD6bS");
|
||||
|
||||
bool bl = SetCharacters(k);
|
||||
|
||||
string tmp = "{\"a\":1}";
|
||||
|
||||
byte[] t = Encoding.ASCII.GetBytes(tmp);
|
||||
|
||||
var b = Encode(t);
|
||||
|
||||
tmp = Marshal.PtrToStringAnsi(b);
|
||||
|
||||
t = Encoding.ASCII.GetBytes("m1jD8rjX5vYUmTMFSR8AgRDdvWPD8vdckSDPO0yfYrnEStBvMJBifXy1kUfE28AlDK0bYy8TVjdK02GyyZ7ck");
|
||||
|
||||
var d = Decode(t);
|
||||
|
||||
Console.WriteLine(Marshal.PtrToStringAnsi(d));
|
||||
}
|
||||
}
|
||||
```
|
66
base62json.go
Normal file
66
base62json.go
Normal file
@ -0,0 +1,66 @@
|
||||
package base62json
|
||||
|
||||
import (
|
||||
"errors"
|
||||
|
||||
"git.fsdpf.net/go/base62"
|
||||
"git.fsdpf.net/go/jsonpack"
|
||||
|
||||
"github.com/samber/lo"
|
||||
)
|
||||
|
||||
const cursor = 59
|
||||
|
||||
var bs62 *base62.Base62
|
||||
|
||||
func init() {
|
||||
bs62 = base62.GetInstance()
|
||||
}
|
||||
|
||||
// 项目入口
|
||||
func GenerateProjectSecret(key, secret string) ([]byte, error) {
|
||||
appSecret := lo.FindUniques([]byte(secret))
|
||||
|
||||
if len(appSecret) != 62 {
|
||||
return nil, errors.New("Invalid secret")
|
||||
}
|
||||
|
||||
for i := 0; i < len(key); i++ {
|
||||
appSecret[key[i]%cursor], appSecret[cursor-(key[i]%cursor)] = appSecret[cursor-(key[i]%cursor)], appSecret[key[i]%cursor]
|
||||
}
|
||||
|
||||
return appSecret, nil
|
||||
}
|
||||
|
||||
func SetCharacters(secret []byte) error {
|
||||
if res, err := bs62.SetCharacters(secret); err == nil {
|
||||
bs62 = res
|
||||
} else {
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func Encode[T []any | map[string]any](data T) (string, error) {
|
||||
str, err := jsonpack.Pack(data)
|
||||
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
|
||||
res := bs62.Encode([]byte(str))
|
||||
|
||||
return res, nil
|
||||
}
|
||||
|
||||
func Decode(str string) (string, error) {
|
||||
json, err := bs62.Decode(str)
|
||||
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
|
||||
data := string(json)
|
||||
|
||||
return jsonpack.Unpack(data)
|
||||
}
|
53
base62json_test.go
Normal file
53
base62json_test.go
Normal file
@ -0,0 +1,53 @@
|
||||
package base62json
|
||||
|
||||
import (
|
||||
"testing"
|
||||
)
|
||||
|
||||
func main() {
|
||||
|
||||
}
|
||||
|
||||
func TestEcode(t *testing.T) {
|
||||
a := []any{"ab", "啊啊", 1, -2, 11111111111, -222222222222, true, false, nil, 0, 1.22, 1.222222222, -1.222222222}
|
||||
|
||||
b1 := "5MQOdKtsl8sXRDOMO5Jq7vTWsq2KQwq4CE2sCyqIPm0XZ4uDq9cil9d4Cu2Y9RxusgfuoSGSPLiX5LaT1iQ7V483R6SCUXPlfVCgVg2XlQEPetPO9lokLkTWmQFOpsSr"
|
||||
|
||||
b, _ := Encode(a)
|
||||
|
||||
if b == b1 {
|
||||
t.Logf("\n 输入=>%d \n 输出=>%s", a, b)
|
||||
} else {
|
||||
t.Fatalf("\n 输入=>%s \n 预计输出=>%s \n 实际输出=>%s", a, b1, b)
|
||||
}
|
||||
}
|
||||
|
||||
func TestDecode(t *testing.T) {
|
||||
a := "5MQOdKtsl8sXRDOMO5Jq7vTWsq2KQwq4CE2sCyqIPm0XZ4uDq9cil9d4Cu2Y9RxusgfuoSGSPLiX5LaT1iQ7V483R6SCUXPlfVCgVg2XlQEPetPO9lokLkTWmQFOpsSr"
|
||||
|
||||
b1 := `["ab","啊啊",1,-2,11111111111,-222222222222,true,false,null,0,1.22,1.222222222,-1.222222222]`
|
||||
|
||||
b, _ := Decode(a)
|
||||
|
||||
if b == b1 {
|
||||
t.Logf("\n 输入=>%s \n 输出=>%s", a, b)
|
||||
} else {
|
||||
t.Fatalf("\n 输入=>%s \n 预计输出=>%s \n 实际输出=>%s", a, b1, b)
|
||||
}
|
||||
}
|
||||
|
||||
func TestGenerateProjectSecret(t *testing.T) {
|
||||
// PrV4uZRqFHWOjkYIo07J1wdU6cDyXzh5Asi2xb3QMCNSmEvLl8pgetBGfnT9aK 数据库
|
||||
// PrV4uZRqFHWOjkYIo07J1wdU6cDyXzh5Asi2xb3QMCNSmEvLl8pgetBGfnT9aK 加密后
|
||||
|
||||
|
||||
// PrV4uZReFpWOjkYIoNCJ1wdU6cDyXzh5Asi2xb3QM70SmEvLl8HgqtBGfnT9aK
|
||||
res, _ := GenerateProjectSecret("demo", "PrV4uZReFpWOjkYIoNCJ1wdU6cDyXzh5Asi2xb3QM70SmEvLl8HgqtBGfnT9aK")
|
||||
t.Log(string(res))
|
||||
|
||||
res1, _ := GenerateProjectSecret("demo", "PrV4uZRqFHWOjkYIo07J1wdU6cDyXzh5Asi2xb3QMCNSmEvLl8pgetBGfnT9aK")
|
||||
t.Log(string(res1))
|
||||
|
||||
// PrV4uZRqFHWOjkYIo07J1wdU6cDyXzh5Asi2xb3QMCNSmEvLl8pgetBGfnT9aK
|
||||
// PrV4uZReFpWOjkYIoNCJ1wdU6cDyXzh5Asi2xb3QM70SmEvLl8HgqtBGfnT9aK
|
||||
}
|
11
go.mod
Normal file
11
go.mod
Normal file
@ -0,0 +1,11 @@
|
||||
module git.fsdpf.net/go/base62json
|
||||
|
||||
go 1.18
|
||||
|
||||
require (
|
||||
git.fsdpf.net/go/base62 v0.0.0-20230414160450-79c590957c64
|
||||
git.fsdpf.net/go/jsonpack v0.0.0-20230414160652-34bc26cb4839
|
||||
github.com/samber/lo v1.38.1
|
||||
)
|
||||
|
||||
require golang.org/x/exp v0.0.0-20220303212507-bbda1eaf7a17 // indirect
|
8
go.sum
Normal file
8
go.sum
Normal file
@ -0,0 +1,8 @@
|
||||
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/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/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=
|
||||
golang.org/x/exp v0.0.0-20220303212507-bbda1eaf7a17/go.mod h1:lgLbSvA5ygNOMpwM/9anMpWVlVJ7Z+cHWq/eFuinpGE=
|
Loading…
Reference in New Issue
Block a user