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

概述

报错代码

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

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

所以代码应该是这样

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标准包遇到的问题报错代码所遇到的程序开发问题。

如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。

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

评论列表共有 0 条评论

立即
投稿
返回
顶部