Skip to content

Instantly share code, notes, and snippets.

@fredshonorio
Last active August 29, 2015 14:02
Show Gist options
  • Save fredshonorio/7b3a1258a0b3efec87df to your computer and use it in GitHub Desktop.
Save fredshonorio/7b3a1258a0b3efec87df to your computer and use it in GitHub Desktop.
Yet another base64url for php
<?php
function base64url_encode($data) {
if (!mb_check_encoding($data, "ASCII"))
throw new InvalidArgumentException("Arguments can only contain ASCII characters.");
return rtrim(strtr(base64_encode($data), '+/', '-_'), '=');
}
function base64url_decode($data) {
return base64_decode(strtr($data, '-_', '+/'));
}
?>
<?php
require("base64url.php");
function test_enc(){
$handle = fopen("plain", "r");
if ($handle) {
while (($line = fgets($handle)) !== false) {
echo base64url_encode(rtrim($line, "\n")) . "\n";
}
} else {
}
fclose($handle);
}
function test_dec(){
$handle = fopen("encoded", "r");
if ($handle) {
while (($line = fgets($handle)) !== false) {
echo base64url_decode(rtrim($line, "\n")) . "\n";
}
} else {
}
fclose($handle);
}
if (PHP_SAPI == 'cli')
if ($argv[1] == 'enc')
test_enc();
if ($argv[1] == 'dec')
test_dec();
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment