From 5968127d758322c7f02ba32f8cf1acd25d168512 Mon Sep 17 00:00:00 2001 From: Inhere Date: Sat, 16 Sep 2023 18:50:58 +0800 Subject: [PATCH] :necktie: up: rename the new sub package encodx to encodes --- encodx/encodx.go => encodes/encodes.go | 2 +- encodes/encodes_test.go | 29 +++++++++++++++++++ {encodx => encodes}/hashutil/hashutil.go | 10 +++---- {encodx => encodes}/hashutil/hashutil_test.go | 0 {encodx => encodes}/hashutil/passwd.go | 0 {encodx => encodes}/hashutil/passwd_test.go | 0 {encodx => encodes}/secutil/aes_crypt.go | 0 {encodx => encodes}/secutil/aes_crypt_test.go | 2 +- {encodx => encodes}/secutil/secutil.go | 0 encodx/encodx_test.go | 29 ------------------- strutil/encode.go | 7 ++--- strutil/random.go | 3 +- 12 files changed, 39 insertions(+), 43 deletions(-) rename encodx/encodx.go => encodes/encodes.go (98%) create mode 100644 encodes/encodes_test.go rename {encodx => encodes}/hashutil/hashutil.go (92%) rename {encodx => encodes}/hashutil/hashutil_test.go (100%) rename {encodx => encodes}/hashutil/passwd.go (100%) rename {encodx => encodes}/hashutil/passwd_test.go (100%) rename {encodx => encodes}/secutil/aes_crypt.go (100%) rename {encodx => encodes}/secutil/aes_crypt_test.go (97%) rename {encodx => encodes}/secutil/secutil.go (100%) delete mode 100644 encodx/encodx_test.go diff --git a/encodx/encodx.go b/encodes/encodes.go similarity index 98% rename from encodx/encodx.go rename to encodes/encodes.go index 199aef3a3..306325ea2 100644 --- a/encodx/encodx.go +++ b/encodes/encodes.go @@ -1,4 +1,4 @@ -package encodx +package encodes import ( "encoding/base32" diff --git a/encodes/encodes_test.go b/encodes/encodes_test.go new file mode 100644 index 000000000..069419533 --- /dev/null +++ b/encodes/encodes_test.go @@ -0,0 +1,29 @@ +package encodes_test + +import ( + "testing" + + "github.com/gookit/goutil/encodes" + "github.com/gookit/goutil/testutil/assert" +) + +func TestBaseDecode(t *testing.T) { + is := assert.New(t) + + is.Eq("GEZGCYTD", encodes.B32Encode("12abc")) + is.Eq("12abc", encodes.B32Decode("GEZGCYTD")) + + // b23 hex + is.Eq("64P62OJ3", encodes.B32Hex.EncodeToString([]byte("12abc"))) + // fmt.Println(time.Now().Format("20060102150405")) + dateStr := "20230908101122" + is.Eq("68O34CPG74O3GC9G64OJ4CG", encodes.B32Hex.EncodeToString([]byte(dateStr))) + + is.Eq("YWJj", encodes.B64Encode("abc")) + is.Eq("abc", encodes.B64Decode("YWJj")) + + is.Eq([]byte("YWJj"), encodes.B64EncodeBytes([]byte("abc"))) + is.Eq([]byte("abc"), encodes.B64DecodeBytes([]byte("YWJj"))) + + is.Eq("MTJhYmM", encodes.B64URL.EncodeToString([]byte("12abc"))) +} diff --git a/encodx/hashutil/hashutil.go b/encodes/hashutil/hashutil.go similarity index 92% rename from encodx/hashutil/hashutil.go rename to encodes/hashutil/hashutil.go index e9898ece1..7b7b7c4fb 100644 --- a/encodx/hashutil/hashutil.go +++ b/encodes/hashutil/hashutil.go @@ -12,7 +12,7 @@ import ( "hash/crc64" "strings" - "github.com/gookit/goutil/encodx" + "github.com/gookit/goutil/encodes" ) // hash algorithm names @@ -59,8 +59,8 @@ func Hash32(algo string, src any) string { // Base32Bytes generate base32 hash bytes by given algorithm func Base32Bytes(algo string, src any) []byte { bs := HashSum(algo, src) - dst := make([]byte, encodx.B32Hex.EncodedLen(len(bs))) - encodx.B32Hex.Encode(dst, bs) + dst := make([]byte, encodes.B32Hex.EncodedLen(len(bs))) + encodes.B32Hex.Encode(dst, bs) return dst } @@ -72,8 +72,8 @@ func Hash64(algo string, src any) string { // Base64Bytes generate base64 hash bytes by given algorithm func Base64Bytes(algo string, src any) []byte { bs := HashSum(algo, src) - dst := make([]byte, encodx.B64Std.EncodedLen(len(bs))) - encodx.B64Std.Encode(dst, bs) + dst := make([]byte, encodes.B64Std.EncodedLen(len(bs))) + encodes.B64Std.Encode(dst, bs) return dst } diff --git a/encodx/hashutil/hashutil_test.go b/encodes/hashutil/hashutil_test.go similarity index 100% rename from encodx/hashutil/hashutil_test.go rename to encodes/hashutil/hashutil_test.go diff --git a/encodx/hashutil/passwd.go b/encodes/hashutil/passwd.go similarity index 100% rename from encodx/hashutil/passwd.go rename to encodes/hashutil/passwd.go diff --git a/encodx/hashutil/passwd_test.go b/encodes/hashutil/passwd_test.go similarity index 100% rename from encodx/hashutil/passwd_test.go rename to encodes/hashutil/passwd_test.go diff --git a/encodx/secutil/aes_crypt.go b/encodes/secutil/aes_crypt.go similarity index 100% rename from encodx/secutil/aes_crypt.go rename to encodes/secutil/aes_crypt.go diff --git a/encodx/secutil/aes_crypt_test.go b/encodes/secutil/aes_crypt_test.go similarity index 97% rename from encodx/secutil/aes_crypt_test.go rename to encodes/secutil/aes_crypt_test.go index 09a9f700a..556a8947b 100644 --- a/encodx/secutil/aes_crypt_test.go +++ b/encodes/secutil/aes_crypt_test.go @@ -5,7 +5,7 @@ import ( "github.com/gookit/goutil/byteutil" "github.com/gookit/goutil/dump" - "github.com/gookit/goutil/strutil/secutil" + "github.com/gookit/goutil/encodes/secutil" "github.com/gookit/goutil/testutil/assert" ) diff --git a/encodx/secutil/secutil.go b/encodes/secutil/secutil.go similarity index 100% rename from encodx/secutil/secutil.go rename to encodes/secutil/secutil.go diff --git a/encodx/encodx_test.go b/encodx/encodx_test.go deleted file mode 100644 index 575807cf4..000000000 --- a/encodx/encodx_test.go +++ /dev/null @@ -1,29 +0,0 @@ -package encodx_test - -import ( - "testing" - - "github.com/gookit/goutil/encodx" - "github.com/gookit/goutil/testutil/assert" -) - -func TestBaseDecode(t *testing.T) { - is := assert.New(t) - - is.Eq("GEZGCYTD", encodx.B32Encode("12abc")) - is.Eq("12abc", encodx.B32Decode("GEZGCYTD")) - - // b23 hex - is.Eq("64P62OJ3", encodx.B32Hex.EncodeToString([]byte("12abc"))) - // fmt.Println(time.Now().Format("20060102150405")) - dateStr := "20230908101122" - is.Eq("68O34CPG74O3GC9G64OJ4CG", encodx.B32Hex.EncodeToString([]byte(dateStr))) - - is.Eq("YWJj", encodx.B64Encode("abc")) - is.Eq("abc", encodx.B64Decode("YWJj")) - - is.Eq([]byte("YWJj"), encodx.B64EncodeBytes([]byte("abc"))) - is.Eq([]byte("abc"), encodx.B64DecodeBytes([]byte("YWJj"))) - - is.Eq("MTJhYmM", encodx.B64URL.EncodeToString([]byte("12abc"))) -} diff --git a/strutil/encode.go b/strutil/encode.go index 5bda6e955..66e876f64 100644 --- a/strutil/encode.go +++ b/strutil/encode.go @@ -111,11 +111,8 @@ func B32Decode(str string) string { return string(dec) } -// base64 encoding with no padding -var ( - B64Std = base64.StdEncoding.WithPadding(base64.NoPadding) - B64URL = base64.URLEncoding.WithPadding(base64.NoPadding) -) +// B64Std base64 encoding with no padding +var B64Std = base64.StdEncoding.WithPadding(base64.NoPadding) // B64Encode base64 encode func B64Encode(str string) string { diff --git a/strutil/random.go b/strutil/random.go index 775377e5e..56410baf0 100644 --- a/strutil/random.go +++ b/strutil/random.go @@ -5,7 +5,6 @@ import ( "time" "github.com/gookit/goutil/byteutil" - "github.com/gookit/goutil/encodx" ) // some consts string chars @@ -89,7 +88,7 @@ func RandWithTpl(n int, letters string) string { // token, err := RandomString(16) // eg: "I7S4yFZddRMxQoudLZZ-eg" func RandomString(length int) (string, error) { b, err := RandomBytes(length) - return encodx.B64URL.EncodeToString(b), err + return encodes.B64URL.EncodeToString(b), err } // RandomBytes generate