File tree Expand file tree Collapse file tree 1 file changed +5
-4
lines changed
Expand file tree Collapse file tree 1 file changed +5
-4
lines changed Original file line number Diff line number Diff line change @@ -54,17 +54,18 @@ func Base64Decode(str string) (string, error) {
5454 return string (s ), e
5555}
5656
57+ var urlSafeBase64EncodeReplacer = strings .NewReplacer (`/` , `_` , `+` , `-` )
58+ var urlSafeBase64DecodeReplacer = strings .NewReplacer (`_` , `/` , `-` , `+` )
59+
5760// URLSafeBase64 base64字符串编码为URL友好的字符串
5861func URLSafeBase64 (str string , encode bool ) string {
5962 if encode { // 编码后处理
6063 str = strings .TrimRight (str , `=` )
61- str = strings .Replace (str , `/` , `_` , - 1 )
62- str = strings .Replace (str , `+` , `-` , - 1 )
64+ str = urlSafeBase64EncodeReplacer .Replace (str )
6365 return str
6466 }
6567 // 解码前处理
66- str = strings .Replace (str , `_` , `/` , - 1 )
67- str = strings .Replace (str , `-` , `+` , - 1 )
68+ str = urlSafeBase64DecodeReplacer .Replace (str )
6869 var missing = (4 - len (str )% 4 ) % 4
6970 if missing > 0 {
7071 str += strings .Repeat (`=` , missing )
You can’t perform that action at this time.
0 commit comments