From 961ca0879d0fb513218f5523a27eb3e2b9a20fd7 Mon Sep 17 00:00:00 2001 From: John Date: Tue, 2 Jul 2019 16:56:10 +0800 Subject: [PATCH] improve gbase64 --- g/encoding/gbase64/gbase64.go | 32 +++++++++++++++++++++++-------- g/net/ghttp/ghttp_request_auth.go | 7 ++++--- geg/other/test.go | 17 +++++++--------- 3 files changed, 35 insertions(+), 21 deletions(-) diff --git a/g/encoding/gbase64/gbase64.go b/g/encoding/gbase64/gbase64.go index 1ed8e92ab..1ce7267ae 100644 --- a/g/encoding/gbase64/gbase64.go +++ b/g/encoding/gbase64/gbase64.go @@ -4,20 +4,36 @@ // If a copy of the MIT was not distributed with this file, // You can obtain one at https://github.com/gogf/gf. -// Package gbase64 provides useful API for BASE64 encoding/decoding algorithms. +// Package gbase64 provides useful API for BASE64 encoding/decoding algorithm. package gbase64 import ( "encoding/base64" ) -// base64 encode -func Encode(str string) string { - return base64.StdEncoding.EncodeToString([]byte(str)) +// Encode encodes bytes with BASE64 algorithm. +func Encode(src []byte) []byte { + dst := make([]byte, base64.StdEncoding.EncodedLen(len(src))) + base64.StdEncoding.Encode(dst, src) + return dst } -// base64 decode -func Decode(str string) (string, error) { - s, e := base64.StdEncoding.DecodeString(str) - return string(s), e +// Decode decodes bytes with BASE64 algorithm. +func Decode(dst []byte) ([]byte, error) { + src := make([]byte, base64.StdEncoding.DecodedLen(len(dst))) + _, err := base64.StdEncoding.Decode(src, dst) + if err != nil { + return nil, err + } + return src, nil +} + +// EncodeString encodes bytes with BASE64 algorithm. +func EncodeString(src []byte) string { + return string(Encode(src)) +} + +// DecodeString decodes string with BASE64 algorithm. +func DecodeString(str string) ([]byte, error) { + return Decode([]byte(str)) } diff --git a/g/net/ghttp/ghttp_request_auth.go b/g/net/ghttp/ghttp_request_auth.go index 219c2c391..a097f4327 100644 --- a/g/net/ghttp/ghttp_request_auth.go +++ b/g/net/ghttp/ghttp_request_auth.go @@ -8,9 +8,10 @@ package ghttp import ( "fmt" - "github.com/gogf/gf/g/encoding/gbase64" "net/http" "strings" + + "github.com/gogf/gf/g/encoding/gbase64" ) // 设置Basic Auth校验提示 @@ -40,12 +41,12 @@ func (r *Request) BasicAuth(user, pass string, tips ...string) bool { } switch authArray[0] { case "Basic": - authStr, err := gbase64.Decode(authArray[1]) + authBytes, err := gbase64.DecodeString(authArray[1]) if err != nil { r.Response.WriteStatus(http.StatusForbidden, err.Error()) return false } - authArray := strings.SplitN(string(authStr), ":", 2) + authArray := strings.SplitN(string(authBytes), ":", 2) if len(authArray) != 2 { r.Response.WriteStatus(http.StatusForbidden) return false diff --git a/geg/other/test.go b/geg/other/test.go index 1b134b6c5..073e90196 100644 --- a/geg/other/test.go +++ b/geg/other/test.go @@ -1,7 +1,9 @@ package main import ( - "github.com/gogf/gf/g" + "fmt" + + "github.com/gogf/gf/g/encoding/gbase64" "github.com/gogf/gf/g/net/ghttp" ) @@ -12,13 +14,8 @@ func (order *Order) Get(r *ghttp.Request) { } func main() { - s := g.Server() - s.BindHookHandlerByMap("/api.v1/*any", map[string]ghttp.HandlerFunc{ - "BeforeServe": func(r *ghttp.Request) { - r.Response.CORSDefault() - }, - }) - s.BindObjectRest("/api.v1/{.struct}", new(Order)) - s.SetPort(8199) - s.Run() + s := `BgsnyD6IBEzExNDUzNjEzNDg4MzYxOTMzNjQSShAJGgzns7vnu5/pgJrnn6UiOGh0dHA6Ly9wdWItbWVkLWxvZ28uaW1ncy5tZWRsaW5rZXIubmV0L25ldy1zeXN0ZW1AM3gucG5` + b, err := gbase64.DecodeString(s) + fmt.Println(err) + fmt.Println(string(b)) }