Skip to content

Commit

Permalink
up: str - update and add some encode util functions
Browse files Browse the repository at this point in the history
  • Loading branch information
inhere committed Nov 26, 2022
1 parent 55b0e17 commit 9adb2fe
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 25 deletions.
86 changes: 62 additions & 24 deletions strutil/encode.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,21 +74,25 @@ func StripSlashes(s string) string {
//

// Md5 Generate a 32-bit md5 string
func Md5(src any) string { return GenMd5(src) }
func Md5(src any) string {
return hex.EncodeToString(Md5Bytes(src))
}

// MD5 Generate a 32-bit md5 string
func MD5(src any) string { return GenMd5(src) }
func MD5(src any) string { return Md5(src) }

// GenMd5 Generate a 32-bit md5 string
func GenMd5(src any) string {
func GenMd5(src any) string { return Md5(src) }

// Md5Bytes Generate a 32-bit md5 bytes
func Md5Bytes(src any) []byte {
h := md5.New()
if s, ok := src.(string); ok {
h.Write([]byte(s))
} else {
h.Write([]byte(fmt.Sprint(src)))
}

return hex.EncodeToString(h.Sum(nil))
return h.Sum(nil)
}

// URLEncode encode url string.
Expand Down Expand Up @@ -126,39 +130,73 @@ func B32Decode(str string) string {
return string(dec)
}

// Base64 encode
func Base64(str string) string {
return base64.StdEncoding.EncodeToString([]byte(str))
}

// B64Encode base64 encode
func B64Encode(str string) string {
return base64.StdEncoding.EncodeToString([]byte(str))
}

// B64EncodeBytes base64 encode
func B64EncodeBytes(src []byte) []byte {
buf := make([]byte, base64.StdEncoding.EncodedLen(len(src)))
base64.StdEncoding.Encode(buf, src)

return buf
}

// B64Decode base64 decode
func B64Decode(str string) string {
dec, _ := base64.StdEncoding.DecodeString(str)
return string(dec)
}

// BaseEncoder struct
type BaseEncoder struct {
// Base value
Base int
}
// B64DecodeBytes base64 decode
func B64DecodeBytes(str string) []byte {
dbuf := make([]byte, base64.StdEncoding.DecodedLen(len(str)))
n, _ := base64.StdEncoding.Decode(dbuf, []byte(str))

// NewBaseEncoder instance
func NewBaseEncoder(base int) *BaseEncoder {
return &BaseEncoder{Base: base}
return dbuf[:n]
}

// Encode handle
func (be *BaseEncoder) Encode(s string) string {
return s
// BaseEncoder interface
type BaseEncoder interface {
Encode(dst []byte, src []byte)
EncodeToString(src []byte) string
Decode(dst []byte, src []byte) (n int, err error)
DecodeString(s string) ([]byte, error)
}

// Decode handle
func (be *BaseEncoder) Decode(s string) (string, error) {
return s, nil
// BaseType for base encoding
type BaseType uint8

// types for base encoding
const (
BaseTypeStd BaseType = iota
BaseTypeHex
BaseTypeURL
BaseTypeRawStd
BaseTypeRawURL
)

// Encoding instance
func Encoding(base int, typ BaseType) BaseEncoder {
if base == 32 {
switch typ {
case BaseTypeHex:
return base32.HexEncoding
default:
return base32.StdEncoding
}
}

// base 64
switch typ {
case BaseTypeURL:
return base64.URLEncoding
case BaseTypeRawURL:
return base64.RawURLEncoding
case BaseTypeRawStd:
return base64.RawStdEncoding
default:
return base64.StdEncoding
}
}
2 changes: 1 addition & 1 deletion strutil/encode_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ func TestBaseDecode(t *testing.T) {
is.Eq("MFRGG===", strutil.B32Encode("abc"))
is.Eq("abc", strutil.B32Decode("MFRGG==="))

is.Eq("YWJj", strutil.Base64("abc"))
is.Eq("YWJj", strutil.B64Encode("abc"))
is.Eq("abc", strutil.B64Decode("YWJj"))
is.Eq([]byte("YWJj"), strutil.B64EncodeBytes([]byte("abc")))
}

0 comments on commit 9adb2fe

Please sign in to comment.