Add "base64url" as an encoding option for binaryEncode()

Right now, binaryEncode() and binaryDecode() support hex and base64 encodings. But, over the years, base64url has emerged as a common practice because it’s compact, like base64, but also removes several characters that are problematic in terms of URL-encoding.

It’s easy enough to implement this on your own:

var value = binaryEncode( bytes, "base64" )
    .replace( "+", "-", "all" )
    .replace( "/", "_", "all" )
    .replace( "=", "", "all" )
;

But, it would be nice to be able to just allow base64url to be passed around as an encoding argument and have it “just work”.

Looking at Lucee’s Coder.java implementation, it seems like it would be just one more else if to add.

6 Likes

Decode as well, although putting back trimmed “=” may be problematical.

I think the == padding stuff is just something that is used to make the number of bytes divisible by 4 or something (I can’t quite remember the details). But, there’s definitely a way to get it back. That said, yes, it would definitely need to be in the decode version as well.