Skip to content

Instantly share code, notes, and snippets.

@jef-sure
Created June 18, 2019 11:57
Show Gist options
  • Save jef-sure/96fcf0f070751c03a3648645f3cfecfb to your computer and use it in GitHub Desktop.
Save jef-sure/96fcf0f070751c03a3648645f3cfecfb to your computer and use it in GitHub Desktop.
# Perl vs PHP
# Perl (with signatures)
sub encode_burl($param) {
my $burl = encode_base64($param, "");
$burl =~ tr|+/|-_|;
my $padd = $burl =~ tr|=||d;
return $burl . $padd;
}
sub decode_burl($burl) {
my $padd = substr($burl, -1, 1, "");
$burl .= "=" x $padd;
$burl =~ tr|-_|+/|;
return decode_base64($burl);
}
# PHP
function encode_burl($plainText)
{
$base64 = base64_encode($plainText);
$base64url = strtr($base64, '+/', '-_');
$qpos = strpos($base64url, '=');
if ($qpos === FALSE)
return $base64url . '0';
$blen = strlen($base64url);
return substr($base64url, 0, $qpos) . ($blen - $qpos);
}
function decode_burl($burl)
{
$padd = substr($burl, - 1, 1);
$base64 = strtr(substr($burl, 0, - 1), '-_', '+/') . str_repeat('=', $padd);
return base64_decode($base64);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment