Closed
Description
Please answer these questions before submitting your issue. Thanks!
- What version of Go are you using (
go version
)?
go1.6
- What operating system and processor architecture are you using (
go env
)?
All operating systems. - What did you do?
https://play.golang.org/p/DiwWTDmGyC
package main
import (
"encoding/base64"
"encoding/hex"
"fmt"
)
func main() {
// Good base64.
goodBytes, err := base64.StdEncoding.DecodeString("WvLTlMrX9NpYDQlEIFlnDA==")
if err != nil {
fmt.Println(err)
return
}
// Bad base64.
badBytes, err := base64.StdEncoding.DecodeString("WvLTlMrX9NpYDQlEIFlnDB==")
if err != nil {
fmt.Println(err)
return
}
goodHex := hex.EncodeToString(goodBytes)
badHex := hex.EncodeToString(badBytes)
fmt.Println(goodHex)
fmt.Println(badHex)
goodBytes, err = hex.DecodeString(goodHex)
if err != nil {
fmt.Println(err)
return
}
badBytes, err = hex.DecodeString(badHex)
if err != nil {
fmt.Println(err)
return
}
goodBase64 := base64.StdEncoding.EncodeToString(goodBytes)
badBase64 := base64.StdEncoding.EncodeToString(badBytes)
fmt.Println(goodBase64)
fmt.Println(badBase64)
}
- What did you expect to see?
Error for the second bad base64
badBytes, err := base64.StdEncoding.DecodeString("WvLTlMrX9NpYDQlEIFlnDB==")
Here err should be non 'nil'.
An example of ruby decoder throwing ArgumentError for similar conditions under RFC4648.
require "base64"
good = Base64.strict_decode64("WvLTlMrX9NpYDQlEIFlnDA==") ## RFC4648
puts good.length
begin
bad = Base64.strict_decode64("WvLTlMrX9NpYDQlEIFlnDB==") ## RFC4648
puts bad.length
rescue ArgumentError
puts "Invalid base64"
end
- What did you see instead?
Success for the second bad base64.