From 66b060dfd3f490aed6017f7de2e4c6482d911603 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9D=8E=E5=85=89=E6=98=A5?= Date: Tue, 24 May 2022 23:59:57 +0800 Subject: [PATCH] - init --- .drone.yml | 17 +++++++++++++++++ .gitignore | 9 +++++++++ go.mod | 7 +++++++ go.sum | 4 ++++ gobase64.go | 34 ++++++++++++++++++++++++++++++++++ gobase64_test.go | 45 +++++++++++++++++++++++++++++++++++++++++++++ version.go | 3 +++ version_test.go | 7 +++++++ 8 files changed, 126 insertions(+) create mode 100644 .drone.yml create mode 100644 .gitignore create mode 100644 go.mod create mode 100644 go.sum create mode 100644 gobase64.go create mode 100644 gobase64_test.go create mode 100644 version.go create mode 100644 version_test.go diff --git a/.drone.yml b/.drone.yml new file mode 100644 index 0000000..c56c479 --- /dev/null +++ b/.drone.yml @@ -0,0 +1,17 @@ +kind: pipeline +type: docker +name: clone + +steps: + - name: Test + image: golang:1.18 + commands: + - go env -w GO111MODULE=on + - go env -w GOPROXY=https://goproxy.cn,direct + - go test -v ./... + - name: Benchmark + image: golang:1.18 + commands: + - go env -w GO111MODULE=on + - go env -w GOPROXY=https://goproxy.cn,direct + - go test -bench=. -benchmem \ No newline at end of file diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..4794692 --- /dev/null +++ b/.gitignore @@ -0,0 +1,9 @@ +.env +.git +.svn +.idea +.vscode +*.log +goinit.sh +gomod.sh +/vendor/ \ No newline at end of file diff --git a/go.mod b/go.mod new file mode 100644 index 0000000..b6aeb63 --- /dev/null +++ b/go.mod @@ -0,0 +1,7 @@ +module go.dtapp.net/gobase64 + +go 1.18 + +require go.dtapp.net/gophp v1.0.0 + +require go.dtapp.net/gostring v1.0.3 // indirect diff --git a/go.sum b/go.sum new file mode 100644 index 0000000..b9db192 --- /dev/null +++ b/go.sum @@ -0,0 +1,4 @@ +go.dtapp.net/gophp v1.0.0 h1:bjAqT8GAI4gfIaBy7JTB41o0vt7nnFEvpcwKAFUDxLU= +go.dtapp.net/gophp v1.0.0/go.mod h1:s78OSeHXbBKsTW3YF/JPSQ33wWE3wzplSUNxuwHOvN4= +go.dtapp.net/gostring v1.0.3 h1:KSOq4D77/g5yZN/bqWfZ0kOOaPr/P1240vg03+XdENI= +go.dtapp.net/gostring v1.0.3/go.mod h1:+ggrOvgQDQturi1QGsXEpyRN/ZPoRDaqhMujIk5lrgQ= diff --git a/gobase64.go b/gobase64.go new file mode 100644 index 0000000..a0e3e4c --- /dev/null +++ b/gobase64.go @@ -0,0 +1,34 @@ +package gobase64 + +import ( + "encoding/base64" + "go.dtapp.net/gophp" +) + +// Encode base64编码 +func Encode(input string) string { + return base64.StdEncoding.EncodeToString([]byte(input)) +} + +// Decode base64解码 +func Decode(input string) string { + decodeString, err := base64.StdEncoding.DecodeString(input) + if err != nil { + return "" + } + return string(decodeString) +} + +// EncodeUrl Base64安全URL编码 +func EncodeUrl(input string) string { + return gophp.Rtrim(gophp.Strtr(base64.StdEncoding.EncodeToString([]byte(input)), "+/", "-_"), "=") +} + +// DecodeUrl Base64安全URL解码 +func DecodeUrl(input string) string { + decodeString, err := base64.StdEncoding.DecodeString(gophp.StrPad(gophp.Strtr(input, "-_", "+/"), len(input)/4, "=")) + if err != nil { + return "" + } + return string(decodeString) +} diff --git a/gobase64_test.go b/gobase64_test.go new file mode 100644 index 0000000..12546f2 --- /dev/null +++ b/gobase64_test.go @@ -0,0 +1,45 @@ +package gobase64 + +import ( + "testing" +) + +func TestEncode(t *testing.T) { + t.Log(Encode("广东茂名")) +} + +func BenchmarkEncode(b *testing.B) { + for i := 0; i < b.N; i++ { + b.Log(Encode("广东茂名")) + } +} + +func TestDecode(t *testing.T) { + t.Log(Decode("5bm/5Lic6IyC5ZCN")) +} + +func BenchmarkDecode(b *testing.B) { + for i := 0; i < b.N; i++ { + b.Log(Decode("5bm/5Lic6IyC5ZCN")) + } +} + +func TestEncodeUrl(t *testing.T) { + t.Log(EncodeUrl("广东茂名")) +} + +func BenchmarkEncodeUrl(b *testing.B) { + for i := 0; i < b.N; i++ { + b.Log(EncodeUrl("广东茂名")) + } +} + +func TestDecodeUrl(t *testing.T) { + t.Log(DecodeUrl("5bm_5Lic6IyC5ZCN")) +} + +func BenchmarkDecodeUrl(b *testing.B) { + for i := 0; i < b.N; i++ { + b.Log(DecodeUrl("5bm_5Lic6IyC5ZCN")) + } +} diff --git a/version.go b/version.go new file mode 100644 index 0000000..981540a --- /dev/null +++ b/version.go @@ -0,0 +1,3 @@ +package gobase64 + +const Version = "1.0.0" diff --git a/version_test.go b/version_test.go new file mode 100644 index 0000000..c342a02 --- /dev/null +++ b/version_test.go @@ -0,0 +1,7 @@ +package gobase64 + +import "testing" + +func TestVersion(t *testing.T) { + t.Log(Version) +}