type script:
Buffer.from('Мегафон').toString('base64') //0JzQtdCz0LDRhNC 0L0=
go:
decode, err := base64.URLEncoding.DecodeString("0JzQtdCz0LDRhNC 0L0=") //err : illegal base64 data at input byte 15
if i try:
base64.URLEncoding.EncodeToString([]byte("Мегафон")) //0JzQtdCz0LDRhNC-0L0=
That is, the difference is only in and -.
I made it work with
v = strings.ReplaceAll(v, " ", "-")
but this is clearly not a solution.
CodePudding user response:
You are comparing "standard" base64 encoding with URL-safe base64 encoding. To use normal base64 encoding in Go don't use base64.URLEncoding.DecodeString
but instead base64.StdEncoding.DecodeString
.
CodePudding user response:
You encoded your string with standard base64 encoding, and decoded with url-safe decoder. Check out base64.go
// StdEncoding is the standard base64 encoding, as defined in
// RFC 4648.
var StdEncoding = NewEncoding(encodeStd)
// URLEncoding is the alternate base64 encoding defined in RFC 4648.
// It is typically used in URLs and file names.
var URLEncoding = NewEncoding(encodeURL)
This will works.
base64.StdEncoding.DecodeString("0JzQtdCz0LDRhNC 0L0=")
If you want to encode your string url-safely, use base64url
instead of base64
. Then you can decode the string with base64.URLEncoding
.
Buffer.from('Мегафон').toString('base64url'); //'0JzQtdCz0LDRhNC-0L0'