Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

base64 url safe encoding #407

Closed
Gashmob opened this issue Jun 5, 2023 · 1 comment
Closed

base64 url safe encoding #407

Gashmob opened this issue Jun 5, 2023 · 1 comment
Assignees
Labels
Type: Enhancement Most issues will probably ask for additions or changes.

Comments

@Gashmob
Copy link
Contributor

Gashmob commented Jun 5, 2023

Is your feature request related to a problem? Please describe.
For the implementation of WebAuthn in a web app, I need to send a key id in the uri. So I use base64 encoding for that. But following the rfc the encoding uses + and / characters which are not really safe for an uri. Section 5 of rfc describe an url version of encoding using - and _ characters. Is it possible to implement it inside psl?

Describe the solution you'd like
It will be nice to have functions like this:

function encodeUrl(string $binary): string;

function decodeUrl(string $base64): string;

Describe alternatives you've considered
Currently, as a workaround I'm doing that:

strtr(encode($key_id), '+/', '-_');
@Gashmob Gashmob added the Type: Enhancement Most issues will probably ask for additions or changes. label Jun 5, 2023
@azjezz
Copy link
Owner

azjezz commented Jun 5, 2023

I'm okay with adding two new functions to Psl\Encoding\Base64, or a new Psl\Encoding\Base64\Url sub-component.

a PR would be welcome.

use Psl;
use Psl\Encoding\Base64;

$data = '...';

$encoded = Base64\url_encode($data);
$decoded = Base64\url_decode($encoded);

Psl\invariant($data === $decoded, 'expected $decoded to be the same as $data');

or

use Psl;
use Psl\Encoding\Base64\Url;

$data = '...';

$encoded = Url\encode($data);
$decoded = Url\decode($encoded);

Psl\invariant($data === $decoded, 'expected $decoded to be the same as $data');

Gashmob added a commit to Gashmob/psl that referenced this issue Jun 5, 2023
issue azjezz#407

This commit introduces functions url_encode and url_decode in
Psl\Encoding\Base64 namespace. Following rfc4648 it uses base64url
alphabet to make base64 encoding url safe.
@azjezz azjezz closed this as completed in a5466a3 Jun 16, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Type: Enhancement Most issues will probably ask for additions or changes.
Projects
None yet
Development

No branches or pull requests

2 participants