github.com/OrigamiWang/msd/micro@v0.0.0-20240229032328-b62246268db9/util/base64/base64.go (about) 1 package base64 2 3 import ( 4 "encoding/base64" 5 "strings" 6 ) 7 8 func EncodeBase64(src []byte) string { 9 return base64.URLEncoding.EncodeToString(src) 10 } 11 12 func DecodeBase64(str string) ([]byte, error) { 13 // fill '=' until the length of the str is the multiple of 4 14 if l := len(str) % 4; l > 0 { 15 str += strings.Repeat("=", 4-l) 16 } 17 decoded, err := base64.URLEncoding.DecodeString(str) 18 if err != nil { 19 return nil, err 20 } 21 22 return decoded, nil 23 }