Base64 encode/decode without padding on golang ( appengine )

huangapple go评论110阅读模式
英文:

Base64 encode/decode without padding on golang ( appengine )

问题

有没有一种方法可以在不在字符串末尾添加填充的情况下对字符串进行Base64编码/解码?我的意思是不使用'=='结尾。

我正在使用base64.URLEncoding.EncodeToString进行编码,它运行得很完美,但我没有看到可以选择不使用末尾填充的方法(就像在Java中一样)。

英文:

There's a way to encode/decode a string to/from Base64 without having the padding at the end? I mean the '==' ending.

I'm using base64.URLEncoding.EncodeToString to encode and it works perfectly but I didn't see a way to decide to not use the padding at the end ( like on java ).

答案1

得分: 21

Go1.5版本将在Encoding上添加一个WithPadding选项。此外,还将添加两个预定义的编码,RawStdEncodingRawURLEncoding,它们将不包含填充。

由于您正在使用app-engine,并且暂时无法访问Go1.5,您可以编写一些辅助函数根据需要添加和删除填充。

以下是一个编码和解码字符串的示例。如果需要,可以轻松地调整它以使用[]byte更高效地工作。

func base64EncodeStripped(s string) string {
    encoded := base64.StdEncoding.EncodeToString([]byte(s))
    return strings.TrimRight(encoded, "=")
}

func base64DecodeStripped(s string) (string, error) {
    if i := len(s) % 4; i != 0 {
        s += strings.Repeat("=", 4-i)
    }
    decoded, err := base64.StdEncoding.DecodeString(s)
    return string(decoded), err
}

希望对您有所帮助!

英文:

Go1.5 will have a WithPadding option on Encoding.

This also will add 2 pre-defined encodings, RawStdEncoding, and RawURLEncoding, which will have no padding.

Though since you're on app-engine, and won't have access to Go1.5 for a while, you can make some helper function to add and remove the padding as needed.

Here is an example to encode and decode strings. If you need, it could easily be adapted to work more efficiently using []byte.

func base64EncodeStripped(s string) string {
	encoded := base64.StdEncoding.EncodeToString([]byte(s))
	return strings.TrimRight(encoded, "=")
}

func base64DecodeStripped(s string) (string, error) {
	if i := len(s) % 4; i != 0 {
		s += strings.Repeat("=", 4-i)
	}
	decoded, err := base64.StdEncoding.DecodeString(s)
	return string(decoded), err
}

答案2

得分: 9

简单地说,使用base64.RawStdEncoding.EncodeToString代替base64.StdEncoding.EncodeToString

或者,使用base64.RawURLEncoding.EncodeToString代替base64.URLEncoding.EncodeToString

参考:请查看源代码注释第94至110行。

RawURLEncoding是在RFC 4648中定义的无填充的替代base64编码。它通常用于URL和文件名。它与URLEncoding相同,但省略了填充字符。

英文:

Simply,

use base64.RawStdEncoding.EncodeToString instead of base64.StdEncoding.EncodeToString

OR else

use base64.RawURLEncoding.EncodeToString instead of base64.URLEncoding.EncodeToString.

Reference: see source-code comments Line 94 to 110:

// RawURLEncoding is the unpadded alternate base64 encoding defined in RFC 4648.
// It is typically used in URLs and file names.
// This is the same as URLEncoding but omits padding characters.

答案3

得分: 0

编码:

str := "encode this"
encodedStr := base64.StdEncoding.
    WithPadding(base64.NoPadding).
    EncodeToString([]byte(str))

fmt.Println(encodedStr)

解码:

data, err := base64.
    StdEncoding.WithPadding(base64.NoPadding).
    DecodeString(encodedStr)

if err != nil {
    fmt.Println(err)
}

fmt.Println(string(data))

Go Playground: https://go.dev/play/p/TAZgLMSSe-K

英文:

To Encode:

str := "encode this"
encodedStr := base64.StdEncoding.
	WithPadding(base64.NoPadding).
	EncodeToString([]byte(str))

fmt.Println(encodedStr)

To Decode:

data, err := base64.
    StdEncoding.WithPadding(base64.NoPadding).
    DecodeString(encodedStr)

if err != nil {
    fmt.Println(err)
}

fmt.Println(string(data))

Go Playground: https://go.dev/play/p/TAZgLMSSe-K

huangapple
  • 本文由 发表于 2015年8月13日 01:19:59
  • 转载请务必保留本文链接:https://go.coder-hub.com/31971614.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定