I have an URL Safe encoded string (produced by Perl), that I need to decode in Go. Here are two programs, in Perl and in Go - Perl works fine, but Go rises error. I can't understand it. Please help!
=== Perl code - works fine
#!/usr/bin/env perl
use common::sense;
use MIME::Base64::URLSafe;
my $str = 'Oi6cQzmolrUhkgHsNehtj9p_OsasB_6CIeygK0owoxTsXCtVWyQi-7DXxIJiaV-kSc6PGNC6uNz5V0A9QOGCaeCy6PolQY2Lt_v4JM42VEbsuML8guHfMO0ydvbXVcCR-yLfkz5CO0f-P1hVqxJBD8qPvk1t1DRzqmHP41DSfIm_WzlhtITnd_Wjt6E3CFS78HL3XjJlM-QBW9Z_GZgic8y7TlOWFzCRUf2Q-EZschrDi9l81E93XBNKe8knInL_uFN_oK_ob7fjnkGJO54RNn3coVsrzuIoNa6AI6oWLfsaJ5NyQYor5P0';
say urlsafe_b64decode($str);
=== Go code - rises error
package main
import (
b64 "encoding/base64"
"fmt"
)
func main() {
str := "Oi6cQzmolrUhkgHsNehtj9p_OsasB_6CIeygK0owoxTsXCtVWyQi-7DXxIJiaV-kSc6PGNC6uNz5V0A9QOGCaeCy6PolQY2Lt_v4JM42VEbsuML8guHfMO0ydvbXVcCR-yLfkz5CO0f-P1hVqxJBD8qPvk1t1DRzqmHP41DSfIm_WzlhtITnd_Wjt6E3CFS78HL3XjJlM-QBW9Z_GZgic8y7TlOWFzCRUf2Q-EZschrDi9l81E93XBNKe8knInL_uFN_oK_ob7fjnkGJO54RNn3coVsrzuIoNa6AI6oWLfsaJ5NyQYor5P0"
fmt.Println("source B64:", str)
_, err := b64.URLEncoding.DecodeString(str)
if err != nil {
fmt.Println("error b64:", err)
}
}
=== END
As decode returns truncated result it is impossible to use it in next step.
Use the RawURLEncoding when there's no padding:
_, err := b64.RawURLEncoding.DecodeString(str)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With