我是靠谱客的博主 真实柠檬,这篇文章主要介绍使用GoBase64标准包遇到的问题报错代码,现在分享给大家,希望可以做个参考。

报错代码

复制代码
1
2
3
4
5
6
7
8
9
enstr := "eyJBY2NvdW50SWQiOiIxIiwiQ2xpZW50IjoiIiwiRW1haWwiOiJ5YWt1Lm1pb3RvQGdtYWlsLmNvbSIsIk1hc3RlckZsYWciOnRydWUsImV4cCI6MTU0ODc0NTY5OSwidHlwZSI6ImVtcGxveWVlcyJ9" // {"AccountId":"1","Client":"","Email":"yaku.mioto@gmail.com","MasterFlag":true,"exp":1548745699,"type":"employees"} debytes, err := base64.StdEncoding.DecodeString(enstr) if err := nil { // ... // err output: illegal base64 data at input byte xxx } // ...

源码: https://golang.org/src/encoding/base64/base64.go

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
const ( StdPadding rune = '=' // Standard padding character NoPadding rune = -1 // No padding ) const encodeStd = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/" // StdEncoding is the standard base64 encoding, as defined in // RFC 4648. var StdEncoding = NewEncoding(encodeStd) // RawStdEncoding is the standard raw, unpadded base64 encoding, // as defined in RFC 4648 section 3.2. // This is the same as StdEncoding but omits padding characters. var RawStdEncoding = StdEncoding.WithPadding(NoPadding) // NewEncoding returns a new padded Encoding defined by the given alphabet, // which must be a 64-byte string that does not contain the padding character // or CR / LF ('r', 'n'). // The resulting Encoding uses the default padding character ('='), // which may be changed or disabled via WithPadding. func NewEncoding(encoder string) *Encoding { // ... }

原因

jwt 的 base64 是去除填充物的, 所以不能使用 StdEncoding 应该使用 RawStdEncoding

所以代码应该是这样

复制代码
1
2
3
4
5
6
7
8
9
enstr := "eyJBY2NvdW50SWQiOiIxIiwiQ2xpZW50IjoiIiwiRW1haWwiOiJ5YWt1Lm1pb3RvQGdtYWlsLmNvbSIsIk1hc3RlckZsYWciOnRydWUsImV4cCI6MTU0ODc0NTY5OSwidHlwZSI6ImVtcGxveWVlcyJ9" // {"AccountId":"1","Client":"","Email":"yaku.mioto@gmail.com","MasterFlag":true,"exp":1548745699,"type":"employees"} debytes, err := base64.RawStdEncoding.DecodeString(enstr) if err := nil { // ... // err output: illegal base64 data at input byte xxx } // ...

转载:https://mioto.me/2018/01/problems-encountered-in-using-gobase64-standard-library/

参考

  • https://stackoverflow.com/a/42683706/9176562

最后

以上就是真实柠檬最近收集整理的关于使用GoBase64标准包遇到的问题报错代码的全部内容,更多相关使用GoBase64标准包遇到内容请搜索靠谱客的其他文章。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(74)

评论列表共有 0 条评论

立即
投稿
返回
顶部