Last active
August 29, 2015 14:02
-
-
Save fredshonorio/7b3a1258a0b3efec87df to your computer and use it in GitHub Desktop.
Yet another base64url for php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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, '-_', '+/')); | |
} | |
?> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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