# base64_encode without padding

- Thread: https://externals.io/message/122630
- Original archive: https://news-web.php.net/php.internals/122630
- Started by Remi Collet on 2024-03-13T16:06:01+00:00
- Messages: 9
- Reply by email to internals@lists.php.net
- More for agents: search with https://externals.io/search.md?q={query} · RFCs at https://externals.io/rfc.md · JSON API at https://externals.io/api · documentation at https://externals.io/llms.txt

---

## #122630 — Remi Collet — 2024-03-13T16:06:01+00:00

URL: https://externals.io/message/122630#122630

I think padding should be optional (on by default to keep BC)

Of course in user land, simple to write

   $enc = trim(base64_encode('foo'), '=');

This proposal allow to simply use

   $enc = base64_encode('foo', PHP_BASE64_NO_PADDING);

And also expose it for extension as

   PHPAPI extern zend_string *php_base64_encode_ex
     (const unsigned char *, size_t, zend_long flags);


Use case: for ARGON2 password hashing
see https://github.com/php/php-src/pull/13635

It may seems ugly to add and remove th padding char

Is a RFC needed for such a minor feature ?


Remi

---

## #122631 — Remi Collet — 2024-03-13T16:08:06+00:00

URL: https://externals.io/message/122630#122631 · In reply to: #122630

Link to the PR  https://github.com/php/php-src/pull/13698

---

## #122633 — tim@bastelstu.be — 2024-03-13T19:29:28+00:00

URL: https://externals.io/message/122630#122633 · In reply to: #122630

Hi


> Use case: for ARGON2 password hashing
> see https://github.com/php/php-src/pull/13635
> 
> It may seems ugly to add and remove th padding char
> 
> Is a RFC needed for such a minor feature ?
> 

Previous related discussion: https://externals.io/message/119243#119243. 
My remark about using a cache-timing safe encoder might be relevant for 
the argon2 usecase.

Best regards
Tim Düstzerhus

---

## #122637 — Dik Takken — 2024-03-14T11:46:46+00:00

URL: https://externals.io/message/122630#122637 · In reply to: #122630

> I think padding should be optional (on by default to keep BC)
> 
> Of course in user land, simple to write
> 
>    $enc = trim(base64_encode('foo'), '=');
> 
> This proposal allow to simply use
> 
>    $enc = base64_encode('foo', PHP_BASE64_NO_PADDING);
> 
> And also expose it for extension as
> 
>    PHPAPI extern zend_string *php_base64_encode_ex
>      (const unsigned char *, size_t, zend_long flags);
> 
> 
> Use case: for ARGON2 password hashing
> see https://github.com/php/php-src/pull/13635
> 
> It may seems ugly to add and remove th padding char
> 
> Is a RFC needed for such a minor feature ?
> 
> 
> Remi

Great addition! Only I would strongly prefer a boolean argument over a 
flag. Especially now that we have named arguments, adding a bunch of 
optional boolean arguments to the signature does not lead to unreadable 
code anymore. In fact, I think it yields code that is easier to read.

Regards,
Dik

---

## #122639 — Remi Collet — 2024-03-14T14:53:47+00:00

URL: https://externals.io/message/122630#122639 · In reply to: #122637

Le 14/03/2024 à 12:46, Dik Takken a écrit :

> Only I would strongly prefer a boolean argument over a flag.

Make sense.
PR updated

---

## #122643 — Yasuo Ohgaki — 2024-03-14T20:31:17+00:00

URL: https://externals.io/message/122630#122643 · In reply to: #122630

>
> I think padding should be optional (on by default to keep BC)
>
> Of course in user land, simple to write
>
>    $enc = trim(base64_encode('foo'), '=');
>
> This proposal allow to simply use
>
>    $enc = base64_encode('foo', PHP_BASE64_NO_PADDING);
>
>
Please add PHP_BASE64_URL flag also.
BASE64 URL encoding (URL safe Base64 encoding) is used everywhere.
i.e. str_replace(['+','/','='], ['-','_',''], base64_encode($str);

https://base64.guru/standards/base64url

Decoding should be extended.
i.e. base64_decode($encoded, PHP_BASE64_URL)

Adding PHP_BASE64_STRICT flag may be reasonable, that requires specific
encoding strictly for decoding.

Regards,

--
Yasuo Ohgaki
yohgaki@ohgaki.net



> And also expose it for extension as
>
>    PHPAPI extern zend_string *php_base64_encode_ex
>      (const unsigned char *, size_t, zend_long flags);
>
>
> Use case: for ARGON2 password hashing
> see https://github.com/php/php-src/pull/13635
>
> It may seems ugly to add and remove th padding char
>
> Is a RFC needed for such a minor feature ?
>
>
> Remi

---

## #122645 — Remi Collet — 2024-03-15T08:32:01+00:00

URL: https://externals.io/message/122630#122645 · In reply to: #122643

Le 14/03/2024 à 21:31, Yasuo Ohgaki a écrit :

> Please add PHP_BASE64_URL flag also.

This is another feature, out of the scope of this PR


Remi

---

## #122646 — Robert Landers — 2024-03-15T11:32:32+00:00

URL: https://externals.io/message/122630#122646 · In reply to: #122645

On Fri, Mar 15, 2024 at 9:34 AM Remi Collet <Fedora@famillecollet.com>
wrote:

> Le 14/03/2024 à 21:31, Yasuo Ohgaki a écrit :
>
> > […]
>
> This is another feature, out of the scope of this PR
>
>
> Remi
>

Perhaps you should consider it? Base64 "just without" padding isn't really
a standard (that I'm aware of), but url-base64 is and padding is optional.
Simply removing padding (in most real-world cases) isn't enough so this
flag only gets you part of the way there. So, perhaps adding two flags
PHP_BASE64_URL along with this padding flag would be really useful for
those of us having to transform a base64 encoded string to a url-base64.

Robert Landers
Software Engineer
Utrecht NL

---

## #122773 — Remi Collet — 2024-03-27T07:15:24+00:00

URL: https://externals.io/message/122630#122773 · In reply to: #122630

Le 13/03/2024 à 17:06, Remi Collet a écrit :

>    PHPAPI extern zend_string *php_base64_encode_ex
>      (const unsigned char *, size_t, zend_long flags);

This is done in master

No change in user-land
and no plan from me.


Remi
