1
0
mirror of https://github.com/nitezs/sub2sing-box.git synced 2024-12-25 08:50:54 -05:00
sub2sing-box/internal/util/base64.go

24 lines
467 B
Go
Raw Normal View History

package util
2024-03-10 15:13:42 -04:00
import (
"encoding/base64"
"strings"
)
func DecodeBase64(s string) (string, error) {
s = strings.TrimSpace(s)
2024-03-18 15:33:25 -04:00
// url safe
if strings.Contains(s, "-") || strings.Contains(s, "_") {
s = strings.Replace(s, "-", "+", -1)
s = strings.Replace(s, "_", "/", -1)
}
2024-03-10 15:13:42 -04:00
if len(s)%4 != 0 {
s += strings.Repeat("=", 4-len(s)%4)
}
decodeStr, err := base64.StdEncoding.DecodeString(s)
if err != nil {
return "", err
}
return string(decodeStr), nil
}