在golang中解码base64后一些字符消失
Some character disappear after decode base64 in golang
我构建了简单的程序来生成 Json Web 令牌 (JWT),一切正常 运行 直到我进行令牌验证以验证 JWT,问题出现在我解码 base64 字符串以检查header 标记。二进制文件的某些部分消失了,而字符串值来自编码的 base64。这是我的代码和结果
package auth
import "encoding/base64"
import "crypto/sha256"
import "encoding/json"
import "strings"
import "crypto/hmac"
import "encoding/hex"
import "fmt"
type Header struct{
Alg string
Typ string
}
type Payload struct{
id int
name string
userType bool
keepLogin bool
}
var key string = "12345678" //temporary
var payloadJson Payload
func GenerateToken(alg string, typ string, payload []byte)(string, error){
var headerEncoded, payloadEncoded, signature, mergedEncoded string
header := Header{Alg: alg,Typ: typ}
hmacDeclare := hmac.New(sha256.New, []byte(key))
headerJson, errHeader := json.Marshal(header)//struct -> byte
if errHeader != nil{
return "",errHeader
}
headerEncoded = encodeBase64(headerJson)//byte -> string
payloadEncoded = encodeBase64(payload)//byte -> string
mergedEncoded = headerEncoded+"."+payloadEncoded
if alg=="SHA256"{
hmacDeclare.Write([]byte(mergedEncoded))
signature = hex.EncodeToString(hmacDeclare.Sum(nil))
}
finalToken := headerEncoded+"."+payloadEncoded+"."+signature
//JUST SAMPLE TO TEST JWT RESULT
fmt.Println("Before encode (byte) => ", headerJson)
fmt.Println("Before encode (string) => ", string(headerJson))
fmt.Println("After encode (base64/string) => ", headerEncoded)
checkDecode,_ := decodeBase64(headerEncoded)//string -> byte
fmt.Println("After decode (byte) => ", checkDecode)
fmt.Println("After decode (string) => ", string(checkDecode))
// _, _ = VerifyToken(finalToken)
return finalToken, nil
}
func encodeBase64(data []byte) string {
formatString := strings.TrimRight(base64.StdEncoding.EncodeToString(data),"=")
return formatString
}
func decodeBase64(data string)([]byte, error){
result, err := base64.StdEncoding.DecodeString(data)
if err != nil{
return result, err
}
return result, nil
}
这是结果
Before encode (byte) => [123 34 65 108 103 34 58 34 83 72 65 50 53 54 34 44 34 84 121 112 34 58 34 74 87 84 34 125]
Before encode (string) => {"Alg":"SHA256","Typ":"JWT"}
After encode (base64/string) => eyJBbGciOiJTSEEyNTYiLCJUeXAiOiJKV1QifQ
After decode (byte) => [123 34 65 108 103 34 58 34 83 72 65 50 53 54 34 44 34 84 121 112 34 58 34 74 87 84 34]
After decode (string) => {"Alg":"SHA256","Typ":"JWT"
有人遇到同样的问题吗?我试过浏览它但没有发现相同的问题
如果您不需要填充,请不要 trim,而是使用 WithPadding
and pass in NoPadding
。
func encodeBase64(data []byte) string {
e := base64.StdEncoding.WithPadding(base64.NoPadding)
return e.EncodeToString(data)
}
func decodeBase64(data string) ([]byte, error) {
e := base64.StdEncoding.WithPadding(base64.NoPadding)
result, err := e.DecodeString(data)
if err != nil {
return result, err
}
return result, nil
}
我构建了简单的程序来生成 Json Web 令牌 (JWT),一切正常 运行 直到我进行令牌验证以验证 JWT,问题出现在我解码 base64 字符串以检查header 标记。二进制文件的某些部分消失了,而字符串值来自编码的 base64。这是我的代码和结果
package auth
import "encoding/base64"
import "crypto/sha256"
import "encoding/json"
import "strings"
import "crypto/hmac"
import "encoding/hex"
import "fmt"
type Header struct{
Alg string
Typ string
}
type Payload struct{
id int
name string
userType bool
keepLogin bool
}
var key string = "12345678" //temporary
var payloadJson Payload
func GenerateToken(alg string, typ string, payload []byte)(string, error){
var headerEncoded, payloadEncoded, signature, mergedEncoded string
header := Header{Alg: alg,Typ: typ}
hmacDeclare := hmac.New(sha256.New, []byte(key))
headerJson, errHeader := json.Marshal(header)//struct -> byte
if errHeader != nil{
return "",errHeader
}
headerEncoded = encodeBase64(headerJson)//byte -> string
payloadEncoded = encodeBase64(payload)//byte -> string
mergedEncoded = headerEncoded+"."+payloadEncoded
if alg=="SHA256"{
hmacDeclare.Write([]byte(mergedEncoded))
signature = hex.EncodeToString(hmacDeclare.Sum(nil))
}
finalToken := headerEncoded+"."+payloadEncoded+"."+signature
//JUST SAMPLE TO TEST JWT RESULT
fmt.Println("Before encode (byte) => ", headerJson)
fmt.Println("Before encode (string) => ", string(headerJson))
fmt.Println("After encode (base64/string) => ", headerEncoded)
checkDecode,_ := decodeBase64(headerEncoded)//string -> byte
fmt.Println("After decode (byte) => ", checkDecode)
fmt.Println("After decode (string) => ", string(checkDecode))
// _, _ = VerifyToken(finalToken)
return finalToken, nil
}
func encodeBase64(data []byte) string {
formatString := strings.TrimRight(base64.StdEncoding.EncodeToString(data),"=")
return formatString
}
func decodeBase64(data string)([]byte, error){
result, err := base64.StdEncoding.DecodeString(data)
if err != nil{
return result, err
}
return result, nil
}
这是结果
Before encode (byte) => [123 34 65 108 103 34 58 34 83 72 65 50 53 54 34 44 34 84 121 112 34 58 34 74 87 84 34 125]
Before encode (string) => {"Alg":"SHA256","Typ":"JWT"}
After encode (base64/string) => eyJBbGciOiJTSEEyNTYiLCJUeXAiOiJKV1QifQ
After decode (byte) => [123 34 65 108 103 34 58 34 83 72 65 50 53 54 34 44 34 84 121 112 34 58 34 74 87 84 34]
After decode (string) => {"Alg":"SHA256","Typ":"JWT"
有人遇到同样的问题吗?我试过浏览它但没有发现相同的问题
如果您不需要填充,请不要 trim,而是使用 WithPadding
and pass in NoPadding
。
func encodeBase64(data []byte) string {
e := base64.StdEncoding.WithPadding(base64.NoPadding)
return e.EncodeToString(data)
}
func decodeBase64(data string) ([]byte, error) {
e := base64.StdEncoding.WithPadding(base64.NoPadding)
result, err := e.DecodeString(data)
if err != nil {
return result, err
}
return result, nil
}