Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to URL Safe decode Base64 in Go, encoded in Perl?

Tags:

base64

go

decode

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.

like image 679
Demiurg Avatar asked Dec 23 '22 21:12

Demiurg


1 Answers

Use the RawURLEncoding when there's no padding:

_, err := b64.RawURLEncoding.DecodeString(str)
like image 81
Bayta Darell Avatar answered Feb 18 '23 23:02

Bayta Darell