13

So I'm stumped. I know there's lots of Base64 encoders/decoders for JS, but not for the modified (and Facebook-favored) Base64URL variation. So far searching across stackoverflow has come up dry.

Yes, I could use PHP or another server-side library to decode this, but I'm trying to keep this universal regardless of what platform I'm using... for example, if I were to host a HTML-only Facebook app on Amazon S3/CloudFront and only use their JS SDK and jQuery to take care of processing forms and getting data.

That said, does anyone know of any Base64URL-specific decoders for JavaScript?

Thanks in advance!

chrisfullman
  • 141
  • 1
  • 1
  • 5
  • 1
    `Base64Url` encoding is specified in [RFC 4648, The Base16, Base32, and Base64 Data Encodings](http://tools.ietf.org/html/rfc4648). The only difference between `Base64` and `Base64Url` is two values (62 and 63). Just replace `"+"` with `"-"` and `"/"` with `"_"`. – jww Jun 23 '14 at 18:12
  • 2
    @jww would this be correct? `var base64url = function(aStr) { return btoa(aStr.replace(/\+/g,'-').replace(/\//g,'_')).replace(/\=+$/m,'') }` with the trialing `=`'s stripped? – Noitidart May 11 '16 at 22:01

3 Answers3

28

Use this before decoding :

var decode = function(input) {
        // Replace non-url compatible chars with base64 standard chars
        input = input
            .replace(/-/g, '+')
            .replace(/_/g, '/');

        // Pad out with standard base64 required padding characters
        var pad = input.length % 4;
        if(pad) {
          if(pad === 1) {
            throw new Error('InvalidLengthError: Input base64url string is the wrong length to determine padding');
          }
          input += new Array(5-pad).join('=');
        }

        return input;
    }

After using this function you can use any base64 decoder

mohamad
  • 500
  • 6
  • 5
4

Solution:

var b64str = base64.encode('foo bar');

// fix padding according to the new format
b64str = b64str.padRight(b64str.length + (4 - b64str.length % 4) % 4, '=');

Using this great base64 encode/decode: http://code.google.com/p/stringencoders/source/browse/trunk/javascript/base64.js

Also depends on the padRight method:

String.prototype.padRight = function(n, pad){
    t = this;
    if(n > this.length)
        for(i = 0; i < n-this.length; i++)
            t += pad;
    return t;
}
Simeon
  • 5,443
  • 3
  • 27
  • 50
  • 1
    Hi Simeon... I think this could work, but I think I also need to replace "-" and "+" with "_" and "/" - good starting point though. I was hoping to find one with everything wrapped in the same library, but I guess I might just have to modify a library a little bit afterall. – chrisfullman Mar 08 '11 at 21:33
  • Strange... actually, I just did a JS version of this answer: http://stackoverflow.com/questions/1228701/code-for-decoding-encoding-a-modified-base64-url – Simeon Mar 09 '11 at 08:24
-8
var str = "string";
var encoded = btoa(str); // encode a string (base64)
var decoded = atob(encoded); //decode the string 
alert( ["string base64 encoded:",encoded,"\r\n", "string base64 decoded:",decoded].join('') );
jww
  • 90,984
  • 81
  • 374
  • 818
The Mask
  • 16,429
  • 36
  • 107
  • 177