2
我有一個函數,它將我想要的超時信息字符串化爲base64 url encode
,但是當我嘗試解碼編碼的字符串時,它會返回與最初創建的字符串無關的垃圾。你如何在Perl中對Base64進行編碼和解碼連接字符串?
輸出
[i] original string > 05cee990c62ca2ce5dfe6cd77115a96d|1331012040|1331004840|7200
[i] encoded string > MDVjZWU5OTBjNjJjYTJjZTVkZmU2Y2Q3NzExNWE5NmR8MTMzMTAxMjA0MHwxMzMxMDA0ODQwfDcyMDA=
[i] decoded string > Ó{ßtskgå×ÞéÇ{ï]ykÞ
悲哀的是同樣的功能是能夠解析來自Facebook檢索base64url編碼的信息。
功能
sub timeoutStringGen{
my ($name,$seconds) = @_;
my $uuid = uniqueID(); #generates random string
my $timeStr = time();
my $timeEnd = $timeStr + $seconds;
my $timeoutString = "$uuid|$timeEnd|$timeStr|$seconds";
my $encodedString = encode_base64url($timeoutString);
my $decodedString = decode_base64url($timeoutString);
_info "original string > $timeoutString"; #interal log function outputs to STDERR
_info "encoded string > $encodedString";
_info "decoded string > $decodedString";
return $timeoutString;
}
sub timeoutStringParse{
my ($timeoutString) = @_;
return 0 unless $timeoutString;
my ($uuid,$end,$start,$secs) = split /\Q|/,$timeoutString;
my $curr = time();
my $left = $end - $curr;
my $isExpired = ($left > 0) ? 1 : 0;
my $timeHash = {
uuid => $uuid,
end => $end,
start => $start,
secs => $secs,
exp => $isExpired,
left => $left,
curr => $curr
};
return $timeHash;
}
but maybe you can help me understand why these dont work to encode and decode the string properly
#--------
sub encode64{
my($data) = @_;
return MIME::Base64::encode_base64($data);
}
#--------
sub decode64{
my($data) = @_;
return MIME::Base64::decode_base64($data);
}
#--------
sub encode_base64url{
my($data) = @_;
return 0 unless $data;
$data = encode64($data);
$data =~ tr#\-_#+/#;
return($data);
}
#--------
sub decode_base64url{
my($data) = @_;
return 0 unless $data;
$data =~ tr#+/#\-_#;
$data = decode64($data);
return($data);
}
DOH!這正是問題所在。 *打額頭*無論如何謝謝! – qodeninja 2012-03-06 03:39:46
它發生在我們所有人身上。走出去,得到下一個錯誤! – bernie 2012-03-06 03:41:25