mirror of
https://gitee.com/johng/gf
synced 2026-07-08 22:40:30 +08:00
improve gbase64
This commit is contained in:
@ -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))
|
||||
}
|
||||
|
||||
@ -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
|
||||
|
||||
@ -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))
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user