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

huangapple go评论71阅读模式
英文:

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

问题

我有一个URL安全编码的字符串(由Perl生成),我需要在Go中解码它。
这里有两个程序,一个是Perl,一个是Go - Perl可以正常工作,但是Go会出错。
我无法理解这个问题。
请帮忙!

=== Perl代码 - 正常工作

#!/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代码 - 出错

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)
    }
}

=== 结束

由于解码返回的结果被截断,因此无法在下一步中使用它。

英文:

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.

答案1

得分: 11

当没有填充时,请使用RawURLEncoding

_, err := b64.RawURLEncoding.DecodeString(str)
英文:

Use the RawURLEncoding when there's no padding:

_, err := b64.RawURLEncoding.DecodeString(str)

huangapple
  • 本文由 发表于 2017年1月25日 15:21:01
  • 转载请务必保留本文链接:https://go.coder-hub.com/41845629.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定