Edit report at https://bugs.php.net/bug.php?id=61982&edit=1

 ID:                 61982
 Updated by:         ni...@php.net
 Reported by:        nospam at vyznev dot net
 Summary:            Provide encode/decode functions for RFC 4648
                     "base64url" format
-Status:             Open
+Status:             Duplicate
 Type:               Feature/Change Request
 Package:            *General Issues
 PHP Version:        Irrelevant
 Block user comment: N
 Private report:     N

 New Comment:

This is a duplicate of https://bugs.php.net/bug.php?id=33650.


Previous Comments:
------------------------------------------------------------------------
[2012-05-09 14:31:05] nospam at vyznev dot net

Description:
------------
Including Base64-encoded data in URLs and file names is a commonly encountered 
task.  The PHP base64_encode() function is not suitable for this, since its 
output may encode the characters "+" (which has special meaning in URL query 
parameters) and "/" (which is used as a path separator in file names and URL 
paths).

RFC 4648 (http://tools.ietf.org/html/rfc4648) defines, in section 5, a standard 
variant of Base64 encoding, called "base64url", for this purpose.  It differs 
from the usual "base64" format in that the characters "+" and "/" are replaced 
with "-" and "_" respectively, and that the use of the padding character "=" is 
optional and not usually recommended.

I would like to suggest that functions for encoding and decoding data in this 
format be provided in PHP alongside the existing base64_encode() and 
base64_decode() functions.  Although such functions can be relatively easily 
implemented in pure PHP code based on the existing Base64 encoding functions, a 
built-in implementation would presumably be both more efficient and more likely 
to be user correctly by script authors.

A sample PHP implementation of the proposed functions (which could be included 
in the documentation for backward compatibility purposes) is included below:

    function base64url_encode( $data, $pad = false ) {
        $base64 = strtr( base64_encode( $data ), '+/', '-_' );
        return ( $pad ? $base64 : rtrim( $base64, '=' ) );
    }

    function base64url_decode( $base64, $strict = false ) {
        return base64_decode( strtr( $base64, '-_+/', '+/-_' ), $strict );
    }




------------------------------------------------------------------------



-- 
Edit this bug report at https://bugs.php.net/bug.php?id=61982&edit=1

Reply via email to