Skip to content

Commit

Permalink
👔 up: strutil - update some decode and encode func logic
Browse files Browse the repository at this point in the history
  • Loading branch information
inhere committed Sep 1, 2023
1 parent c03d243 commit 983446a
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 4 deletions.
10 changes: 8 additions & 2 deletions strutil/encode.go
Original file line number Diff line number Diff line change
Expand Up @@ -94,14 +94,20 @@ func URLDecode(s string) string {
// -------------------- base encode --------------------
//

// base32 encoding with no padding
var (
B32Std = base32.StdEncoding.WithPadding(base32.NoPadding)
B32Hex = base32.HexEncoding.WithPadding(base32.NoPadding)
)

// B32Encode base32 encode
func B32Encode(str string) string {
return base32.StdEncoding.EncodeToString([]byte(str))
return B32Std.EncodeToString([]byte(str))
}

// B32Decode base32 decode
func B32Decode(str string) string {
dec, _ := base32.StdEncoding.DecodeString(str)
dec, _ := B32Std.DecodeString(str)
return string(dec)
}

Expand Down
9 changes: 7 additions & 2 deletions strutil/encode_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package strutil_test

import (
"testing"
"time"

"github.com/gookit/goutil/strutil"
"github.com/gookit/goutil/testutil/assert"
Expand Down Expand Up @@ -41,8 +42,12 @@ func TestURLEnDecode(t *testing.T) {
func TestBaseDecode(t *testing.T) {
is := assert.New(t)

is.Eq("MFRGG===", strutil.B32Encode("abc"))
is.Eq("abc", strutil.B32Decode("MFRGG==="))
is.Eq("GEZGCYTD", strutil.B32Encode("12abc"))
is.Eq("12abc", strutil.B32Decode("GEZGCYTD"))

// b23 hex
is.Eq("64P62OJ3", strutil.B32Hex.EncodeToString([]byte("12abc")))
is.Eq("68O34CPG70P3IC9M6GO32CO", strutil.B32Hex.EncodeToString([]byte(time.Now().Format("20060102150405"))))

is.Eq("YWJj", strutil.B64Encode("abc"))
is.Eq("abc", strutil.B64Decode("YWJj"))
Expand Down

0 comments on commit 983446a

Please sign in to comment.